From ba4f4b44d263ea2a2a21ea81a0cb0b324e415da8 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Tue, 26 Jan 2016 15:56:48 +0300 Subject: [PATCH 1/3] Add more sensitive plugins tests --- test/fixtures/plugins-imported.css | 1 + test/fixtures/plugins-imported.expected.css | 3 ++ test/fixtures/plugins-root.css | 3 ++ test/fixtures/plugins-root.expected.css | 3 ++ test/fixtures/plugins.css | 2 - test/fixtures/plugins.expected.css | 6 --- test/plugins.js | 42 +++++++++++++-------- 7 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 test/fixtures/plugins-imported.css create mode 100644 test/fixtures/plugins-imported.expected.css create mode 100644 test/fixtures/plugins-root.css create mode 100644 test/fixtures/plugins-root.expected.css delete mode 100644 test/fixtures/plugins.css delete mode 100644 test/fixtures/plugins.expected.css diff --git a/test/fixtures/plugins-imported.css b/test/fixtures/plugins-imported.css new file mode 100644 index 00000000..ddd2bfb5 --- /dev/null +++ b/test/fixtures/plugins-imported.css @@ -0,0 +1 @@ +@import 'plugins-root'; diff --git a/test/fixtures/plugins-imported.expected.css b/test/fixtures/plugins-imported.expected.css new file mode 100644 index 00000000..64114ce8 --- /dev/null +++ b/test/fixtures/plugins-imported.expected.css @@ -0,0 +1,3 @@ +foo-converted {} +bar-converted {} +baz-converted {} diff --git a/test/fixtures/plugins-root.css b/test/fixtures/plugins-root.css new file mode 100644 index 00000000..d85093b7 --- /dev/null +++ b/test/fixtures/plugins-root.css @@ -0,0 +1,3 @@ +foo {} +bar {} +baz {} diff --git a/test/fixtures/plugins-root.expected.css b/test/fixtures/plugins-root.expected.css new file mode 100644 index 00000000..64114ce8 --- /dev/null +++ b/test/fixtures/plugins-root.expected.css @@ -0,0 +1,3 @@ +foo-converted {} +bar-converted {} +baz-converted {} diff --git a/test/fixtures/plugins.css b/test/fixtures/plugins.css deleted file mode 100644 index 2db3907d..00000000 --- a/test/fixtures/plugins.css +++ /dev/null @@ -1,2 +0,0 @@ -@import "foo-decl"; -@import "bar-decl"; diff --git a/test/fixtures/plugins.expected.css b/test/fixtures/plugins.expected.css deleted file mode 100644 index dc1c10dd..00000000 --- a/test/fixtures/plugins.expected.css +++ /dev/null @@ -1,6 +0,0 @@ -body { - baz: baz; -} -body { - qux: qux; -} diff --git a/test/plugins.js b/test/plugins.js index d5b8bd8c..f5e79ab9 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -4,27 +4,37 @@ import scss from "postcss-scss" import atImport from ".." import compareFixtures from "./lib/compare-fixtures" -test("should apply plugins", t => { - return compareFixtures(t, "plugins", { +test("should apply plugins to root", t => { + return compareFixtures(t, "plugins-root", { 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.walkRules(rule => { + rule.selector += "-converted" + }) + }, ], }) }) +test("should apply plugins to imported files", t => { + return compareFixtures(t, "plugins-imported", { + plugins: [ + css => { + css.walkRules(rule => { + if (rule.selector === "foo") { + rule.remove() + } + else { + rule.selector += "-converted" + } + }) + }, + ], + }, { + from: "fixtures/plugins-imported.css", + }) +}) + test("should error when value is not an array", t => { return postcss() .use(atImport({ From 5397ffb9c828b0cb3828a8395d1dba4a8ffc21e2 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Wed, 27 Jan 2016 11:08:43 +0300 Subject: [PATCH 2/3] New plugins behaviour --- index.js | 54 ++++++++++----------- test/fixtures/plugins-imported.css | 1 - test/fixtures/plugins-imported.expected.css | 3 -- test/fixtures/plugins-root.css | 3 -- test/fixtures/plugins-root.expected.css | 3 -- test/fixtures/plugins.css | 4 ++ test/fixtures/plugins.expected.css | 3 ++ test/plugins.js | 38 +++++++-------- 8 files changed, 50 insertions(+), 59 deletions(-) delete mode 100644 test/fixtures/plugins-imported.css delete mode 100644 test/fixtures/plugins-imported.expected.css delete mode 100644 test/fixtures/plugins-root.css delete mode 100644 test/fixtures/plugins-root.expected.css create mode 100644 test/fixtures/plugins.css create mode 100644 test/fixtures/plugins.expected.css 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-imported.css b/test/fixtures/plugins-imported.css deleted file mode 100644 index ddd2bfb5..00000000 --- a/test/fixtures/plugins-imported.css +++ /dev/null @@ -1 +0,0 @@ -@import 'plugins-root'; diff --git a/test/fixtures/plugins-imported.expected.css b/test/fixtures/plugins-imported.expected.css deleted file mode 100644 index 64114ce8..00000000 --- a/test/fixtures/plugins-imported.expected.css +++ /dev/null @@ -1,3 +0,0 @@ -foo-converted {} -bar-converted {} -baz-converted {} diff --git a/test/fixtures/plugins-root.css b/test/fixtures/plugins-root.css deleted file mode 100644 index d85093b7..00000000 --- a/test/fixtures/plugins-root.css +++ /dev/null @@ -1,3 +0,0 @@ -foo {} -bar {} -baz {} diff --git a/test/fixtures/plugins-root.expected.css b/test/fixtures/plugins-root.expected.css deleted file mode 100644 index 64114ce8..00000000 --- a/test/fixtures/plugins-root.expected.css +++ /dev/null @@ -1,3 +0,0 @@ -foo-converted {} -bar-converted {} -baz-converted {} diff --git a/test/fixtures/plugins.css b/test/fixtures/plugins.css new file mode 100644 index 00000000..4c5a01e9 --- /dev/null +++ b/test/fixtures/plugins.css @@ -0,0 +1,4 @@ +@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 new file mode 100644 index 00000000..d6eb96b0 --- /dev/null +++ b/test/fixtures/plugins.expected.css @@ -0,0 +1,3 @@ +foo-converted {} +@level-1-1 {} +@level-1-2 {} diff --git a/test/plugins.js b/test/plugins.js index f5e79ab9..65248a91 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -5,33 +5,31 @@ import atImport from ".." import compareFixtures from "./lib/compare-fixtures" test("should apply plugins to root", t => { - return compareFixtures(t, "plugins-root", { + const atRules = [] + const rules = [] + return compareFixtures(t, "plugins", { plugins: [ css => { - css.walkRules(rule => { - rule.selector += "-converted" - }) - }, - ], - }) -}) - -test("should apply plugins to imported files", t => { - return compareFixtures(t, "plugins-imported", { - plugins: [ - css => { - css.walkRules(rule => { - if (rule.selector === "foo") { - rule.remove() + css.walk(node => { + if (node.type === "rule") { + rules.push(node.selector) + if (node.selector === "bar") { + node.remove() + } + else { + node.selector += "-converted" + } } - else { - rule.selector += "-converted" + if (node.type === "atrule") { + atRules.push(node.name) } }) }, ], - }, { - from: "fixtures/plugins-imported.css", + }) + .then(() => { + t.same(atRules, [ "import" ]) + t.same(rules, [ "bar", "foo" ]) }) }) From a3a5877618cfda2ab8a26e982b13136988d2d361 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Wed, 27 Jan 2016 11:40:41 +0300 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) 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