Skip to content

Commit f306d5f

Browse files
committed
Merge pull request #77 from tjallingt/master
Fix separation of loaders
2 parents 58c8ed0 + 60ee38d commit f306d5f

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/*
22
!node_modules/cool-styles
3+
npm-debug.log

cmify.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function Cmify(filename, opts) {
1313
this.cssExt = /\.css$/;
1414
this._data = "";
1515
this._filename = filename;
16+
this._cssOutFilename = opts.cssOutFilename;
1617
}
1718

1819
Cmify.prototype.isCssFile = function (filename) {

index.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Some css-modules-loader-code dependencies use Promise so we'll provide it for older node versions
2-
if (!global.Promise) { global.Promise = require('promise-polyfill') }
2+
if (!global.Promise) { global.Promise = require('promise-polyfill'); }
33

44
var fs = require('fs');
55
var path = require('path');
@@ -99,7 +99,7 @@ module.exports = function (browserify, options) {
9999

100100
var cssOutFilename = options.output || options.o;
101101
var jsonOutFilename = options.json || options.jsonOutput;
102-
var sourceKey = cssOutFilename;
102+
transformOpts.cssOutFilename = cssOutFilename;
103103

104104
// PostCSS plugins passed to FileSystemLoader
105105
var plugins = options.use || options.u;
@@ -142,16 +142,11 @@ module.exports = function (browserify, options) {
142142
return plugin;
143143
});
144144

145-
// get (or create) a loader for this entry file
146-
var loader = loadersByFile[sourceKey];
147-
if (!loader) {
148-
loader = loadersByFile[sourceKey] = new FileSystemLoader(rootDir, plugins);
145+
// create a loader for this entry file
146+
if (!loadersByFile[cssOutFilename]) {
147+
loadersByFile[cssOutFilename] = new FileSystemLoader(rootDir, plugins);
149148
}
150149

151-
// the compiled CSS stream needs to be avalible to the transform,
152-
// but re-created on each bundle call.
153-
var compiledCssStream;
154-
155150
// TODO: clean this up so there's less scope crossing
156151
Cmify.prototype._flush = function (callback) {
157152
var self = this;
@@ -160,19 +155,20 @@ module.exports = function (browserify, options) {
160155
// only handle .css files
161156
if (!this.isCssFile(filename)) { return callback(); }
162157

158+
// grab the correct loader
159+
var loader = loadersByFile[this._cssOutFilename];
160+
163161
// convert css to js before pushing
164162
// reset the `tokensByFile` cache
165-
var relFilename = path.relative(rootDir, filename)
163+
var relFilename = path.relative(rootDir, filename);
166164
tokensByFile[filename] = loader.tokensByFile[filename] = null;
167165

168166
loader.fetch(relFilename, '/').then(function (tokens) {
169167
var deps = loader.deps.dependenciesOf(filename);
170-
var output = [
171-
deps.map(function (f) {
172-
return "require('" + f + "')"
173-
}).join('\n'),
174-
'module.exports = ' + JSON.stringify(tokens)
175-
].join('\n');
168+
var output = deps.map(function (f) {
169+
return 'require("' + f + '")';
170+
});
171+
output.push('module.exports = ' + JSON.stringify(tokens));
176172

177173
var isValid = true;
178174
var isUndefined = /\bundefined\b/;
@@ -184,18 +180,18 @@ module.exports = function (browserify, options) {
184180

185181
if (!isValid) {
186182
var err = 'Composition in ' + filename + ' contains an undefined reference';
187-
console.error(err)
188-
output += '\nconsole.error("' + err + '");';
183+
console.error(err);
184+
output.push('console.error("' + err + '");');
189185
}
190186

191187
assign(tokensByFile, loader.tokensByFile);
192188

193-
self.push(output);
194-
return callback()
189+
self.push(output.join('\n'));
190+
return callback();
195191
}).catch(function (err) {
196192
self.push('console.error("' + err + '");');
197193
browserify.emit('error', err);
198-
return callback()
194+
return callback();
199195
});
200196
};
201197

@@ -205,13 +201,14 @@ module.exports = function (browserify, options) {
205201

206202
browserify.on('bundle', function (bundle) {
207203
// on each bundle, create a new stream b/c the old one might have ended
208-
compiledCssStream = new ReadableStream();
204+
var compiledCssStream = new ReadableStream();
209205
compiledCssStream._read = function () {};
210206

211207
bundle.emit('css stream', compiledCssStream);
212208

213209
bundle.on('end', function () {
214210
// Combine the collected sources for a single bundle into a single CSS file
211+
var loader = loadersByFile[cssOutFilename];
215212
var css = loader.finalSource;
216213

217214
// end the output stream

0 commit comments

Comments
 (0)