Skip to content

Commit 12efd77

Browse files
committed
Merge pull request #153 from postcss/resolve-test
Add resolve tests
2 parents 84b2a0e + 1f4a659 commit 12efd77

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+154
-116
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions

README.md

Lines changed: 5 additions & 5 deletions

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ function AtImport(options) {
2222
options.path = [ options.path ]
2323
}
2424

25+
if (!Array.isArray(options.path)) {
26+
options.path = []
27+
}
28+
2529
options.path = options.path.map(function(p) {
26-
return path.resolve(p)
30+
return path.resolve(options.root, p)
2731
})
2832

2933
return function(styles, result) {

lib/resolve-id.js

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
1-
var fs = require("fs")
2-
var path = require("path")
31
var resolve = require("resolve")
42

53
var moduleDirectories = [
64
"web_modules",
75
"node_modules",
86
]
97

10-
function isFile(path) {
11-
return new Promise(function(resolve, reject) {
12-
fs.stat(path, function(err, stat) {
13-
if (err) {
14-
if (err.code === "ENOENT") {
15-
return resolve(false)
16-
}
17-
return reject(err)
18-
}
19-
resolve(stat.isFile() || stat.isFIFO())
20-
})
21-
})
22-
}
23-
248
function resolveModule(id, opts) {
259
return new Promise(function(res, rej) {
2610
resolve(id, opts, function(err, path) {
@@ -41,36 +25,30 @@ module.exports = function(id, base, options) {
4125
paths: paths,
4226
extensions: [ ".css" ],
4327
packageFilter: function processPackage(pkg) {
44-
pkg.main = pkg.style || "index.css"
28+
if (pkg.style) {
29+
pkg.main = pkg.style
30+
}
31+
else if (!pkg.main || !/\.css$/.test(pkg.main)) {
32+
pkg.main = "index.css"
33+
}
4534
return pkg
4635
},
4736
}
4837

49-
// local detect
50-
return resolveModule("./" + id, resolveOpts).catch(function() {
51-
// modules and paths detect
38+
return resolveModule("./" + id, resolveOpts)
39+
.catch(function() {
5240
return resolveModule(id, resolveOpts)
53-
}).catch(function() {
54-
// LAST HOPE
55-
return Promise.all(paths.map(function(p) {
56-
return isFile(path.resolve(p, id))
57-
})).then(function(results) {
58-
for (var i = 0; i < results.length; i += 1) {
59-
if (results[i]) {
60-
return path.resolve(paths[i], id)
61-
}
62-
}
63-
64-
if (paths.indexOf(base) === -1) {
65-
paths.unshift(base)
66-
}
67-
68-
throw new Error([
69-
"Failed to find '" + id + "'",
70-
"in [ ",
71-
" " + paths.join(",\n "),
72-
"]",
73-
].join("\n "))
74-
})
41+
})
42+
.catch(function() {
43+
if (paths.indexOf(base) === -1) {
44+
paths.unshift(base)
45+
}
46+
47+
throw new Error([
48+
"Failed to find '" + id + "'",
49+
"in [ ",
50+
" " + paths.join(",\n "),
51+
"]",
52+
].join("\n "))
7553
})
7654
}

test/fixtures/imports/local-module/main.css

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

test/fixtures/imports/local-module/package.json

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+
empty {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main-js {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {}

0 commit comments

Comments
 (0)