Skip to content

Commit

Permalink
Merge pull request #157 from postcss/plugins
Browse files Browse the repository at this point in the history
Add more sensitive plugins tests
  • Loading branch information
MoOx committed Jan 27, 2016
2 parents 12efd77 + a3a5877 commit 0137426
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 52 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ postcssImport({
([#147](https://github.com/postcss/postcss-import/pull/147))
- Added: detect css extension in package.json `main` field
([153](https://github.com/postcss/postcss-import/pull/153))
- Changed: `options.plugins` are applied to unprocessed ast before imports detecting
([157](https://github.com/postcss/postcss-import/pull/157))

# 7.1.3 - 2015-11-05

Expand Down
54 changes: 25 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,41 +310,37 @@ function loadImportContent(
return
}

var newStyles = postcss().process(content, {
return processor.process(content, {
from: filename,
syntax: result.opts.syntax,
parser: result.opts.parser,
}).root

if (options.skipDuplicates) {
var hasImport = newStyles.some(function(child) {
return child.type === "atrule" && child.name === "import"
})
if (!hasImport) {
// save hash files to skip them next time
if (!state.hashFiles[content]) {
state.hashFiles[content] = {}
})
.then(function(importedResult) {
var styles = importedResult.root
result.messages = result.messages.concat(importedResult.messages)

if (options.skipDuplicates) {
var hasImport = styles.some(function(child) {
return child.type === "atrule" && child.name === "import"
})
if (!hasImport) {
// save hash files to skip them next time
if (!state.hashFiles[content]) {
state.hashFiles[content] = {}
}
state.hashFiles[content][media] = true
}
state.hashFiles[content][media] = true
}
}

// recursion: import @import from imported file
return parseStyles(
result,
newStyles,
options,
state,
media,
processor
)
.then(function(statements) {
return processor.process(newStyles)
.then(function(newResult) {
result.messages = result.messages.concat(newResult.messages)

return statements
})
// recursion: import @import from imported file
return parseStyles(
result,
styles,
options,
state,
media,
processor
)
})
})
}
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/plugins.css
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
@import "foo-decl";
@import "bar-decl";
@import 'foo/index.css';
@import 'bar.css';
@level-1-1 {}
@level-1-2 {}
9 changes: 3 additions & 6 deletions test/fixtures/plugins.expected.css
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
body {
baz: baz;
}
body {
qux: qux;
}
foo-converted {}
@level-1-1 {}
@level-1-2 {}
38 changes: 23 additions & 15 deletions test/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,33 @@ import scss from "postcss-scss"
import atImport from ".."
import compareFixtures from "./lib/compare-fixtures"

test("should apply plugins", t => {
test("should apply plugins to root", t => {
const atRules = []
const rules = []
return compareFixtures(t, "plugins", {
plugins: [
postcss.plugin("postcss-no-foo", () => {
return css => {
css.walkDecls("foo", decl => {
decl.remove()
})
}
}),
postcss.plugin("postcss-no-bar", () => {
return css => {
css.walkDecls("bar", decl => {
decl.remove()
})
}
}),
css => {
css.walk(node => {
if (node.type === "rule") {
rules.push(node.selector)
if (node.selector === "bar") {
node.remove()
}
else {
node.selector += "-converted"
}
}
if (node.type === "atrule") {
atRules.push(node.name)
}
})
},
],
})
.then(() => {
t.same(atRules, [ "import" ])
t.same(rules, [ "bar", "foo" ])
})
})

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

0 comments on commit 0137426

Please sign in to comment.