From 312dd0959fe24996b7948305a543c4aefce95937 Mon Sep 17 00:00:00 2001 From: Alexey Litvinov Date: Sat, 28 Nov 2015 20:38:18 +0300 Subject: [PATCH 1/3] cache invalidation in the development mode --- src/index.js | 10 +++++-- test/cache.js | 57 ++++++++++++++++++++++++++++++++++++++++ test/fixture/dynamic.css | 4 +++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 test/cache.js create mode 100644 test/fixture/dynamic.css diff --git a/src/index.js b/src/index.js index 46fba71..3938bca 100644 --- a/src/index.js +++ b/src/index.js @@ -67,9 +67,15 @@ function fetch(to, from) { // https://github.com/postcss/postcss/blob/master/docs/api.md#lazywarnings lazyResult.warnings().forEach(message => console.warn(message.text)); - // updating cache tokens = lazyResult.root.tokens; - tokensByFile[filename] = tokens; + + if (process.env.NODE_ENV !== 'development') { + // updating cache + tokensByFile[filename] = tokens; + } else { + // clearing cache in development mode + delete require.cache[filename]; + } if (postProcess) { postProcess(lazyResult.css, filename); diff --git a/test/cache.js b/test/cache.js new file mode 100644 index 0000000..9da175c --- /dev/null +++ b/test/cache.js @@ -0,0 +1,57 @@ +import { equal } from 'assert'; +import { writeFileSync } from 'fs'; +import { join } from 'path'; +import { dropCache } from '../utils/sugar'; +import hook from '../src'; + +const fixture = join(__dirname, 'fixture/dynamic.css'); + +function updateFile(content) { + writeFileSync(fixture, content, 'utf8'); +} + +describe('development mode', () => { + describe('should cache calls not in development mode', () => { + before(() => { + hook(); + updateFile('.block\n{\n display: block;\n}'); + process.env.NODE_ENV = ''; + require(fixture); + updateFile('.inline-block\n{\n display: inline-block;\n}'); + }); + + after(() => { + process.env.NODE_ENV = ''; + dropCache(fixture); + }); + + it('should retrive data from cache', () => { + const tokens = require(fixture); + const keys = Object.keys(tokens); + equal(keys.length, 1); + equal(keys.join(''), 'block'); + }); + }); + + describe('should clear cache in development mode', () => { + before(() => { + hook(); + updateFile('.block\n{\n display: block;\n}'); + process.env.NODE_ENV = 'development'; + require(fixture); + updateFile('.inline-block\n{\n display: inline-block;\n}'); + }); + + after(() => { + process.env.NODE_ENV = ''; + dropCache(fixture); + }); + + it('should retrive data from fs', () => { + const tokens = require(fixture); + const keys = Object.keys(tokens); + equal(keys.length, 1); + equal(keys.join(''), 'inline-block'); + }); + }); +}); diff --git a/test/fixture/dynamic.css b/test/fixture/dynamic.css new file mode 100644 index 0000000..d34188e --- /dev/null +++ b/test/fixture/dynamic.css @@ -0,0 +1,4 @@ +.inline-block +{ + display: inline-block; +} \ No newline at end of file From 032aec06329c5160fd19caa46bc1628ef383791b Mon Sep 17 00:00:00 2001 From: Alexey Litvinov Date: Sat, 28 Nov 2015 20:42:28 +0300 Subject: [PATCH 2/3] slightly bigger timeout --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 7dc654c..fc77617 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "fixture": "webpack", "start": "esw -w .", "lint": "eslint .", - "test": "mocha --compilers js:babel/register", + "test": "mocha --compilers js:babel/register --timeout 5000", "test:cov": "`npm bin`/babel-node `npm bin`/isparta cover --report text --report html `npm bin`/_mocha", "test:gen": "babel-node generate-tests", "build": "babel src --out-dir dist", From 13eea02838332a0836a8a43ce10fa706d0175df9 Mon Sep 17 00:00:00 2001 From: Alexey Litvinov Date: Sat, 28 Nov 2015 20:50:39 +0300 Subject: [PATCH 3/3] readme --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index eefe146..3b6b987 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,14 @@ $ npm i css-modules-require-hook In this section I've tried to cover the common cases of usage. +### Development mode + +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: + +```bash +$ NODE_ENV=development node server.js +``` + ### Basic example Basically to attach the require hook you need to require this module. If you need to adjust it see the tuning section below.