From 028663b04b222d0f2da5b113f16f8b3a852fc1c9 Mon Sep 17 00:00:00 2001 From: Evan Sherwood Date: Thu, 15 Dec 2016 16:09:46 -0800 Subject: [PATCH] Add `addModulesDirectories` config (#256) * Add `addModulesDirectories` config Allows appending to the default list of modules directories. Relevant discussion: https://github.com/postcss/postcss-import/issues/195 --- README.md | 13 +++++++++++++ index.js | 1 + lib/resolve-id.js | 2 +- test/fixtures/resolve-custom-modules.css | 7 +++++++ test/fixtures/resolve-custom-modules.expected.css | 8 ++++++++ test/resolve.js | 9 +++++++++ test/shared_modules/shared-auto/index.css | 1 + test/shared_modules/shared-by-hand/style.css | 1 + test/shared_modules/shared-dep/index.css | 1 + test/shared_modules/shared-fake/main.css | 1 + test/shared_modules/shared-fake/package.json | 3 +++ test/shared_modules/shared-nest/index.css | 1 + .../shared-nest/shared_modules/shared-ed/index.css | 1 + test/shared_modules/shared-use-dep-too/index.css | 1 + test/shared_modules/shared-use-dep/index.css | 1 + 15 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/resolve-custom-modules.css create mode 100644 test/fixtures/resolve-custom-modules.expected.css create mode 100644 test/shared_modules/shared-auto/index.css create mode 100644 test/shared_modules/shared-by-hand/style.css create mode 100644 test/shared_modules/shared-dep/index.css create mode 100644 test/shared_modules/shared-fake/main.css create mode 100644 test/shared_modules/shared-fake/package.json create mode 100644 test/shared_modules/shared-nest/index.css create mode 100644 test/shared_modules/shared-nest/shared_modules/shared-ed/index.css create mode 100644 test/shared_modules/shared-use-dep-too/index.css create mode 100644 test/shared_modules/shared-use-dep/index.css diff --git a/README.md b/README.md index cc126f1a..d0af4a96 100755 --- a/README.md +++ b/README.md @@ -198,6 +198,19 @@ postcss: function(webpack) { } ``` +#### `addModulesDirectories` + +Type: `Array` +Default: `[]` + +An array of folder names to add to [Node's resolver](https://github.com/substack/node-resolve). +Values will be appended to the default resolve directories: +`["node_modules", "web_modules"]`. + +This option is only for adding additional directories to default resolver. If +you provide your own resolver via the `resolve` configuration option above, then +this value will be ignored. + #### Example with some options ```js diff --git a/index.js b/index.js index 4d3e945a..326ab7f0 100755 --- a/index.js +++ b/index.js @@ -16,6 +16,7 @@ function AtImport(options) { resolve: resolveId, load: loadContent, plugins: [], + addModulesDirectories: [], }, options) options.root = path.resolve(options.root) diff --git a/lib/resolve-id.js b/lib/resolve-id.js index 82afed3b..a5002e49 100644 --- a/lib/resolve-id.js +++ b/lib/resolve-id.js @@ -29,7 +29,7 @@ module.exports = function(id, base, options) { var resolveOpts = { basedir: base, - moduleDirectory: moduleDirectories, + moduleDirectory: moduleDirectories.concat(options.addModulesDirectories), paths: paths, extensions: [ ".css" ], packageFilter: function processPackage(pkg) { diff --git a/test/fixtures/resolve-custom-modules.css b/test/fixtures/resolve-custom-modules.css new file mode 100644 index 00000000..b80b56ee --- /dev/null +++ b/test/fixtures/resolve-custom-modules.css @@ -0,0 +1,7 @@ +@import "shared-fake"; +@import "shared-auto"; +@import "shared-nest"; +@import "shared-by-hand/style.css"; +@import "shared-use-dep"; +@import "shared-use-dep-too"; +@import "shared-use-dep" screen; diff --git a/test/fixtures/resolve-custom-modules.expected.css b/test/fixtures/resolve-custom-modules.expected.css new file mode 100644 index 00000000..011e8074 --- /dev/null +++ b/test/fixtures/resolve-custom-modules.expected.css @@ -0,0 +1,8 @@ +.shared-fake{} +.shared-auto{} +.shared-nested{} +.shared-byHand{} +.shared-dep{} +@media screen{ +.shared-dep{} +} diff --git a/test/resolve.js b/test/resolve.js index 5b8f906c..90dd05e4 100644 --- a/test/resolve.js +++ b/test/resolve.js @@ -54,3 +54,12 @@ test("should be able to consume npm package or local modules", t => { from: "fixtures/imports/foo.css", }) }) + +test("should be able to consume modules from custom modules directories", t => { + return compareFixtures(t, "resolve-custom-modules", { + path: null, + addModulesDirectories: [ "shared_modules" ], + }, { + from: "fixtures/imports/foo.css", + }) +}) diff --git a/test/shared_modules/shared-auto/index.css b/test/shared_modules/shared-auto/index.css new file mode 100644 index 00000000..c0389260 --- /dev/null +++ b/test/shared_modules/shared-auto/index.css @@ -0,0 +1 @@ +.shared-auto{} diff --git a/test/shared_modules/shared-by-hand/style.css b/test/shared_modules/shared-by-hand/style.css new file mode 100644 index 00000000..8c4529b7 --- /dev/null +++ b/test/shared_modules/shared-by-hand/style.css @@ -0,0 +1 @@ +.shared-byHand{} diff --git a/test/shared_modules/shared-dep/index.css b/test/shared_modules/shared-dep/index.css new file mode 100644 index 00000000..bcb816d4 --- /dev/null +++ b/test/shared_modules/shared-dep/index.css @@ -0,0 +1 @@ +.shared-dep{} diff --git a/test/shared_modules/shared-fake/main.css b/test/shared_modules/shared-fake/main.css new file mode 100644 index 00000000..7154d977 --- /dev/null +++ b/test/shared_modules/shared-fake/main.css @@ -0,0 +1 @@ +.shared-fake{} diff --git a/test/shared_modules/shared-fake/package.json b/test/shared_modules/shared-fake/package.json new file mode 100644 index 00000000..f0181f31 --- /dev/null +++ b/test/shared_modules/shared-fake/package.json @@ -0,0 +1,3 @@ +{ + "style": "main.css" +} diff --git a/test/shared_modules/shared-nest/index.css b/test/shared_modules/shared-nest/index.css new file mode 100644 index 00000000..82685050 --- /dev/null +++ b/test/shared_modules/shared-nest/index.css @@ -0,0 +1 @@ +@import "shared-ed"; diff --git a/test/shared_modules/shared-nest/shared_modules/shared-ed/index.css b/test/shared_modules/shared-nest/shared_modules/shared-ed/index.css new file mode 100644 index 00000000..5f420fc8 --- /dev/null +++ b/test/shared_modules/shared-nest/shared_modules/shared-ed/index.css @@ -0,0 +1 @@ +.shared-nested{} diff --git a/test/shared_modules/shared-use-dep-too/index.css b/test/shared_modules/shared-use-dep-too/index.css new file mode 100644 index 00000000..8d53b982 --- /dev/null +++ b/test/shared_modules/shared-use-dep-too/index.css @@ -0,0 +1 @@ +@import "shared-dep"; diff --git a/test/shared_modules/shared-use-dep/index.css b/test/shared_modules/shared-use-dep/index.css new file mode 100644 index 00000000..8d53b982 --- /dev/null +++ b/test/shared_modules/shared-use-dep/index.css @@ -0,0 +1 @@ +@import "shared-dep";