diff --git a/README.md b/README.md index 0d553668..080fce18 100755 --- a/README.md +++ b/README.md @@ -163,10 +163,12 @@ files). 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, array of paths or promise resolving paths. -You can use [resolve](https://github.com/substack/node-resolve) for that. +You can provide a custom path resolver with this option. This function gets +`(id, basedir, importOptions)` arguments and should return a path, an array of +paths or a promise resolving to the path(s). If you do not return an absolute +path, your path will be resolved to an absolute path using the default +resolver. +You can use [resolve](https://github.com/substack/node-resolve) for this. #### `load` diff --git a/index.js b/index.js index 0a484905..589228f0 100755 --- a/index.js +++ b/index.js @@ -227,11 +227,18 @@ function resolveImportId( : options.root return Promise.resolve(options.resolve(stmt.uri, base, options)) - .then(function(resolved) { - if (!Array.isArray(resolved)) { - resolved = [ resolved ] + .then(function(paths) { + if (!Array.isArray(paths)) { + paths = [ paths ] } + return Promise.all(paths.map(function(file) { + // Ensure that each path is absolute: + if (!path.isAbsolute(file)) return resolveId(file, base, options) + return file + })) + }) + .then(function(resolved) { // Add dependency messages: resolved.forEach(function(file) { result.messages.push({ diff --git a/test/custom-resolve.js b/test/custom-resolve.js index 974bde58..b4453f67 100644 --- a/test/custom-resolve.js +++ b/test/custom-resolve.js @@ -1,5 +1,7 @@ import test from "ava" import compareFixtures from "./helpers/compare-fixtures" +import postcss from "postcss" +import atImport from ".." import path from "path" test.serial("should accept file", t => { @@ -43,3 +45,23 @@ test.serial("should accept promised array of files", t => { }, }) }) + +test( + "should apply default resolver when custom doesn't return an absolute path", + function(t) { + return postcss() + .use(atImport({ + resolve: path => { + return path.replace("foo", "imports/bar") + }, + load: p => { + t.is(p, path.resolve("fixtures/imports", "bar.css")) + return "/* comment */" + }, + })) + .process(`@import "foo.css";`, { from: "fixtures/custom-resolve-file" }) + .then(result => { + t.is(result.css, "/* comment */") + }) + } +)