From 5ebd132949a79c9f07e8f7c7152fcb0a1d680c2b Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 9 Jan 2016 23:24:21 +0300 Subject: [PATCH 1/4] Extended transform support --- index.js | 11 +++++--- test/fixtures/transform-content.css | 1 + test/fixtures/transform-content.expected.css | 1 + test/fixtures/transform-undefined.css | 1 + .../fixtures/transform-undefined.expected.css | 1 + test/transform.js | 26 +++++++++++++++++++ 6 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/transform-content.css create mode 100644 test/fixtures/transform-content.expected.css create mode 100644 test/fixtures/transform-undefined.css create mode 100644 test/fixtures/transform-undefined.expected.css create mode 100644 test/transform.js diff --git a/index.js b/index.js index d77495bf..48efd494 100755 --- a/index.js +++ b/index.js @@ -284,10 +284,15 @@ function loadImportContent( return Promise.resolve(options.load(filename, options)) .then(function(content) { - if (typeof options.transform === "function") { - content = options.transform(content, filename) + if (typeof options.transform !== "function") { + return content } - + return Promise.resolve(options.transform(content, filename, options)) + .then(function(transformed) { + return transformed == null ? content : transformed + }) + }) + .then(function(content) { if (content.trim() === "") { result.warn(filename + " is empty", { node: atRule }) return diff --git a/test/fixtures/transform-content.css b/test/fixtures/transform-content.css new file mode 100644 index 00000000..0a2e522a --- /dev/null +++ b/test/fixtures/transform-content.css @@ -0,0 +1 @@ +@import "foo" diff --git a/test/fixtures/transform-content.expected.css b/test/fixtures/transform-content.expected.css new file mode 100644 index 00000000..44fe46f6 --- /dev/null +++ b/test/fixtures/transform-content.expected.css @@ -0,0 +1 @@ +transformed-content {} diff --git a/test/fixtures/transform-undefined.css b/test/fixtures/transform-undefined.css new file mode 100644 index 00000000..0a2e522a --- /dev/null +++ b/test/fixtures/transform-undefined.css @@ -0,0 +1 @@ +@import "foo" diff --git a/test/fixtures/transform-undefined.expected.css b/test/fixtures/transform-undefined.expected.css new file mode 100644 index 00000000..d2d19a3e --- /dev/null +++ b/test/fixtures/transform-undefined.expected.css @@ -0,0 +1 @@ +foo{} diff --git a/test/transform.js b/test/transform.js new file mode 100644 index 00000000..988b303f --- /dev/null +++ b/test/transform.js @@ -0,0 +1,26 @@ +import test from "ava" +import compareFixtures from "./lib/compare-fixtures" + +test.serial("should accept content", t => { + return compareFixtures(t, "transform-content", { + transform: () => "transformed-content {}", + }) +}) + +test.serial("should accept promised content", t => { + return compareFixtures(t, "transform-content", { + transform: () => Promise.resolve("transformed-content {}"), + }) +}) + +test.serial("should ignore returned undefined", t => { + return compareFixtures(t, "transform-undefined", { + transform: () => undefined, + }) +}) + +test.serial("should ignore promised undefined", t => { + return compareFixtures(t, "transform-undefined", { + transform: () => Promise.resolve(undefined), + }) +}) From 8552b3fa772f767a82822b0885fe7fcb1cd965f4 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sat, 9 Jan 2016 23:32:26 +0300 Subject: [PATCH 2/4] Add documentation and changelog --- CHANGELOG.md | 3 +++ README.md | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4bb5623..52f0dd2c 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,9 @@ postcssImport({ }) ``` +- Changed: support promise in `transform` option and `undefined` result will be skipped +([#147](https://github.com/postcss/postcss-import/pull/147)) + # 7.1.3 - 2015-11-05 - Fixed: ensure node 0.12 compatibility, round 2 diff --git a/README.md b/README.md index 4063eca2..8f5824bc 100755 --- a/README.md +++ b/README.md @@ -109,7 +109,8 @@ _Note: nested `@import` will additionally benefit of the relative dirname of imp Type: `Function` Default: `null` -A function to transform the content of imported files. Take one argument (file content) & should return the modified content. +A function to transform the content of imported files. Take one argument (file content) and should return the modified content or promise with it. +`undefined` result will be skipped. #### `plugins` From 81a6d616d2c3a905f1aea2731aa5d25e0b9d86d4 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sun, 10 Jan 2016 03:03:20 +0300 Subject: [PATCH 3/4] Remove unnecessary test --- package.json | 1 - test/fixtures/imports/foo.styl | 2 -- test/fixtures/transform.css | 4 ---- test/fixtures/transform.expected.css | 9 --------- test/import.js | 6 ------ 5 files changed, 22 deletions(-) delete mode 100755 test/fixtures/imports/foo.styl delete mode 100755 test/fixtures/transform.css delete mode 100755 test/fixtures/transform.expected.css diff --git a/package.json b/package.json index 4ae27683..cade715a 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,6 @@ }, "devDependencies": { "ava": "^0.8.0", - "css-whitespace": "^1.1.1", "eslint": "^1.1.0", "postcss-scss": "^0.1.3" }, diff --git a/test/fixtures/imports/foo.styl b/test/fixtures/imports/foo.styl deleted file mode 100755 index 59bb3b52..00000000 --- a/test/fixtures/imports/foo.styl +++ /dev/null @@ -1,2 +0,0 @@ -foo - styl: true diff --git a/test/fixtures/transform.css b/test/fixtures/transform.css deleted file mode 100755 index 6ac154ab..00000000 --- a/test/fixtures/transform.css +++ /dev/null @@ -1,4 +0,0 @@ -@import "foo.styl"; -@import "foo.styl" (min-width: 25em); - -content{} diff --git a/test/fixtures/transform.expected.css b/test/fixtures/transform.expected.css deleted file mode 100755 index 7ac882c4..00000000 --- a/test/fixtures/transform.expected.css +++ /dev/null @@ -1,9 +0,0 @@ -foo { - styl: true; -} -@media (min-width: 25em) { -foo { - styl: true; -} -} -content{} diff --git a/test/import.js b/test/import.js index 57cc4663..c4bfe0d9 100644 --- a/test/import.js +++ b/test/import.js @@ -31,12 +31,6 @@ test("should import stylsheets relatively", t => { return compareFixtures(t, "relative") }) -test("should support transform", t => { - return compareFixtures(t, "transform", { - transform: require("css-whitespace"), - }) -}) - test("should work without a specified path", t => { return compareFixtures(t, "cwd") }) From 40cdbf6cdcb9af3454edb2ad868c72ddf161a2c7 Mon Sep 17 00:00:00 2001 From: Bogdan Chadkin Date: Sun, 10 Jan 2016 16:26:53 +0300 Subject: [PATCH 4/4] More strict transformed content checking --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 48efd494..72d32615 100755 --- a/index.js +++ b/index.js @@ -289,7 +289,7 @@ function loadImportContent( } return Promise.resolve(options.transform(content, filename, options)) .then(function(transformed) { - return transformed == null ? content : transformed + return typeof transformed === "string" ? transformed : content }) }) .then(function(content) {