Skip to content

Commit

Permalink
Merge pull request #120 from postcss/resolved-array
Browse files Browse the repository at this point in the history
Add array of resolved files support
  • Loading branch information
MoOx committed Dec 29, 2015
2 parents c4384fc + 4244e06 commit 5cb7c79
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
58 changes: 35 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})
Expand All @@ -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
Expand Down Expand Up @@ -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
})
})
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/resolve-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/custom-resolve-array.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "any-path";
2 changes: 2 additions & 0 deletions test/fixtures/custom-resolve-array.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.custom-array-1 {}
.custom-array-2 {}
1 change: 1 addition & 0 deletions test/fixtures/imports/custom-array-1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.custom-array-1 {}
1 change: 1 addition & 0 deletions test/fixtures/imports/custom-array-2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.custom-array-2 {}
13 changes: 13 additions & 0 deletions test/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
})

0 comments on commit 5cb7c79

Please sign in to comment.