Skip to content

Commit 91b9c9d

Browse files
committed
Move final CSS packing to the pack stage of the browserify pipeline
1 parent a0b2f24 commit 91b9c9d

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

index.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var FileSystemLoader = require('./file-system-loader');
99
var assign = require('object-assign');
1010
var stringHash = require('string-hash');
1111
var ReadableStream = require('stream').Readable;
12+
var through = require('through');
1213

1314
/*
1415
Custom `generateScopedName` function for `postcss-modules-scope`.
@@ -206,37 +207,48 @@ module.exports = function (browserify, options) {
206207

207208
browserify.emit('css stream', compiledCssStream);
208209

209-
bundle.on('end', function () {
210+
browserify.pipeline.get('pack').push(through(null, function () {
210211
// Combine the collected sources for a single bundle into a single CSS file
212+
var self = this;
211213
var loader = loadersByFile[cssOutFilename];
212214
var css = loader.finalSource;
213215

214216
// end the output stream
215217
compiledCssStream.push(css);
216218
compiledCssStream.push(null);
217219

220+
const writes = [];
221+
218222
// write the css file
219223
if (cssOutFilename) {
220-
fs.writeFile(cssOutFilename, css, function (err) {
221-
if (err) {
222-
browserify.emit('error', err);
223-
}
224-
});
224+
writes.push(writeFile(cssOutFilename, css));
225225
}
226226

227227
// write the classname manifest
228228
if (jsonOutFilename) {
229-
fs.writeFile(jsonOutFilename, JSON.stringify(normalizeManifestPaths(tokensByFile, rootDir)), function (err) {
230-
if (err) {
231-
browserify.emit('error', err);
232-
}
233-
});
229+
writes.push(writeFile(jsonOutFilename, JSON.stringify(normalizeManifestPaths(tokensByFile, rootDir))));
234230
}
235-
});
231+
232+
Promise.all(writes)
233+
.then(function () { self.queue(null); })
234+
.catch(function (err) { self.emit('error', err); })
235+
}));
236236
});
237237

238+
238239
return browserify;
239240
};
240241

242+
function writeFile(filename, content) {
243+
return new Promise(function (resolve, reject) {
244+
fs.writeFile(filename, content, function (err) {
245+
if (err)
246+
reject(err);
247+
else
248+
resolve();
249+
});
250+
});
251+
}
252+
241253
module.exports.generateShortName = generateShortName;
242254
module.exports.generateLongName = generateLongName;

0 commit comments

Comments
 (0)