Skip to content
Closed
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ It's to optimize output and skip similar files like `normalize.css` for example.
If this behavior is not what you want, just set this option to `false` to
disable it.

#### `cache`

Type: `Object`
Default: `null`

Cache to save expensive file io. It maps the resolved file name to the corresponding transformed file contents (`String`).

#### Example with some options

```js
Expand Down
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function AtImport(options) {
importedFiles: {},
ignoredAtRules: [],
hashFiles: {},
cache: options.cache || {},
}
if (opts.from) {
state.importedFiles[opts.from] = {
Expand Down Expand Up @@ -347,7 +348,8 @@ function readImportedContent(
options.encoding,
options.transform || function(value) {
return value
}
},
state.cache
)

if (fileContent.trim() === "") {
Expand Down Expand Up @@ -537,8 +539,11 @@ function resolveFilename(name, root, paths, source, resolver) {
*
* @param {String} file
*/
function readFile(file, encoding, transform) {
return transform(fs.readFileSync(file, encoding || "utf8"), file)
function readFile(file, encoding, transform, cache) {
if (!cache[file]) {
cache[file] = transform(fs.readFileSync(file, encoding || "utf8"), file)
}
return cache[file]
}

/**
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,23 @@ test("sync", function(t) {

t.end()
})

test("cache option", function(t) {
t.plan(1)

postcss()
.use(atImport({
resolve: function(file) {
return path.basename(file)
},
cache: {
b: ".b{}",
c: "@import '/d';.c{}",
d: ".d{}",
},
}))
.process("@import '/c';@import '/b';.a{}")
.then(function(res) {
t.equal(res.css, ".d{}.c{}.b{}.a{}")
})
})