Skip to content

Commit 3ddc78c

Browse files
committed
Merge pull request #144 from postcss/custom-load
Add custom load support
2 parents a91f4fe + d798ffa commit 3ddc78c

File tree

7 files changed

+62
-19
lines changed

7 files changed

+62
-19
lines changed

CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ See [resolve option](https://github.com/postcss/postcss-import#resolve) for more
1313
([#130](https://github.com/postcss/postcss-import/pull/130))
1414
- Changed: glob resolver do not add `moduleDirectories` and parse all uri as glob patterns
1515
([#131](https://github.com/postcss/postcss-import/pull/131))
16+
- Added: support custom `load` option
17+
([#144](https://github.com/postcss/postcss-import/pull/144))
18+
- Removed: `encoding` option.
19+
([#144](https://github.com/postcss/postcss-import/pull/144))
20+
21+
Encoding can be specified in custom `load` option
22+
23+
```js
24+
postcssImport({
25+
load: function(filename) {
26+
return fs.readFileSync(filename, "utf-8")
27+
}
28+
})
29+
```
1630

1731
# 7.1.3 - 2015-11-05
1832

README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,6 @@ Default: `undefined`
118118

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

121-
#### `encoding`
122-
123-
Type: `String`
124-
Default: `utf8`
125-
126-
Use if your CSS is encoded in anything other than UTF-8.
127-
128121
#### `onImport`
129122

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

145+
#### `load`
146+
147+
Type: `Function`
148+
Default: null
149+
150+
You can overwrite the default loading way by setting this option.
151+
This function gets `(filename, importOptions)` arguments and returns content or promised content.
152+
152153
#### `skipDuplicates`
153154

154155
Type: `Boolean`

index.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
var fs = require("fs")
21
var path = require("path")
32
var assign = require("object-assign")
43
var postcss = require("postcss")
54
var joinMedia = require("./lib/join-media")
65
var resolveId = require("./lib/resolve-id")
6+
var loadContent = require("./lib/load-content")
77
var parseStatements = require("./lib/parse-statements")
88

99
function AtImport(options) {
1010
options = assign({
1111
root: process.cwd(),
1212
path: [],
1313
skipDuplicates: true,
14-
encoding: "utf8",
14+
load: loadContent,
1515
}, options)
1616

1717
options.root = path.resolve(options.root)
@@ -281,14 +281,8 @@ function loadImportContent(
281281
state.importedFiles[filename][media] = true
282282
}
283283

284-
return new Promise(function(resolve, reject) {
285-
fs.readFile(filename, options.encoding, function(err, data) {
286-
if (err) {
287-
return reject(err)
288-
}
289-
resolve(data)
290-
})
291-
}).then(function(content) {
284+
return Promise.resolve(options.load(filename, options))
285+
.then(function(content) {
292286
if (typeof options.transform === "function") {
293287
content = options.transform(content, filename)
294288
}
@@ -333,8 +327,10 @@ function loadImportContent(
333327
state,
334328
media,
335329
processor
336-
).then(function(statements) {
337-
return processor.process(newStyles).then(function(newResult) {
330+
)
331+
.then(function(statements) {
332+
return processor.process(newStyles)
333+
.then(function(newResult) {
338334
result.messages = result.messages.concat(newResult.messages)
339335

340336
return statements

lib/load-content.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var fs = require("fs")
2+
3+
module.exports = function(filename) {
4+
return new Promise(function(resolve, reject) {
5+
fs.readFile(filename, "utf-8", function(err, data) {
6+
if (err) {
7+
return reject(err)
8+
}
9+
resolve(data)
10+
})
11+
})
12+
}

test/custom-load.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import test from "ava"
2+
import compareFixtures from "./lib/compare-fixtures"
3+
4+
test("should accept content", t => {
5+
return compareFixtures(t, "custom-load", {
6+
load: () => {
7+
return "custom-content {}"
8+
},
9+
})
10+
})
11+
12+
test("should accept promised content", t => {
13+
return compareFixtures(t, "custom-load", {
14+
load: () => {
15+
return Promise.resolve("custom-content {}")
16+
},
17+
})
18+
})

test/fixtures/custom-load.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "foo"
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
custom-content {}

0 commit comments

Comments
 (0)