Skip to content

Commit 1064876

Browse files
committed
Merge pull request #51 from css-modules/sullenor/development-mode
cache invalidation in the development mode
2 parents 4f6c19d + 13eea02 commit 1064876

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ $ npm i css-modules-require-hook
3232

3333
In this section I've tried to cover the common cases of usage.
3434

35+
### Development mode
36+
37+
Usually, Node.js caches all the `require` calls by default. In order to invalidate cache for the purpose of development you should set the environment variable `NODE_ENV` to `development`. For example:
38+
39+
```bash
40+
$ NODE_ENV=development node server.js
41+
```
42+
3543
### Basic example
3644

3745
Basically to attach the require hook you need to require this module. If you need to adjust it see the tuning section below.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"fixture": "webpack",
5454
"start": "esw -w .",
5555
"lint": "eslint .",
56-
"test": "mocha --compilers js:babel/register",
56+
"test": "mocha --compilers js:babel/register --timeout 5000",
5757
"test:cov": "`npm bin`/babel-node `npm bin`/isparta cover --report text --report html `npm bin`/_mocha",
5858
"test:gen": "babel-node generate-tests",
5959
"build": "babel src --out-dir dist",

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)