Skip to content

Commit 4fb0c2b

Browse files
committed
Merge pull request #143 from postcss/custom-resolve
Refactor resolve option
2 parents 3ddc78c + 3eface9 commit 4fb0c2b

14 files changed

+58
-52
lines changed

index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function AtImport(options) {
1111
root: process.cwd(),
1212
path: [],
1313
skipDuplicates: true,
14+
resolve: resolveId,
1415
load: loadContent,
1516
}, options)
1617

@@ -221,14 +222,12 @@ function resolveImportId(
221222
processor
222223
) {
223224
var atRule = stmt.node
224-
var resolve = options.resolve ? options.resolve : resolveId
225225
var base = atRule.source && atRule.source.input && atRule.source.input.file
226226
? path.dirname(atRule.source.input.file)
227227
: options.root
228228

229-
return Promise.resolve().then(function() {
230-
return resolve(stmt.uri, base, options)
231-
}).then(function(resolved) {
229+
return Promise.resolve(options.resolve(stmt.uri, base, options))
230+
.then(function(resolved) {
232231
if (!Array.isArray(resolved)) {
233232
resolved = [ resolved ]
234233
}
@@ -242,15 +241,17 @@ function resolveImportId(
242241
processor
243242
)
244243
}))
245-
}).then(function(result) {
244+
})
245+
.then(function(result) {
246246
// Merge loaded statements
247247
stmt.children = result.reduce(function(result, statements) {
248248
if (statements) {
249249
result = result.concat(statements)
250250
}
251251
return result
252252
}, [])
253-
}).catch(function(err) {
253+
})
254+
.catch(function(err) {
254255
result.warn(err.message, { node: atRule })
255256
})
256257
}

test/custom-resolve.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import test from "ava"
2+
import compareFixtures from "./lib/compare-fixtures"
3+
import path from "path"
4+
5+
test("should accept file", t => {
6+
return compareFixtures(t, "custom-resolve-file", {
7+
resolve: () => {
8+
return path.resolve("fixtures/imports/custom-resolve-1.css")
9+
},
10+
})
11+
})
12+
13+
test("should accept promised file", t => {
14+
return compareFixtures(t, "custom-resolve-file", {
15+
resolve: () => {
16+
return Promise.resolve(
17+
path.resolve("fixtures/imports/custom-resolve-1.css")
18+
)
19+
},
20+
})
21+
})
22+
23+
test("should accept array of files", t => {
24+
return compareFixtures(t, "custom-resolve-array", {
25+
resolve: () => {
26+
return [
27+
path.resolve("fixtures/imports/custom-resolve-1.css"),
28+
path.resolve("fixtures/imports/custom-resolve-2.css"),
29+
path.resolve("fixtures/imports/custom-resolve-1.css"),
30+
]
31+
},
32+
})
33+
})
34+
35+
test("should accept promised array of files", t => {
36+
return compareFixtures(t, "custom-resolve-array", {
37+
resolve: () => {
38+
return Promise.resolve([
39+
path.resolve("fixtures/imports/custom-resolve-1.css"),
40+
path.resolve("fixtures/imports/custom-resolve-2.css"),
41+
path.resolve("fixtures/imports/custom-resolve-1.css"),
42+
])
43+
},
44+
})
45+
})
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
.custom-array-1 {}
2-
.custom-array-2 {}
1+
custom-resolve-1 {}
2+
custom-resolve-2 {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "any-path";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
custom-resolve-1 {}

test/fixtures/custom-resolve-modules.css

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/fixtures/custom-resolve-modules.expected.css

Lines changed: 0 additions & 3 deletions
This file was deleted.

test/fixtures/imports/custom-array-1.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/imports/custom-array-2.css

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
custom-resolve-1 {}

0 commit comments

Comments
 (0)