@@ -3,7 +3,7 @@ if (!global.Promise) { global.Promise = require('promise-polyfill') }
33
44var fs = require ( 'fs' ) ;
55var path = require ( 'path' ) ;
6- var through = require ( 'through ' ) ;
6+ var Cmify = require ( './cmify ' ) ;
77var Core = require ( 'css-modules-loader-core' ) ;
88var FileSystemLoader = require ( 'css-modules-loader-core/lib/file-system-loader' ) ;
99var assign = require ( 'object-assign' ) ;
@@ -87,8 +87,6 @@ function dedupeSources (sources) {
8787 } )
8888}
8989
90- var cssExt = / \. c s s $ / ;
91-
9290// caches
9391//
9492// persist these for as long as the process is running. #32
@@ -161,31 +159,43 @@ module.exports = function (browserify, options) {
161159 // but re-created on each bundle call.
162160 var compiledCssStream ;
163161
164- function transform ( filename ) {
165- // only handle .css files
166- if ( ! cssExt . test ( filename ) ) {
167- return through ( ) ;
168- }
169-
170- return through ( function noop ( ) { } , function end ( ) {
171- var self = this ;
172-
173- loader . fetch ( path . relative ( rootDir , filename ) , '/' ) . then ( function ( tokens ) {
174- var output = 'module.exports = ' + JSON . stringify ( tokens ) ;
175-
176- assign ( tokensByFile , loader . tokensByFile ) ;
162+ // TODO: clean this up so there's less scope crossing
163+ Cmify . prototype . _flush = function ( callback ) {
164+ var self = this ;
165+ var filename = this . _filename ;
177166
178- self . queue ( output ) ;
179- self . queue ( null ) ;
180- } , function ( err ) {
181- self . emit ( 'error' , err ) ;
182- } ) ;
167+ // only handle .css files
168+ if ( ! this . isCssFile ( filename ) ) { return callback ( ) ; }
169+
170+ // convert css to js before pushing
171+ // reset the `tokensByFile` cache
172+ var relFilename = path . relative ( rootDir , filename )
173+ tokensByFile [ filename ] = loader . tokensByFile [ filename ] = null ;
174+
175+ loader . fetch ( relFilename , '/' ) . then ( function ( tokens ) {
176+ var newFiles = Object . keys ( loader . tokensByFile )
177+ var oldFiles = Object . keys ( tokensByFile )
178+ var diff = newFiles . filter ( function ( f ) {
179+ return oldFiles . indexOf ( f ) === - 1
180+ } )
181+
182+ var output = diff . map ( function ( f ) {
183+ return "require('" + f + "')\n"
184+ } ) + '\n\n' + 'module.exports = ' + JSON . stringify ( tokens ) ;
185+
186+ assign ( tokensByFile , loader . tokensByFile ) ;
187+
188+ self . push ( output ) ;
189+ return callback ( )
190+ } , function ( err ) {
191+ browserify . emit ( 'error' , err ) ;
192+ return callback ( )
183193 } ) ;
184- }
194+ } ;
185195
186- browserify . transform ( transform , {
187- global : true
188- } ) ;
196+ browserify . transform ( Cmify ) ;
197+
198+ // ----
189199
190200 browserify . on ( 'bundle' , function ( bundle ) {
191201 // on each bundle, create a new stream b/c the old one might have ended
@@ -223,9 +233,6 @@ module.exports = function (browserify, options) {
223233 }
224234 } ) ;
225235 }
226-
227- // reset the `tokensByFile` cache
228- tokensByFile = { } ;
229236 } ) ;
230237 } ) ;
231238
0 commit comments