@@ -3,9 +3,9 @@ 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' ) ;
8- var FileSystemLoader = require ( 'css-modules-loader-core/lib /file-system-loader' ) ;
8+ var FileSystemLoader = require ( '. /file-system-loader' ) ;
99var assign = require ( 'object-assign' ) ;
1010var stringHash = require ( 'string-hash' ) ;
1111var ReadableStream = require ( 'stream' ) . Readable ;
@@ -16,7 +16,7 @@ var ReadableStream = require('stream').Readable;
1616*/
1717function generateShortName ( name , filename , css ) {
1818 // first occurrence of the name
19- // TOOD : better match with regex
19+ // TODO : better match with regex
2020 var i = css . indexOf ( '.' + name ) ;
2121 var numLines = css . substr ( 0 , i ) . split ( / [ \r \n ] / ) . length ;
2222
@@ -74,21 +74,6 @@ function normalizeManifestPaths (tokensByFile, rootDir) {
7474 return output ;
7575}
7676
77- function dedupeSources ( sources ) {
78- var foundHashes = { }
79- Object . keys ( sources ) . forEach ( function ( key ) {
80- var hash = stringHash ( sources [ key ] ) ;
81- if ( foundHashes [ hash ] ) {
82- delete sources [ key ] ;
83- }
84- else {
85- foundHashes [ hash ] = true ;
86- }
87- } )
88- }
89-
90- var cssExt = / \. c s s $ / ;
91-
9277// caches
9378//
9479// persist these for as long as the process is running. #32
@@ -107,6 +92,11 @@ module.exports = function (browserify, options) {
10792 if ( rootDir ) { rootDir = path . resolve ( rootDir ) ; }
10893 if ( ! rootDir ) { rootDir = process . cwd ( ) ; }
10994
95+ var transformOpts = { } ;
96+ if ( options . global ) {
97+ transformOpts . global = true ;
98+ }
99+
110100 var cssOutFilename = options . output || options . o ;
111101 var jsonOutFilename = options . json || options . jsonOutput ;
112102 var sourceKey = cssOutFilename ;
@@ -161,31 +151,56 @@ module.exports = function (browserify, options) {
161151 // but re-created on each bundle call.
162152 var compiledCssStream ;
163153
164- function transform ( filename ) {
165- // only handle .css files
166- if ( ! cssExt . test ( filename ) ) {
167- return through ( ) ;
168- }
154+ // TODO: clean this up so there's less scope crossing
155+ Cmify . prototype . _flush = function ( callback ) {
156+ var self = this ;
157+ var filename = this . _filename ;
169158
170- return through ( function noop ( ) { } , function end ( ) {
171- var self = this ;
159+ // only handle .css files
160+ if ( ! this . isCssFile ( filename ) ) { return callback ( ) ; }
161+
162+ // convert css to js before pushing
163+ // reset the `tokensByFile` cache
164+ var relFilename = path . relative ( rootDir , filename )
165+ tokensByFile [ filename ] = loader . tokensByFile [ filename ] = null ;
166+
167+ loader . fetch ( relFilename , '/' ) . then ( function ( tokens ) {
168+ var deps = loader . deps . dependenciesOf ( filename ) ;
169+ var output = [
170+ deps . map ( function ( f ) {
171+ return "require('" + f + "')"
172+ } ) . join ( '\n' ) ,
173+ 'module.exports = ' + JSON . stringify ( tokens )
174+ ] . join ( '\n' ) ;
175+
176+ var isValid = true ;
177+ var isUndefined = / \b u n d e f i n e d \b / ;
178+ Object . keys ( tokens ) . forEach ( function ( k ) {
179+ if ( isUndefined . test ( tokens [ k ] ) ) {
180+ isValid = false ;
181+ }
182+ } ) ;
172183
173- loader . fetch ( path . relative ( rootDir , filename ) , '/' ) . then ( function ( tokens ) {
174- var output = 'module.exports = ' + JSON . stringify ( tokens ) ;
184+ if ( ! isValid ) {
185+ var err = 'Composition in ' + filename + ' contains an undefined reference' ;
186+ console . error ( err )
187+ output += '\nconsole.error("' + err + '");' ;
188+ }
175189
176- assign ( tokensByFile , loader . tokensByFile ) ;
190+ assign ( tokensByFile , loader . tokensByFile ) ;
177191
178- self . queue ( output ) ;
179- self . queue ( null ) ;
180- } , function ( err ) {
181- self . emit ( 'error' , err ) ;
182- } ) ;
192+ self . push ( output ) ;
193+ return callback ( )
194+ } ) . catch ( function ( err ) {
195+ self . push ( 'console.error("' + err + '");' ) ;
196+ browserify . emit ( 'error' , err ) ;
197+ return callback ( )
183198 } ) ;
184- }
199+ } ;
185200
186- browserify . transform ( transform , {
187- global : true
188- } ) ;
201+ browserify . transform ( Cmify , transformOpts ) ;
202+
203+ // ----
189204
190205 browserify . on ( 'bundle' , function ( bundle ) {
191206 // on each bundle, create a new stream b/c the old one might have ended
@@ -195,10 +210,6 @@ module.exports = function (browserify, options) {
195210 bundle . emit ( 'css stream' , compiledCssStream ) ;
196211
197212 bundle . on ( 'end' , function ( ) {
198- // under certain conditions (eg. with shared libraries) we can end up with
199- // multiple occurrences of the same rule, so we need to remove duplicates
200- dedupeSources ( loader . sources )
201-
202213 // Combine the collected sources for a single bundle into a single CSS file
203214 var css = loader . finalSource ;
204215
@@ -223,9 +234,6 @@ module.exports = function (browserify, options) {
223234 }
224235 } ) ;
225236 }
226-
227- // reset the `tokensByFile` cache
228- tokensByFile = { } ;
229237 } ) ;
230238 } ) ;
231239
0 commit comments