Skip to content

Commit

Permalink
Merge pull request #147 from postcss/transform
Browse files Browse the repository at this point in the history
Extended transform support
  • Loading branch information
MoOx committed Jan 10, 2016
2 parents 74879af + 40cdbf6 commit 6aaa8db
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 26 deletions.
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),
})
})

0 comments on commit 6aaa8db

Please sign in to comment.