1
1
// Some css-modules-loader-code dependencies use Promise so we'll provide it for older node versions
2
- if ( ! global . Promise ) { global . Promise = require ( 'promise-polyfill' ) }
2
+ if ( ! global . Promise ) { global . Promise = require ( 'promise-polyfill' ) ; }
3
3
4
4
var fs = require ( 'fs' ) ;
5
5
var path = require ( 'path' ) ;
@@ -99,7 +99,7 @@ module.exports = function (browserify, options) {
99
99
100
100
var cssOutFilename = options . output || options . o ;
101
101
var jsonOutFilename = options . json || options . jsonOutput ;
102
- var sourceKey = cssOutFilename ;
102
+ transformOpts . cssOutFilename = cssOutFilename ;
103
103
104
104
// PostCSS plugins passed to FileSystemLoader
105
105
var plugins = options . use || options . u ;
@@ -142,16 +142,11 @@ module.exports = function (browserify, options) {
142
142
return plugin ;
143
143
} ) ;
144
144
145
- // get (or create) a loader for this entry file
146
- var loader = loadersByFile [ sourceKey ] ;
147
- if ( ! loader ) {
148
- loader = loadersByFile [ sourceKey ] = new FileSystemLoader ( rootDir , plugins ) ;
145
+ // create a loader for this entry file
146
+ if ( ! loadersByFile [ cssOutFilename ] ) {
147
+ loadersByFile [ cssOutFilename ] = new FileSystemLoader ( rootDir , plugins ) ;
149
148
}
150
149
151
- // the compiled CSS stream needs to be avalible to the transform,
152
- // but re-created on each bundle call.
153
- var compiledCssStream ;
154
-
155
150
// TODO: clean this up so there's less scope crossing
156
151
Cmify . prototype . _flush = function ( callback ) {
157
152
var self = this ;
@@ -160,19 +155,20 @@ module.exports = function (browserify, options) {
160
155
// only handle .css files
161
156
if ( ! this . isCssFile ( filename ) ) { return callback ( ) ; }
162
157
158
+ // grab the correct loader
159
+ var loader = loadersByFile [ this . _cssOutFilename ] ;
160
+
163
161
// convert css to js before pushing
164
162
// reset the `tokensByFile` cache
165
- var relFilename = path . relative ( rootDir , filename )
163
+ var relFilename = path . relative ( rootDir , filename ) ;
166
164
tokensByFile [ filename ] = loader . tokensByFile [ filename ] = null ;
167
165
168
166
loader . fetch ( relFilename , '/' ) . then ( function ( tokens ) {
169
167
var deps = loader . deps . dependenciesOf ( filename ) ;
170
- var output = [
171
- deps . map ( function ( f ) {
172
- return "require('" + f + "')"
173
- } ) . join ( '\n' ) ,
174
- 'module.exports = ' + JSON . stringify ( tokens )
175
- ] . join ( '\n' ) ;
168
+ var output = deps . map ( function ( f ) {
169
+ return 'require("' + f + '")' ;
170
+ } ) ;
171
+ output . push ( 'module.exports = ' + JSON . stringify ( tokens ) ) ;
176
172
177
173
var isValid = true ;
178
174
var isUndefined = / \b u n d e f i n e d \b / ;
@@ -184,18 +180,18 @@ module.exports = function (browserify, options) {
184
180
185
181
if ( ! isValid ) {
186
182
var err = 'Composition in ' + filename + ' contains an undefined reference' ;
187
- console . error ( err )
188
- output += '\nconsole .error("' + err + '");' ;
183
+ console . error ( err ) ;
184
+ output . push ( 'console .error("' + err + '");' ) ;
189
185
}
190
186
191
187
assign ( tokensByFile , loader . tokensByFile ) ;
192
188
193
- self . push ( output ) ;
194
- return callback ( )
189
+ self . push ( output . join ( '\n' ) ) ;
190
+ return callback ( ) ;
195
191
} ) . catch ( function ( err ) {
196
192
self . push ( 'console.error("' + err + '");' ) ;
197
193
browserify . emit ( 'error' , err ) ;
198
- return callback ( )
194
+ return callback ( ) ;
199
195
} ) ;
200
196
} ;
201
197
@@ -205,13 +201,14 @@ module.exports = function (browserify, options) {
205
201
206
202
browserify . on ( 'bundle' , function ( bundle ) {
207
203
// on each bundle, create a new stream b/c the old one might have ended
208
- compiledCssStream = new ReadableStream ( ) ;
204
+ var compiledCssStream = new ReadableStream ( ) ;
209
205
compiledCssStream . _read = function ( ) { } ;
210
206
211
207
bundle . emit ( 'css stream' , compiledCssStream ) ;
212
208
213
209
bundle . on ( 'end' , function ( ) {
214
210
// Combine the collected sources for a single bundle into a single CSS file
211
+ var loader = loadersByFile [ cssOutFilename ] ;
215
212
var css = loader . finalSource ;
216
213
217
214
// end the output stream
0 commit comments