From afbad84ed6967ca1717ea8dbe21e0face9146822 Mon Sep 17 00:00:00 2001 From: tjallingt Date: Sat, 16 Jan 2016 14:30:14 +0100 Subject: [PATCH 1/2] Cleaning up code --- .gitignore | 1 + index.js | 26 ++++++++++++-------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 8f5c351..ce25fd0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/* !node_modules/cool-styles +npm-debug.log \ No newline at end of file diff --git a/index.js b/index.js index 35113b0..e727ce5 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ // Some css-modules-loader-code dependencies use Promise so we'll provide it for older node versions -if (!global.Promise) { global.Promise = require('promise-polyfill') } +if (!global.Promise) { global.Promise = require('promise-polyfill'); } var fs = require('fs'); var path = require('path'); @@ -145,7 +145,7 @@ module.exports = function (browserify, options) { // get (or create) a loader for this entry file var loader = loadersByFile[sourceKey]; if (!loader) { - loader = loadersByFile[sourceKey] = new FileSystemLoader(rootDir, plugins); + loader = loadersByFile[sourceKey] = new FileSystemLoader(rootDir, plugins); } // the compiled CSS stream needs to be avalible to the transform, @@ -162,17 +162,15 @@ module.exports = function (browserify, options) { // convert css to js before pushing // reset the `tokensByFile` cache - var relFilename = path.relative(rootDir, filename) + var relFilename = path.relative(rootDir, filename); tokensByFile[filename] = loader.tokensByFile[filename] = null; loader.fetch(relFilename, '/').then(function (tokens) { var deps = loader.deps.dependenciesOf(filename); - var output = [ - deps.map(function (f) { - return "require('" + f + "')" - }).join('\n'), - 'module.exports = ' + JSON.stringify(tokens) - ].join('\n'); + var output = deps.map(function (f) { + return 'require("' + f + '")'; + }); + output.push('module.exports = ' + JSON.stringify(tokens)); var isValid = true; var isUndefined = /\bundefined\b/; @@ -184,18 +182,18 @@ module.exports = function (browserify, options) { if (!isValid) { var err = 'Composition in ' + filename + ' contains an undefined reference'; - console.error(err) - output += '\nconsole.error("' + err + '");'; + console.error(err); + output.push('console.error("' + err + '");'); } assign(tokensByFile, loader.tokensByFile); - self.push(output); - return callback() + self.push(output.join('\n')); + return callback(); }).catch(function (err) { self.push('console.error("' + err + '");'); browserify.emit('error', err); - return callback() + return callback(); }); }; From 60ee38dacbdc452f4184e178eb65cdd1d66d872f Mon Sep 17 00:00:00 2001 From: tjallingt Date: Sat, 16 Jan 2016 14:53:33 +0100 Subject: [PATCH 2/2] Fixed async code accessing a variable after it has been changed. --- cmify.js | 1 + index.js | 19 +++++++++---------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cmify.js b/cmify.js index cd34282..5247b86 100644 --- a/cmify.js +++ b/cmify.js @@ -13,6 +13,7 @@ function Cmify(filename, opts) { this.cssExt = /\.css$/; this._data = ""; this._filename = filename; + this._cssOutFilename = opts.cssOutFilename; } Cmify.prototype.isCssFile = function (filename) { diff --git a/index.js b/index.js index e727ce5..d1efa55 100644 --- a/index.js +++ b/index.js @@ -99,7 +99,7 @@ module.exports = function (browserify, options) { var cssOutFilename = options.output || options.o; var jsonOutFilename = options.json || options.jsonOutput; - var sourceKey = cssOutFilename; + transformOpts.cssOutFilename = cssOutFilename; // PostCSS plugins passed to FileSystemLoader var plugins = options.use || options.u; @@ -142,16 +142,11 @@ module.exports = function (browserify, options) { return plugin; }); - // get (or create) a loader for this entry file - var loader = loadersByFile[sourceKey]; - if (!loader) { - loader = loadersByFile[sourceKey] = new FileSystemLoader(rootDir, plugins); + // create a loader for this entry file + if (!loadersByFile[cssOutFilename]) { + loadersByFile[cssOutFilename] = new FileSystemLoader(rootDir, plugins); } - // the compiled CSS stream needs to be avalible to the transform, - // but re-created on each bundle call. - var compiledCssStream; - // TODO: clean this up so there's less scope crossing Cmify.prototype._flush = function (callback) { var self = this; @@ -160,6 +155,9 @@ module.exports = function (browserify, options) { // only handle .css files if (!this.isCssFile(filename)) { return callback(); } + // grab the correct loader + var loader = loadersByFile[this._cssOutFilename]; + // convert css to js before pushing // reset the `tokensByFile` cache var relFilename = path.relative(rootDir, filename); @@ -203,13 +201,14 @@ module.exports = function (browserify, options) { browserify.on('bundle', function (bundle) { // on each bundle, create a new stream b/c the old one might have ended - compiledCssStream = new ReadableStream(); + var compiledCssStream = new ReadableStream(); compiledCssStream._read = function () {}; bundle.emit('css stream', compiledCssStream); bundle.on('end', function () { // Combine the collected sources for a single bundle into a single CSS file + var loader = loadersByFile[cssOutFilename]; var css = loader.finalSource; // end the output stream