@@ -6,7 +6,6 @@ var path = require('path');
66var Cmify = require ( './cmify' ) ;
77var Core = require ( 'css-modules-loader-core' ) ;
88var FileSystemLoader = require ( './file-system-loader' ) ;
9- var assign = require ( 'object-assign' ) ;
109var stringHash = require ( 'string-hash' ) ;
1110var ReadableStream = require ( 'stream' ) . Readable ;
1211var through = require ( 'through2' ) ;
@@ -75,35 +74,8 @@ function normalizeManifestPaths (tokensByFile, rootDir) {
7574 return output ;
7675}
7776
78- // caches
79- //
80- // persist these for as long as the process is running. #32
81-
82- // keep track of all tokens so we can avoid duplicates
83- var tokensByFile = { } ;
84-
85- // we need a separate loader for each entry point
86- var loadersByFile = { } ;
87-
88- module . exports = function ( browserify , options ) {
89- options = options || { } ;
90-
91- // if no root directory is specified, assume the cwd
92- var rootDir = options . rootDir || options . d || browserify . _options . basedir ;
93- if ( rootDir ) { rootDir = path . resolve ( rootDir ) ; }
94- if ( ! rootDir ) { rootDir = process . cwd ( ) ; }
95-
96- var transformOpts = { } ;
97- if ( options . global || options . g ) {
98- transformOpts . global = true ;
99- }
100-
101- var cssOutFilename = options . output || options . o ;
102- var jsonOutFilename = options . json || options . jsonOutput ;
103- transformOpts . cssOutFilename = cssOutFilename ;
104- transformOpts . cssFilePattern = options . filePattern ;
105-
106- // PostCSS plugins passed to FileSystemLoader
77+ // PostCSS plugins passed to FileSystemLoader
78+ function getPlugins ( options ) {
10779 var plugins = options . use || options . u ;
10880 if ( ! plugins ) {
10981 plugins = getDefaultPlugins ( options ) ;
@@ -119,7 +91,7 @@ module.exports = function (browserify, options) {
11991 plugins = ( Array . isArray ( postcssBefore ) ? postcssBefore : [ postcssBefore ] ) . concat ( plugins ) . concat ( postcssAfter ) ;
12092
12193 // load plugins by name (if a string is used)
122- plugins = plugins . map ( function requirePlugin ( name ) {
94+ return plugins . map ( function requirePlugin ( name ) {
12395 // assume not strings are already required plugins
12496 if ( typeof name !== 'string' ) {
12597 return name ;
@@ -144,58 +116,54 @@ module.exports = function (browserify, options) {
144116
145117 return plugin ;
146118 } ) ;
119+ }
147120
148- // create a loader for this entry file
149- if ( ! loadersByFile [ cssOutFilename ] ) {
150- loadersByFile [ cssOutFilename ] = new FileSystemLoader ( rootDir , plugins ) ;
121+ module . exports = function ( browserify , options ) {
122+ options = options || { } ;
123+
124+ // if no root directory is specified, assume the cwd
125+ var rootDir = options . rootDir || options . d || browserify . _options . basedir ;
126+ if ( rootDir ) {
127+ rootDir = path . resolve ( rootDir ) ;
128+ }
129+ if ( ! rootDir ) {
130+ rootDir = process . cwd ( ) ;
151131 }
152132
153- // TODO: clean this up so there's less scope crossing
154- Cmify . prototype . _flush = function ( callback ) {
155- var self = this ;
156- var filename = this . _filename ;
157-
158- // only handle .css files
159- if ( ! this . isCssFile ( filename ) ) { return callback ( ) ; }
160-
161- // grab the correct loader
162- var loader = loadersByFile [ this . _cssOutFilename ] ;
163-
164- // convert css to js before pushing
165- // reset the `tokensByFile` cache
166- var relFilename = path . relative ( rootDir , filename ) ;
167- tokensByFile [ filename ] = loader . tokensByFile [ filename ] = null ;
168-
169- loader . fetch ( relFilename , '/' ) . then ( function ( tokens ) {
170- var deps = loader . deps . dependenciesOf ( filename ) ;
171- var output = deps . map ( function ( f ) {
172- return 'require("' + f + '")' ;
173- } ) ;
174- output . push ( 'module.exports = ' + JSON . stringify ( tokens ) ) ;
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- } ) ;
183-
184- if ( ! isValid ) {
185- var err = 'Composition in ' + filename + ' contains an undefined reference' ;
186- console . error ( err ) ;
187- output . push ( 'console.error("' + err + '");' ) ;
188- }
133+ var cssOutFilename = options . output || options . o ;
134+ var jsonOutFilename = options . json || options . jsonOutput ;
135+ var loader ;
136+ // keep track of all tokens so we can avoid duplicates
137+ var tokensByFile ;
138+ if ( options . cache ) {
139+ if ( options . cache . loaders ) {
140+ loader = options . cache . loaders [ cssOutFilename ] ;
141+ } else {
142+ options . cache . loaders = { } ;
143+ }
144+ if ( options . cache . tokens ) {
145+ tokensByFile = options . cache . tokens ;
146+ } else {
147+ options . cache . tokens = { } ;
148+ }
149+ }
189150
190- assign ( tokensByFile , loader . tokensByFile ) ;
151+ loader = loader || new FileSystemLoader ( rootDir , getPlugins ( options ) ) ;
152+ tokensByFile = tokensByFile || { } ;
191153
192- self . push ( output . join ( '\n' ) ) ;
193- return callback ( ) ;
194- } ) . catch ( function ( err ) {
195- self . push ( 'console.error("' + err + '");' ) ;
196- self . emit ( 'error' , err ) ;
197- return callback ( ) ;
198- } ) ;
154+ if ( options . cache ) {
155+ options . cache . loaders [ cssOutFilename ] = loader ;
156+ options . cache . tokens = tokensByFile ;
157+ }
158+
159+ var transformOpts = {
160+ cssFilePattern : options . filePattern
161+ , cssFiles : [ ]
162+ , cssOutFilename : cssOutFilename
163+ , global : options . global || options . g
164+ , loader : loader
165+ , rootDir : rootDir
166+ , tokensByFile : tokensByFile
199167 } ;
200168
201169 browserify . transform ( Cmify , transformOpts ) ;
@@ -214,7 +182,6 @@ module.exports = function (browserify, options) {
214182
215183 // Combine the collected sources for a single bundle into a single CSS file
216184 var self = this ;
217- var loader = loadersByFile [ cssOutFilename ] ;
218185 var css = loader . finalSource ;
219186
220187 // end the output stream
0 commit comments