diff --git a/CHANGELOG.md b/CHANGELOG.md index 8af948eb..63dbf0d8 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/index.js b/index.js index c6e678ec..bcd69f44 100755 --- a/index.js +++ b/index.js @@ -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 + ) }) }) } diff --git a/test/fixtures/plugins.css b/test/fixtures/plugins.css index 2db3907d..4c5a01e9 100644 --- a/test/fixtures/plugins.css +++ b/test/fixtures/plugins.css @@ -1,2 +1,4 @@ -@import "foo-decl"; -@import "bar-decl"; +@import 'foo/index.css'; +@import 'bar.css'; +@level-1-1 {} +@level-1-2 {} diff --git a/test/fixtures/plugins.expected.css b/test/fixtures/plugins.expected.css index dc1c10dd..d6eb96b0 100644 --- a/test/fixtures/plugins.expected.css +++ b/test/fixtures/plugins.expected.css @@ -1,6 +1,3 @@ -body { - baz: baz; -} -body { - qux: qux; -} +foo-converted {} +@level-1-1 {} +@level-1-2 {} diff --git a/test/plugins.js b/test/plugins.js index d5b8bd8c..65248a91 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -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 => {