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 'cache' option for caching parsed output #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,30 @@ function compile(src, file) {
return src;
}
```

### cache
An object that will be used as a cache for parsed output of import files, indexed by absolute file path.
This option may be useful when integrating rework with a watcher by avoiding unnecessary parsing of unchanged files.

Example:

```js
var cache = {};

function buildCss() {
// When './abc' is resolved, the cache will be checked
// first. If not cached, the parsed output will be added
// to the cache.
rework('@import "./abc";', { source: 'index.css' })
.use(reworkNPM({ cache: cache }))
.toString();
}

// Triggered by a watcher, perhaps
function onCssFileChanged(file) {
// A css file has changed, so remove it from the cache
// and run rework again.
delete cache[path.resolve(file)];
buildCss();
}
```
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function reworkNPM(opts) {
var prefilter = opts.prefilter || identity;
var shim = opts.shim || {};
var alias = opts.alias || {};
var cache = opts.cache || null;

function inline(scope, style) {
style.rules = concatMap(style.rules, function(rule) {
Expand All @@ -41,10 +42,19 @@ function reworkNPM(opts) {
}
scope.push(file);

if (cache && cache.hasOwnProperty(file)) {
return cache[file];
}

var contents = fs.readFileSync(file, 'utf8');
contents = prefilter(contents, file);
contents = parse(contents, { source: path.relative(root, file) });
return inline(scope, contents.stylesheet).rules;

var rules = inline(scope, contents.stylesheet).rules;
if (cache) {
cache[file] = rules;
}
return rules;
}

function resolveImport(rule) {
Expand Down
32 changes: 32 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var reworkNPM = require('./');
var fs = require('fs');
var path = require('path');
var rework = require('rework');
var parse = require('css').parse;
var sass = require('node-sass');
var convertSourceMap = require('convert-source-map');
var SourceMapConsumer = require('source-map').SourceMapConsumer;
Expand Down Expand Up @@ -254,3 +255,34 @@ test('Provide filename as second arg to prefilter', function(t) {
: code;
}
});

test('Use cached rules if cache provided', function(t) {
var cachedContents = '.test {\n content: "Cached package";\n}';

var cache = {};
cache[path.resolve('test/node_modules/test/index.css')] = parse(cachedContents, { source: 'test/index.css' }).stylesheet.rules;

var input = '@import "test";';
var output = rework(input, { source: 'test/index.css' })
.use(reworkNPM({ cache: cache }))
.toString();
t.equal(output, cachedContents);

t.end();
});

test('Caches parsed output if cache provided', function(t) {
var cache = {};
var input = '@import "test";';
var output = rework(input, { source: 'test/index.css' })
.use(reworkNPM({ cache: cache }))
.toString();

t.equal(output, '.test {\n content: "Test package";\n}');

var resolvedPath = path.resolve('test/node_modules/test/index.css');
t.ok(cache[resolvedPath]);
t.type(cache[resolvedPath], "object");

t.end();
});