diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ce336ad..d6bfcf1f 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - Changed: custom resolve has more responsibility for paths resolving. See [resolve option](https://github.com/postcss/postcss-import#resolve) for more information about this change ([#116](https://github.com/postcss/postcss-import/pull/116)) +- Added: custom resolve function can return array of paths +([#120](https://github.com/postcss/postcss-import/pull/120)) # 7.1.3 - 2015-11-05 diff --git a/README.md b/README.md index d37171af..ff527f72 100755 --- a/README.md +++ b/README.md @@ -145,7 +145,7 @@ Type: `Function` Default: `null` You can overwrite the default path resolving way by setting this option. -This function gets `(id, basedir, importOptions)` arguments and returns full path or promise resolving full path. +This function gets `(id, basedir, importOptions)` arguments and returns full path, array of paths or promise resolving paths. You can use [resolve](https://github.com/substack/node-resolve) for that. #### `skipDuplicates` diff --git a/index.js b/index.js index 35b0b567..4ab8d53d 100755 --- a/index.js +++ b/index.js @@ -255,23 +255,31 @@ function readAtImport( addInputToPath(options) return Promise.resolve().then(function() { - if (options.resolve) { - return options.resolve(parsedAtImport.uri, dir, options) - } - return resolveId(parsedAtImport.uri, dir, options.path) + var resolver = options.resolve ? options.resolve : resolveId + return resolver(parsedAtImport.uri, dir, options) }).then(function(resolved) { - parsedAtImport.file = resolved - return readImportedContent( - result, - parsedAtImport, - assign({}, options), - state, - media, - processor - ) + if (!Array.isArray(resolved)) { + resolved = [ resolved ] + } + return Promise.all(resolved.map(function(file) { + return readImportedContent( + result, + parsedAtImport, + file, + assign({}, options), + state, + media, + processor + ) + })) }).then(function(ignored) { compoundInstance(parsedAtImport) - return ignored + return ignored.reduce(function(ignored, instance) { + if (instance) { + return ignored.concat(instance) + } + return ignored + }, []) }).catch(function(err) { result.warn(err.message, { node: atRule }) }) @@ -288,12 +296,12 @@ function readAtImport( function readImportedContent( result, parsedAtImport, + resolvedFilename, options, state, media, processor ) { - var resolvedFilename = parsedAtImport.file var atRule = parsedAtImport.node if (options.skipDuplicates) { // skip files already imported at the same scope @@ -366,7 +374,15 @@ function readImportedContent( ).then(function(ignored) { return processor.process(newStyles).then(function(newResult) { result.messages = result.messages.concat(newResult.messages) - parsedAtImport.styles = newStyles + var nodes = parsedAtImport.importedNodes + var importedNodes = newStyles.nodes + if (!nodes) { + parsedAtImport.importedNodes = importedNodes + } + else if (importedNodes.length) { + importedNodes[0].raws.before = importedNodes[0].raws.before || "\n" + parsedAtImport.importedNodes = nodes.concat(importedNodes) + } return ignored }) }) @@ -380,17 +396,13 @@ function readImportedContent( * @param {Object} newStyles */ function compoundInstance(instance) { - if ( - !instance.styles || - !instance.styles.nodes || - !instance.styles.nodes.length - ) { + var nodes = instance.importedNodes + + if (!nodes || !nodes.length) { instance.node.remove() return } - var nodes = instance.styles.nodes - // save styles nodes.forEach(function(node) { node.parent = undefined diff --git a/lib/resolve-id.js b/lib/resolve-id.js index 11121d8b..d32f8268 100644 --- a/lib/resolve-id.js +++ b/lib/resolve-id.js @@ -32,7 +32,8 @@ function resolvePromise(id, opts) { }) } -module.exports = function(id, base, paths) { +module.exports = function(id, base, options) { + var paths = options.path var resolveOpts = { basedir: base, moduleDirectory: moduleDirectories, diff --git a/test/fixtures/custom-resolve-array.css b/test/fixtures/custom-resolve-array.css new file mode 100644 index 00000000..f611628b --- /dev/null +++ b/test/fixtures/custom-resolve-array.css @@ -0,0 +1 @@ +@import "any-path"; diff --git a/test/fixtures/custom-resolve-array.expected.css b/test/fixtures/custom-resolve-array.expected.css new file mode 100644 index 00000000..01ac44bf --- /dev/null +++ b/test/fixtures/custom-resolve-array.expected.css @@ -0,0 +1,2 @@ +.custom-array-1 {} +.custom-array-2 {} diff --git a/test/fixtures/imports/custom-array-1.css b/test/fixtures/imports/custom-array-1.css new file mode 100644 index 00000000..bbeb6133 --- /dev/null +++ b/test/fixtures/imports/custom-array-1.css @@ -0,0 +1 @@ +.custom-array-1 {} diff --git a/test/fixtures/imports/custom-array-2.css b/test/fixtures/imports/custom-array-2.css new file mode 100644 index 00000000..7ced963a --- /dev/null +++ b/test/fixtures/imports/custom-array-2.css @@ -0,0 +1 @@ +.custom-array-2 {} diff --git a/test/import.js b/test/import.js index 58179b17..8e33cb28 100644 --- a/test/import.js +++ b/test/import.js @@ -171,3 +171,16 @@ test("should be able to consume modules in the custom-resolve way", t => { resolve: sassResolve, }) }) + +test("should be able to process array of files in the custom-resolve way", t => { + const arrayResolve = () => { + return [ + path.resolve("fixtures/imports/custom-array-1.css"), + path.resolve("fixtures/imports/custom-array-2.css"), + path.resolve("fixtures/imports/custom-array-1.css"), + ] + } + return compareFixtures(t, "custom-resolve-array", { + resolve: arrayResolve, + }) +})