Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended transform support #147

Merged
merged 4 commits into from
Jan 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
},
"devDependencies": {
"ava": "^0.8.0",
"css-whitespace": "^1.1.1",
"eslint": "^1.1.0",
"postcss-scss": "^0.1.3"
},
Expand Down
2 changes: 0 additions & 2 deletions test/fixtures/imports/foo.styl

This file was deleted.

1 change: 1 addition & 0 deletions test/fixtures/transform-content.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "foo"
1 change: 1 addition & 0 deletions test/fixtures/transform-content.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
transformed-content {}
1 change: 1 addition & 0 deletions test/fixtures/transform-undefined.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "foo"
1 change: 1 addition & 0 deletions test/fixtures/transform-undefined.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo{}
4 changes: 0 additions & 4 deletions test/fixtures/transform.css

This file was deleted.

9 changes: 0 additions & 9 deletions test/fixtures/transform.expected.css

This file was deleted.

6 changes: 0 additions & 6 deletions test/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Expand Down
26 changes: 26 additions & 0 deletions test/transform.js
Original file line number Diff line number Diff line change
@@ -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),
})
})