Skip to content

Fix separation of loaders #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/*
!node_modules/cool-styles
npm-debug.log
1 change: 1 addition & 0 deletions cmify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
43 changes: 20 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -160,19 +155,20 @@ 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)
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/;
Expand All @@ -184,18 +180,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();
});
};

Expand All @@ -205,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
Expand Down