Skip to content

Commit 717b07a

Browse files
committed
Add resolve tests
1 parent 84b2a0e commit 717b07a

39 files changed

+142
-61
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,17 @@ Checkout [tests](test) for more examples.
9292
#### `root`
9393

9494
Type: `String`
95-
Default: `process.cwd()`
95+
Default: `process.cwd()` or _dirname of [the postcss `from`](https://github.com/postcss/postcss#node-source)_
9696

97-
Define the root where to resolve path (eg: place where `node_modules` are). Should not be used that much.
97+
Define the root where to resolve path (eg: place where `node_modules` are). Should not be used that much.
98+
_Note: nested `@import` will additionally benefit of the relative dirname of imported files._
9899

99100
#### `path`
100101

101102
Type: `String|Array`
102-
Default: `process.cwd()` or _dirname of [the postcss `from`](https://github.com/postcss/postcss#node-source)_
103+
Default: `[]`
103104

104-
A string or an array of paths in where to look for files.
105-
_Note: nested `@import` will additionally benefit of the relative dirname of imported files._
105+
A string or an array of paths in where to look for files.
106106

107107
#### `transform`
108108

index.js

+5-1
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

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ module.exports = function(id, base, options) {
4141
paths: paths,
4242
extensions: [ ".css" ],
4343
packageFilter: function processPackage(pkg) {
44-
pkg.main = pkg.style || "index.css"
44+
if (pkg.style) {
45+
pkg.main = pkg.style
46+
}
47+
else if (!pkg.main || !/\.css$/.test(pkg.main)) {
48+
pkg.main = "index.css"
49+
}
4550
return pkg
4651
},
4752
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@import "fake";
2+
@import "auto";
3+
@import "nest";
4+
@import "by-hand/style.css";
5+
@import "use-dep";
6+
@import "use-dep-too";
7+
@import "use-dep" screen;
8+
9+
@import "web-fake";
10+
@import "web-auto";
11+
@import "web-nest";
12+
@import "web-by-hand/style.css";
13+
@import "web-use-dep";
14+
@import "web-use-dep-too";
15+
@import "web-use-dep" screen;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
empty {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main-js {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"main": "main.js"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
style-mained {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"main": "main.css",
3+
"style": "style.css"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main-styled {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"main": "main.css"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
simple {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"style": "style.css"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
style {}

test/fixtures/imports/relative/baz.css

-5
This file was deleted.

test/fixtures/imports/relative/import.css

-1
This file was deleted.

test/fixtures/imports/relative/qux.css

-3
This file was deleted.

test/fixtures/relative-to-source.css

-3
This file was deleted.

test/fixtures/relative-to-source.expected.css

-3
This file was deleted.

test/fixtures/relative.css

-3
This file was deleted.

test/fixtures/relative.expected.css

-13
This file was deleted.
File renamed without changes.

test/fixtures/resolve-from.css

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import "imports/foo.css";
2+
@import "imports/foo-recursive.css";
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo{}
2+
bar{}
3+
foo.recursive{}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@import "fixtures/imports/modules/simple";
2+
@import "fixtures/imports/modules/empty";
3+
@import "fixtures/imports/modules/style";
4+
@import "fixtures/imports/modules/main";
5+
@import "fixtures/imports/modules/main-js";
6+
@import "fixtures/imports/modules/main-style";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
simple {}
2+
empty {}
3+
style {}
4+
main {}
5+
main-js {}
6+
main-styled {}

test/fixtures/resolve-path-cwd.css

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import "foo.css";
2+
@import "foo-recursive.css";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo{}
2+
bar{}
3+
foo.recursive{}

test/fixtures/resolve-path-root.css

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import "foo.css";
2+
@import "foo-recursive.css";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo{}
2+
bar{}
3+
foo.recursive{}

test/fixtures/resolve-root.css

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import "imports/foo.css";
2+
@import "imports/foo-recursive.css";
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
foo{}
2+
bar{}
3+
foo.recursive{}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@import "fake";
2+
@import "auto";
3+
@import "nest";
4+
@import "by-hand/style.css";
5+
@import "use-dep";
6+
@import "use-dep-too";
7+
@import "use-dep" screen;
8+
9+
@import "web-fake";
10+
@import "web-auto";
11+
@import "web-nest";
12+
@import "web-by-hand/style.css";
13+
@import "web-use-dep";
14+
@import "web-use-dep-too";
15+
@import "web-use-dep" screen;

test/import.js

-23
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ test("should ignore & adjust external import", t => {
2727
return compareFixtures(t, "ignore")
2828
})
2929

30-
test("should import stylsheets relatively", t => {
31-
return compareFixtures(t, "relative")
32-
})
33-
34-
test("should work without a specified path", t => {
35-
return compareFixtures(t, "cwd")
36-
})
37-
38-
test("should not need `path` option if `source` option has been passed", t => {
39-
return compareFixtures(t, "relative-to-source", null, {
40-
from: "fixtures/relative-to-source.css",
41-
})
42-
})
43-
4430
test("should be able to consume npm package or local modules", t => {
4531
return compareFixtures(t, "modules", {
4632
root: ".",
@@ -100,15 +86,6 @@ test("should contain a correct sourcemap", t => {
10086
})
10187
})
10288

103-
test("import relative files using path option only", t => {
104-
return postcss()
105-
.use(atImport({ path: "fixtures/imports/relative" }))
106-
.process(readFileSync("fixtures/imports/relative/import.css"))
107-
.then(result => {
108-
t.is(result.css, readFileSync("fixtures/imports/bar.css", "utf-8"))
109-
})
110-
})
111-
11289
test("inlined @import should keep PostCSS AST references clean", t => {
11390
return postcss()
11491
.use(atImport())

test/resolve.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import test from "ava"
2+
import compareFixtures from "./lib/compare-fixtures"
3+
4+
test("should resolve relative to cwd", t => {
5+
return compareFixtures(t, "resolve-cwd", {
6+
path: null,
7+
})
8+
})
9+
10+
test(`should resolve relative to 'root' option`, t => {
11+
return compareFixtures(t, "resolve-root", {
12+
root: "fixtures",
13+
path: null,
14+
})
15+
})
16+
17+
test(`should resolve relative to postcss 'from' option`, t => {
18+
return compareFixtures(t, "resolve-from", {
19+
path: null,
20+
}, {
21+
from: "fixtures/file.css",
22+
})
23+
})
24+
25+
test(`should resolve relative to 'path' which resolved with cwd`, t => {
26+
return compareFixtures(t, "resolve-path-cwd", {
27+
path: "fixtures/imports",
28+
})
29+
})
30+
31+
test(`should resolve relative to 'path' which resolved with 'root'`, t => {
32+
return compareFixtures(t, "resolve-path-root", {
33+
root: "fixtures",
34+
path: "imports",
35+
})
36+
})
37+
38+
test("should resolve local modules", t => {
39+
return compareFixtures(t, "resolve-local-modules", {
40+
path: null,
41+
})
42+
})

0 commit comments

Comments
 (0)