Skip to content

Commit 0137426

Browse files
committed
Merge pull request #157 from postcss/plugins
Add more sensitive plugins tests
2 parents 12efd77 + a3a5877 commit 0137426

File tree

5 files changed

+57
-52
lines changed

5 files changed

+57
-52
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ postcssImport({
4545
([#147](https://github.com/postcss/postcss-import/pull/147))
4646
- Added: detect css extension in package.json `main` field
4747
([153](https://github.com/postcss/postcss-import/pull/153))
48+
- Changed: `options.plugins` are applied to unprocessed ast before imports detecting
49+
([157](https://github.com/postcss/postcss-import/pull/157))
4850

4951
# 7.1.3 - 2015-11-05
5052

index.js

+25-29
Original file line numberDiff line numberDiff line change
@@ -310,41 +310,37 @@ function loadImportContent(
310310
return
311311
}
312312

313-
var newStyles = postcss().process(content, {
313+
return processor.process(content, {
314314
from: filename,
315315
syntax: result.opts.syntax,
316316
parser: result.opts.parser,
317-
}).root
318-
319-
if (options.skipDuplicates) {
320-
var hasImport = newStyles.some(function(child) {
321-
return child.type === "atrule" && child.name === "import"
322-
})
323-
if (!hasImport) {
324-
// save hash files to skip them next time
325-
if (!state.hashFiles[content]) {
326-
state.hashFiles[content] = {}
317+
})
318+
.then(function(importedResult) {
319+
var styles = importedResult.root
320+
result.messages = result.messages.concat(importedResult.messages)
321+
322+
if (options.skipDuplicates) {
323+
var hasImport = styles.some(function(child) {
324+
return child.type === "atrule" && child.name === "import"
325+
})
326+
if (!hasImport) {
327+
// save hash files to skip them next time
328+
if (!state.hashFiles[content]) {
329+
state.hashFiles[content] = {}
330+
}
331+
state.hashFiles[content][media] = true
327332
}
328-
state.hashFiles[content][media] = true
329333
}
330-
}
331334

332-
// recursion: import @import from imported file
333-
return parseStyles(
334-
result,
335-
newStyles,
336-
options,
337-
state,
338-
media,
339-
processor
340-
)
341-
.then(function(statements) {
342-
return processor.process(newStyles)
343-
.then(function(newResult) {
344-
result.messages = result.messages.concat(newResult.messages)
345-
346-
return statements
347-
})
335+
// recursion: import @import from imported file
336+
return parseStyles(
337+
result,
338+
styles,
339+
options,
340+
state,
341+
media,
342+
processor
343+
)
348344
})
349345
})
350346
}

test/fixtures/plugins.css

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
@import "foo-decl";
2-
@import "bar-decl";
1+
@import 'foo/index.css';
2+
@import 'bar.css';
3+
@level-1-1 {}
4+
@level-1-2 {}

test/fixtures/plugins.expected.css

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
body {
2-
baz: baz;
3-
}
4-
body {
5-
qux: qux;
6-
}
1+
foo-converted {}
2+
@level-1-1 {}
3+
@level-1-2 {}

test/plugins.js

+23-15
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,33 @@ import scss from "postcss-scss"
44
import atImport from ".."
55
import compareFixtures from "./lib/compare-fixtures"
66

7-
test("should apply plugins", t => {
7+
test("should apply plugins to root", t => {
8+
const atRules = []
9+
const rules = []
810
return compareFixtures(t, "plugins", {
911
plugins: [
10-
postcss.plugin("postcss-no-foo", () => {
11-
return css => {
12-
css.walkDecls("foo", decl => {
13-
decl.remove()
14-
})
15-
}
16-
}),
17-
postcss.plugin("postcss-no-bar", () => {
18-
return css => {
19-
css.walkDecls("bar", decl => {
20-
decl.remove()
21-
})
22-
}
23-
}),
12+
css => {
13+
css.walk(node => {
14+
if (node.type === "rule") {
15+
rules.push(node.selector)
16+
if (node.selector === "bar") {
17+
node.remove()
18+
}
19+
else {
20+
node.selector += "-converted"
21+
}
22+
}
23+
if (node.type === "atrule") {
24+
atRules.push(node.name)
25+
}
26+
})
27+
},
2428
],
2529
})
30+
.then(() => {
31+
t.same(atRules, [ "import" ])
32+
t.same(rules, [ "bar", "foo" ])
33+
})
2634
})
2735

2836
test("should error when value is not an array", t => {

0 commit comments

Comments
 (0)