Skip to content

Commit 93a9163

Browse files
committed
updates to make watchify work
+ using a different kind of tranform that ends the stream correctly + inject require statements for composed css modules, so that they are added to browserify's dependency graph + clear token cache so that we can rebuild the parts that change
1 parent 01ccedc commit 93a9163

File tree

2 files changed

+68
-28
lines changed

2 files changed

+68
-28
lines changed

cmify.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var stream = require("stream");
2+
var path = require("path");
3+
var util = require("util");
4+
5+
util.inherits(Cmify, stream.Transform);
6+
function Cmify(filename, opts) {
7+
if (!(this instanceof Cmify)) {
8+
return new Cmify(filename, opts);
9+
}
10+
11+
stream.Transform.call(this);
12+
13+
this.cssExt = /\.css$/;
14+
this._data = "";
15+
this._filename = filename;
16+
}
17+
18+
Cmify.prototype.isCssFile = function (filename) {
19+
return this.cssExt.test(filename)
20+
}
21+
22+
Cmify.prototype._transform = function (buf, enc, callback) {
23+
// only handle .css files
24+
if (!this.isCssFile(this._filename)) {
25+
this.push(buf)
26+
return callback()
27+
}
28+
29+
this._data += buf
30+
callback()
31+
};
32+
33+
module.exports = Cmify

index.js

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ if (!global.Promise) { global.Promise = require('promise-polyfill') }
33

44
var fs = require('fs');
55
var path = require('path');
6-
var through = require('through');
6+
var Cmify = require('./cmify');
77
var Core = require('css-modules-loader-core');
88
var FileSystemLoader = require('css-modules-loader-core/lib/file-system-loader');
99
var assign = require('object-assign');
@@ -87,8 +87,6 @@ function dedupeSources (sources) {
8787
})
8888
}
8989

90-
var cssExt = /\.css$/;
91-
9290
// caches
9391
//
9492
// persist these for as long as the process is running. #32
@@ -161,31 +159,43 @@ module.exports = function (browserify, options) {
161159
// but re-created on each bundle call.
162160
var compiledCssStream;
163161

164-
function transform (filename) {
165-
// only handle .css files
166-
if (!cssExt.test(filename)) {
167-
return through();
168-
}
169-
170-
return through(function noop () {}, function end () {
171-
var self = this;
172-
173-
loader.fetch(path.relative(rootDir, filename), '/').then(function (tokens) {
174-
var output = 'module.exports = ' + JSON.stringify(tokens);
175-
176-
assign(tokensByFile, loader.tokensByFile);
162+
// TODO: clean this up so there's less scope crossing
163+
Cmify.prototype._flush = function (callback) {
164+
var self = this;
165+
var filename = this._filename;
177166

178-
self.queue(output);
179-
self.queue(null);
180-
}, function (err) {
181-
self.emit('error', err);
182-
});
167+
// only handle .css files
168+
if (!this.isCssFile(filename)) { return callback(); }
169+
170+
// convert css to js before pushing
171+
// reset the `tokensByFile` cache
172+
var relFilename = path.relative(rootDir, filename)
173+
tokensByFile[filename] = loader.tokensByFile[filename] = null;
174+
175+
loader.fetch(relFilename, '/').then(function (tokens) {
176+
var newFiles = Object.keys(loader.tokensByFile)
177+
var oldFiles = Object.keys(tokensByFile)
178+
var diff = newFiles.filter(function (f) {
179+
return oldFiles.indexOf(f) === -1
180+
})
181+
182+
var output = diff.map(function (f) {
183+
return "require('" + f + "')\n"
184+
}) + '\n\n' + 'module.exports = ' + JSON.stringify(tokens);
185+
186+
assign(tokensByFile, loader.tokensByFile);
187+
188+
self.push(output);
189+
return callback()
190+
}, function (err) {
191+
browserify.emit('error', err);
192+
return callback()
183193
});
184-
}
194+
};
185195

186-
browserify.transform(transform, {
187-
global: true
188-
});
196+
browserify.transform(Cmify);
197+
198+
// ----
189199

190200
browserify.on('bundle', function (bundle) {
191201
// on each bundle, create a new stream b/c the old one might have ended
@@ -223,9 +233,6 @@ module.exports = function (browserify, options) {
223233
}
224234
});
225235
}
226-
227-
// reset the `tokensByFile` cache
228-
tokensByFile = {};
229236
});
230237
});
231238

0 commit comments

Comments
 (0)