@@ -9,6 +9,7 @@ var FileSystemLoader = require('./file-system-loader');
99var assign = require ( 'object-assign' ) ;
1010var stringHash = require ( 'string-hash' ) ;
1111var 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+
241253module . exports . generateShortName = generateShortName ;
242254module . exports . generateLongName = generateLongName ;
0 commit comments