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` diff --git a/index.js b/index.js index d77495bf..72d32615 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 typeof transformed === "string" ? transformed : content + }) + }) + .then(function(content) { if (content.trim() === "") { result.warn(filename + " is empty", { node: atRule }) return 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-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/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") }) 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), + }) +})