Skip to content

Commit 5cb7c79

Browse files
committed
Merge pull request #120 from postcss/resolved-array
Add array of resolved files support
2 parents c4384fc + 4244e06 commit 5cb7c79

9 files changed

+58
-25
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- Changed: custom resolve has more responsibility for paths resolving.
66
See [resolve option](https://github.com/postcss/postcss-import#resolve) for more information about this change
77
([#116](https://github.com/postcss/postcss-import/pull/116))
8+
- Added: custom resolve function can return array of paths
9+
([#120](https://github.com/postcss/postcss-import/pull/120))
810

911
# 7.1.3 - 2015-11-05
1012

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ Type: `Function`
145145
Default: `null`
146146

147147
You can overwrite the default path resolving way by setting this option.
148-
This function gets `(id, basedir, importOptions)` arguments and returns full path or promise resolving full path.
148+
This function gets `(id, basedir, importOptions)` arguments and returns full path, array of paths or promise resolving paths.
149149
You can use [resolve](https://github.com/substack/node-resolve) for that.
150150

151151
#### `skipDuplicates`

index.js

+35-23
Original file line numberDiff line numberDiff line change
@@ -255,23 +255,31 @@ function readAtImport(
255255
addInputToPath(options)
256256

257257
return Promise.resolve().then(function() {
258-
if (options.resolve) {
259-
return options.resolve(parsedAtImport.uri, dir, options)
260-
}
261-
return resolveId(parsedAtImport.uri, dir, options.path)
258+
var resolver = options.resolve ? options.resolve : resolveId
259+
return resolver(parsedAtImport.uri, dir, options)
262260
}).then(function(resolved) {
263-
parsedAtImport.file = resolved
264-
return readImportedContent(
265-
result,
266-
parsedAtImport,
267-
assign({}, options),
268-
state,
269-
media,
270-
processor
271-
)
261+
if (!Array.isArray(resolved)) {
262+
resolved = [ resolved ]
263+
}
264+
return Promise.all(resolved.map(function(file) {
265+
return readImportedContent(
266+
result,
267+
parsedAtImport,
268+
file,
269+
assign({}, options),
270+
state,
271+
media,
272+
processor
273+
)
274+
}))
272275
}).then(function(ignored) {
273276
compoundInstance(parsedAtImport)
274-
return ignored
277+
return ignored.reduce(function(ignored, instance) {
278+
if (instance) {
279+
return ignored.concat(instance)
280+
}
281+
return ignored
282+
}, [])
275283
}).catch(function(err) {
276284
result.warn(err.message, { node: atRule })
277285
})
@@ -288,12 +296,12 @@ function readAtImport(
288296
function readImportedContent(
289297
result,
290298
parsedAtImport,
299+
resolvedFilename,
291300
options,
292301
state,
293302
media,
294303
processor
295304
) {
296-
var resolvedFilename = parsedAtImport.file
297305
var atRule = parsedAtImport.node
298306
if (options.skipDuplicates) {
299307
// skip files already imported at the same scope
@@ -366,7 +374,15 @@ function readImportedContent(
366374
).then(function(ignored) {
367375
return processor.process(newStyles).then(function(newResult) {
368376
result.messages = result.messages.concat(newResult.messages)
369-
parsedAtImport.styles = newStyles
377+
var nodes = parsedAtImport.importedNodes
378+
var importedNodes = newStyles.nodes
379+
if (!nodes) {
380+
parsedAtImport.importedNodes = importedNodes
381+
}
382+
else if (importedNodes.length) {
383+
importedNodes[0].raws.before = importedNodes[0].raws.before || "\n"
384+
parsedAtImport.importedNodes = nodes.concat(importedNodes)
385+
}
370386
return ignored
371387
})
372388
})
@@ -380,17 +396,13 @@ function readImportedContent(
380396
* @param {Object} newStyles
381397
*/
382398
function compoundInstance(instance) {
383-
if (
384-
!instance.styles ||
385-
!instance.styles.nodes ||
386-
!instance.styles.nodes.length
387-
) {
399+
var nodes = instance.importedNodes
400+
401+
if (!nodes || !nodes.length) {
388402
instance.node.remove()
389403
return
390404
}
391405

392-
var nodes = instance.styles.nodes
393-
394406
// save styles
395407
nodes.forEach(function(node) {
396408
node.parent = undefined

lib/resolve-id.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ function resolvePromise(id, opts) {
3232
})
3333
}
3434

35-
module.exports = function(id, base, paths) {
35+
module.exports = function(id, base, options) {
36+
var paths = options.path
3637
var resolveOpts = {
3738
basedir: base,
3839
moduleDirectory: moduleDirectories,
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "any-path";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.custom-array-1 {}
2+
.custom-array-2 {}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.custom-array-1 {}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.custom-array-2 {}

test/import.js

+13
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,16 @@ test("should be able to consume modules in the custom-resolve way", t => {
171171
resolve: sassResolve,
172172
})
173173
})
174+
175+
test("should be able to process array of files in the custom-resolve way", t => {
176+
const arrayResolve = () => {
177+
return [
178+
path.resolve("fixtures/imports/custom-array-1.css"),
179+
path.resolve("fixtures/imports/custom-array-2.css"),
180+
path.resolve("fixtures/imports/custom-array-1.css"),
181+
]
182+
}
183+
return compareFixtures(t, "custom-resolve-array", {
184+
resolve: arrayResolve,
185+
})
186+
})

0 commit comments

Comments
 (0)