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

Add custom load support #144

Merged
merged 2 commits into from
Jan 9, 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
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ See [resolve option](https://github.com/postcss/postcss-import#resolve) for more
([#130](https://github.com/postcss/postcss-import/pull/130))
- Changed: glob resolver do not add `moduleDirectories` and parse all uri as glob patterns
([#131](https://github.com/postcss/postcss-import/pull/131))
- Added: support custom `load` option
([#144](https://github.com/postcss/postcss-import/pull/144))
- Removed: `encoding` option.
([#144](https://github.com/postcss/postcss-import/pull/144))

Encoding can be specified in custom `load` option

```js
postcssImport({
load: function(filename) {
return fs.readFileSync(filename, "utf-8")
}
})
```

# 7.1.3 - 2015-11-05

Expand Down
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,6 @@ Default: `undefined`

An array of plugins to be applied on each imported file.

#### `encoding`

Type: `String`
Default: `utf8`

Use if your CSS is encoded in anything other than UTF-8.

#### `onImport`

Type: `Function`
Expand All @@ -149,6 +142,14 @@ You can overwrite the default path resolving way by setting this option.
This function gets `(id, basedir, importOptions)` arguments and returns full path, array of paths or promise resolving paths.
You can use [resolve](https://github.com/substack/node-resolve) for that.

#### `load`

Type: `Function`
Default: null

You can overwrite the default loading way by setting this option.
This function gets `(filename, importOptions)` arguments and returns content or promised content.

#### `skipDuplicates`

Type: `Boolean`
Expand Down
20 changes: 8 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
var fs = require("fs")
var path = require("path")
var assign = require("object-assign")
var postcss = require("postcss")
var joinMedia = require("./lib/join-media")
var resolveId = require("./lib/resolve-id")
var loadContent = require("./lib/load-content")
var parseStatements = require("./lib/parse-statements")

function AtImport(options) {
options = assign({
root: process.cwd(),
path: [],
skipDuplicates: true,
encoding: "utf8",
load: loadContent,
}, options)

options.root = path.resolve(options.root)
Expand Down Expand Up @@ -281,14 +281,8 @@ function loadImportContent(
state.importedFiles[filename][media] = true
}

return new Promise(function(resolve, reject) {
fs.readFile(filename, options.encoding, function(err, data) {
if (err) {
return reject(err)
}
resolve(data)
})
}).then(function(content) {
return Promise.resolve(options.load(filename, options))
.then(function(content) {
if (typeof options.transform === "function") {
content = options.transform(content, filename)
}
Expand Down Expand Up @@ -333,8 +327,10 @@ function loadImportContent(
state,
media,
processor
).then(function(statements) {
return processor.process(newStyles).then(function(newResult) {
)
.then(function(statements) {
return processor.process(newStyles)
.then(function(newResult) {
result.messages = result.messages.concat(newResult.messages)

return statements
Expand Down
12 changes: 12 additions & 0 deletions lib/load-content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var fs = require("fs")

module.exports = function(filename) {
return new Promise(function(resolve, reject) {
fs.readFile(filename, "utf-8", function(err, data) {
if (err) {
return reject(err)
}
resolve(data)
})
})
}
18 changes: 18 additions & 0 deletions test/custom-load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import test from "ava"
import compareFixtures from "./lib/compare-fixtures"

test("should accept content", t => {
return compareFixtures(t, "custom-load", {
load: () => {
return "custom-content {}"
},
})
})

test("should accept promised content", t => {
return compareFixtures(t, "custom-load", {
load: () => {
return Promise.resolve("custom-content {}")
},
})
})
1 change: 1 addition & 0 deletions test/fixtures/custom-load.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "foo"
1 change: 1 addition & 0 deletions test/fixtures/custom-load.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom-content {}