Skip to content

Commit a15e402

Browse files
authored
Change resolve behavior (#249)
Use default resolver when custom resolve does not return an absolute path.
1 parent 3352a37 commit a15e402

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

README.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,12 @@ files).
163163
Type: `Function`
164164
Default: `null`
165165

166-
You can overwrite the default path resolving way by setting this option.
167-
This function gets `(id, basedir, importOptions)` arguments and returns full
168-
path, array of paths or promise resolving paths.
169-
You can use [resolve](https://github.com/substack/node-resolve) for that.
166+
You can provide a custom path resolver with this option. This function gets
167+
`(id, basedir, importOptions)` arguments and should return a path, an array of
168+
paths or a promise resolving to the path(s). If you do not return an absolute
169+
path, your path will be resolved to an absolute path using the default
170+
resolver.
171+
You can use [resolve](https://github.com/substack/node-resolve) for this.
170172

171173
#### `load`
172174

index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,18 @@ function resolveImportId(
227227
: options.root
228228

229229
return Promise.resolve(options.resolve(stmt.uri, base, options))
230-
.then(function(resolved) {
231-
if (!Array.isArray(resolved)) {
232-
resolved = [ resolved ]
230+
.then(function(paths) {
231+
if (!Array.isArray(paths)) {
232+
paths = [ paths ]
233233
}
234234

235+
return Promise.all(paths.map(function(file) {
236+
// Ensure that each path is absolute:
237+
if (!path.isAbsolute(file)) return resolveId(file, base, options)
238+
return file
239+
}))
240+
})
241+
.then(function(resolved) {
235242
// Add dependency messages:
236243
resolved.forEach(function(file) {
237244
result.messages.push({

test/custom-resolve.js

+22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import test from "ava"
22
import compareFixtures from "./helpers/compare-fixtures"
3+
import postcss from "postcss"
4+
import atImport from ".."
35
import path from "path"
46

57
test.serial("should accept file", t => {
@@ -43,3 +45,23 @@ test.serial("should accept promised array of files", t => {
4345
},
4446
})
4547
})
48+
49+
test(
50+
"should apply default resolver when custom doesn't return an absolute path",
51+
function(t) {
52+
return postcss()
53+
.use(atImport({
54+
resolve: path => {
55+
return path.replace("foo", "imports/bar")
56+
},
57+
load: p => {
58+
t.is(p, path.resolve("fixtures/imports", "bar.css"))
59+
return "/* comment */"
60+
},
61+
}))
62+
.process(`@import "foo.css";`, { from: "fixtures/custom-resolve-file" })
63+
.then(result => {
64+
t.is(result.css, "/* comment */")
65+
})
66+
}
67+
)

0 commit comments

Comments
 (0)