Skip to content

Commit 9723971

Browse files
committed
Add custom load support
1 parent a91f4fe commit 9723971

File tree

5 files changed

+40
-12
lines changed

5 files changed

+40
-12
lines changed

index.js

Lines changed: 8 additions & 12 deletions
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

Lines changed: 12 additions & 0 deletions
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

Lines changed: 18 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "foo"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
custom-content {}

0 commit comments

Comments
 (0)