-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathindex.js
executable file
·192 lines (157 loc) · 4.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"use strict";
var test = require("tape")
var path = require("path")
var fs = require("fs")
var atImport = require("..")
var postcss = require("postcss")
var fixturesDir = path.join(__dirname, "fixtures")
var importsDir = path.join(fixturesDir, "imports")
function read(name) {
return fs.readFileSync("test/" + name + ".css", "utf8").trim()
}
function compareFixtures(t, name, msg, opts, postcssOpts) {
opts = opts || {path: importsDir}
var actual = postcss()
.use(atImport(opts))
.process(read("fixtures/" + name), postcssOpts)
.css.trim()
var expected = read("fixtures/" + name + ".expected")
// handy thing: checkout actual in the *.actual.css file
fs.writeFile("test/fixtures/" + name + ".actual.css", actual)
t.equal(actual, expected, msg)
}
test("@import", function(t) {
compareFixtures(t, "simple", "should import stylsheets")
compareFixtures(t, "no-duplicate", "should not import a stylsheet twice")
compareFixtures(t, "ignore", "should ignore & adjust external import")
compareFixtures(t, "glob", "should handle a glob pattern", {
root: __dirname,
path: importsDir,
glob: true
})
compareFixtures(t, "glob-alt", "should handle a glob pattern with single quote and/or url(...)", {
path: importsDir,
glob: true
})
compareFixtures(t, "recursive", "should import stylsheets recursively")
compareFixtures(t, "relative", "should import stylsheets relatively")
compareFixtures(t, "empty-and-useless", "should work with empty files")
compareFixtures(t, "transform", "should support transform", {
path: importsDir,
transform: require("css-whitespace")
})
compareFixtures(t, "cwd", "should work without a specified path", {})
compareFixtures(t, "relative-to-source", "should not need `path` option if `source` option has been passed to postcss", null, {from: "test/fixtures/relative-to-source.css"})
compareFixtures(t, "modules", "should be able to consume npm package or local modules", {root: __dirname, path: importsDir})
var base = "@import url(http://)"
t.equal(
postcss()
.use(atImport())
.process(base)
.css.trim(),
base,
"should not fail with only one absolute import"
)
t.equal(
postcss()
.use(atImport())
.process("@import url('http://');\n@import 'test/fixtures/imports/foo.css';")
.css.trim(),
"@import url('http://');\nfoo{}",
"should not fail with absolute and local import"
)
t.end()
})
test("@import error output", function(t) {
var file = importsDir + "/import-missing.css"
t.throws(
function() {
postcss()
.use(atImport())
.process(fs.readFileSync(file), {from: file})
.css.trim()
},
/import-missing.css:2:5: Failed to find 'missing-file.css' from .*\n\s+in \[/gm,
"should output readable trace"
)
t.end()
})
test("@import glob pattern matches no files", function(t) {
var file = importsDir + "/glob-missing.css"
t.equal(
postcss()
.use(atImport({glob: true}))
.process(fs.readFileSync(file), {from: file})
.css.trim(),
"foobar{}",
"should fail silently, skipping the globbed import, if no files found"
)
t.end()
})
test("@import sourcemap", function(t) {
t.equal(
postcss()
.use(atImport())
.process(read("sourcemap/in"), {
from: "./test/sourcemap/in.css",
to: null,
map: {
inline: true,
sourcesContent: true
}
})
.css.trim(),
read("sourcemap/out"),
"should contain a correct sourcemap")
t.end()
})
test("@import callback", function(t) {
postcss()
.use(atImport({
path: importsDir,
onImport: function onImport(files) {
t.deepEqual(
files,
[
path.join(__dirname, "fixtures", "recursive.css"),
path.join(__dirname, "fixtures", "imports", "foo-recursive.css"),
path.join(__dirname, "fixtures", "imports", "bar.css")
],
"should have a callback that returns an object containing imported files")
t.end()
}
}))
.process(read("fixtures/recursive"), {
from: "./test/fixtures/recursive.css"
})
.css.trim()
})
test("import relative files using path option only", function(t) {
t.equal(
postcss()
.use(atImport({path: "test/fixtures/imports/relative"}))
.process(read("fixtures/imports/relative/import"))
.css.trim(),
read("fixtures/imports/bar")
)
t.end()
})
test("inlined @import should keep PostCSS AST references clean", function(t) {
var root = postcss()
.use(atImport())
.process("@import 'test/fixtures/imports/foo.css';\nbar{}")
.root
root.nodes.forEach(function(node) {
t.equal(root, node.parent)
})
t.end()
})
test("works with no styles at all", function(t) {
t.doesNotThrow(function() {
postcss()
.use(atImport())
.process("")
.css.trim()
}, "should works with nothing without throwing an error")
t.end()
})