Skip to content

Commit 312dd09

Browse files
committed
cache invalidation in the development mode
1 parent 4f6c19d commit 312dd09

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

src/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ function fetch(to, from) {
6767
// https://github.com/postcss/postcss/blob/master/docs/api.md#lazywarnings
6868
lazyResult.warnings().forEach(message => console.warn(message.text));
6969

70-
// updating cache
7170
tokens = lazyResult.root.tokens;
72-
tokensByFile[filename] = tokens;
71+
72+
if (process.env.NODE_ENV !== 'development') {
73+
// updating cache
74+
tokensByFile[filename] = tokens;
75+
} else {
76+
// clearing cache in development mode
77+
delete require.cache[filename];
78+
}
7379

7480
if (postProcess) {
7581
postProcess(lazyResult.css, filename);

test/cache.js

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { equal } from 'assert';
2+
import { writeFileSync } from 'fs';
3+
import { join } from 'path';
4+
import { dropCache } from '../utils/sugar';
5+
import hook from '../src';
6+
7+
const fixture = join(__dirname, 'fixture/dynamic.css');
8+
9+
function updateFile(content) {
10+
writeFileSync(fixture, content, 'utf8');
11+
}
12+
13+
describe('development mode', () => {
14+
describe('should cache calls not in development mode', () => {
15+
before(() => {
16+
hook();
17+
updateFile('.block\n{\n display: block;\n}');
18+
process.env.NODE_ENV = '';
19+
require(fixture);
20+
updateFile('.inline-block\n{\n display: inline-block;\n}');
21+
});
22+
23+
after(() => {
24+
process.env.NODE_ENV = '';
25+
dropCache(fixture);
26+
});
27+
28+
it('should retrive data from cache', () => {
29+
const tokens = require(fixture);
30+
const keys = Object.keys(tokens);
31+
equal(keys.length, 1);
32+
equal(keys.join(''), 'block');
33+
});
34+
});
35+
36+
describe('should clear cache in development mode', () => {
37+
before(() => {
38+
hook();
39+
updateFile('.block\n{\n display: block;\n}');
40+
process.env.NODE_ENV = 'development';
41+
require(fixture);
42+
updateFile('.inline-block\n{\n display: inline-block;\n}');
43+
});
44+
45+
after(() => {
46+
process.env.NODE_ENV = '';
47+
dropCache(fixture);
48+
});
49+
50+
it('should retrive data from fs', () => {
51+
const tokens = require(fixture);
52+
const keys = Object.keys(tokens);
53+
equal(keys.length, 1);
54+
equal(keys.join(''), 'inline-block');
55+
});
56+
});
57+
});

test/fixture/dynamic.css

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.inline-block
2+
{
3+
display: inline-block;
4+
}

0 commit comments

Comments
 (0)