Skip to content

Commit

Permalink
Merge pull request #143 from postcss/custom-resolve
Browse files Browse the repository at this point in the history
Refactor resolve option
  • Loading branch information
MoOx committed Jan 9, 2016
2 parents 3ddc78c + 3eface9 commit 4fb0c2b
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 52 deletions.
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function AtImport(options) {
root: process.cwd(),
path: [],
skipDuplicates: true,
resolve: resolveId,
load: loadContent,
}, options)

Expand Down Expand Up @@ -221,14 +222,12 @@ function resolveImportId(
processor
) {
var atRule = stmt.node
var resolve = options.resolve ? options.resolve : resolveId
var base = atRule.source && atRule.source.input && atRule.source.input.file
? path.dirname(atRule.source.input.file)
: options.root

return Promise.resolve().then(function() {
return resolve(stmt.uri, base, options)
}).then(function(resolved) {
return Promise.resolve(options.resolve(stmt.uri, base, options))
.then(function(resolved) {
if (!Array.isArray(resolved)) {
resolved = [ resolved ]
}
Expand All @@ -242,15 +241,17 @@ function resolveImportId(
processor
)
}))
}).then(function(result) {
})
.then(function(result) {
// Merge loaded statements
stmt.children = result.reduce(function(result, statements) {
if (statements) {
result = result.concat(statements)
}
return result
}, [])
}).catch(function(err) {
})
.catch(function(err) {
result.warn(err.message, { node: atRule })
})
}
Expand Down
45 changes: 45 additions & 0 deletions test/custom-resolve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import test from "ava"
import compareFixtures from "./lib/compare-fixtures"
import path from "path"

test("should accept file", t => {
return compareFixtures(t, "custom-resolve-file", {
resolve: () => {
return path.resolve("fixtures/imports/custom-resolve-1.css")
},
})
})

test("should accept promised file", t => {
return compareFixtures(t, "custom-resolve-file", {
resolve: () => {
return Promise.resolve(
path.resolve("fixtures/imports/custom-resolve-1.css")
)
},
})
})

test("should accept array of files", t => {
return compareFixtures(t, "custom-resolve-array", {
resolve: () => {
return [
path.resolve("fixtures/imports/custom-resolve-1.css"),
path.resolve("fixtures/imports/custom-resolve-2.css"),
path.resolve("fixtures/imports/custom-resolve-1.css"),
]
},
})
})

test("should accept promised array of files", t => {
return compareFixtures(t, "custom-resolve-array", {
resolve: () => {
return Promise.resolve([
path.resolve("fixtures/imports/custom-resolve-1.css"),
path.resolve("fixtures/imports/custom-resolve-2.css"),
path.resolve("fixtures/imports/custom-resolve-1.css"),
])
},
})
})
4 changes: 2 additions & 2 deletions test/fixtures/custom-resolve-array.expected.css
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.custom-array-1 {}
.custom-array-2 {}
custom-resolve-1 {}
custom-resolve-2 {}
1 change: 1 addition & 0 deletions test/fixtures/custom-resolve-file.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "any-path";
1 change: 1 addition & 0 deletions test/fixtures/custom-resolve-file.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom-resolve-1 {}
3 changes: 0 additions & 3 deletions test/fixtures/custom-resolve-modules.css

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/custom-resolve-modules.expected.css

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/imports/custom-array-1.css

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/imports/custom-array-2.css

This file was deleted.

1 change: 1 addition & 0 deletions test/fixtures/imports/custom-resolve-1.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom-resolve-1 {}
1 change: 1 addition & 0 deletions test/fixtures/imports/custom-resolve-2.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom-resolve-2 {}
1 change: 0 additions & 1 deletion test/fixtures/imports/sass-module/main.scss

This file was deleted.

2 changes: 0 additions & 2 deletions test/fixtures/imports/sass-module/package.json

This file was deleted.

33 changes: 0 additions & 33 deletions test/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,36 +143,3 @@ test("should work with no styles without throwing an error", t => {
t.is(result.warnings().length, 0)
})
})

test("should be able to consume modules in the custom-resolve way", t => {
const resolve = require("resolve")
const sassResolve = (id, base, opts) => {
return resolve.sync(id, {
basedir: base,
extensions: [ ".scss", ".css" ],
paths: opts.path,
packageFilter: pkg => {
pkg.main = pkg.sass || pkg.style || "index"
return pkg
},
})
}
return compareFixtures(t, "custom-resolve-modules", {
root: ".",
path: "fixtures/imports",
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 4fb0c2b

Please sign in to comment.