@@ -43,11 +43,10 @@ export default class Bundler {
4343 // turn on injection of css (inject onto html head)
4444 if ( opts . injectCss || opts . injectCSS ) this . _injectCss = true ;
4545
46- this . _unitsMap = Object . create ( null ) ;
47- this . _user_modules_done = new Set ( ) ;
48- this . _package_modules_done = new Set ( ) ;
49- this . _moduleIds_todo = Object . create ( null ) ;
50- this . _readersMap = Object . create ( null ) ;
46+ this . _unitsMap = { } ;
47+ this . _moduleId_done = new Set ( ) ;
48+ this . _moduleIds_todo = new Set ( ) ;
49+ this . _readersMap = { } ;
5150 this . _locator = opts . packageLocator || defaultPackageLocator ;
5251
5352 // baseUrl default to "dist"
@@ -81,7 +80,7 @@ export default class Bundler {
8180 }
8281
8382 packageReaderFor ( packageConfig ) {
84- if ( this . _readersMap [ packageConfig . name ] ) {
83+ if ( this . _readersMap . hasOwnProperty ( packageConfig . name ) ) {
8584 return Promise . resolve ( this . _readersMap [ packageConfig . name ] ) ;
8685 }
8786
@@ -125,45 +124,26 @@ export default class Bundler {
125124 ) ;
126125 }
127126
128- _didUserModule ( id ) {
127+ _addToDone ( id ) {
129128 if ( typeof id === 'string' ) {
130- this . _user_modules_done . add ( id ) ;
129+ this . _moduleId_done . add ( id ) ;
131130 } else if ( Array . isArray ( id ) ) {
132- id . forEach ( d => this . _user_modules_done . add ( d ) ) ;
133- }
134- }
135-
136- _didPackageModule ( id ) {
137- if ( typeof id === 'string' ) {
138- this . _package_modules_done . add ( id ) ;
139- } else if ( Array . isArray ( id ) ) {
140- id . forEach ( d => this . _package_modules_done . add ( d ) ) ;
141- }
142- }
143-
144- _addTodo ( d , isPackageSpace ) {
145- if ( this . _moduleIds_todo [ d ] ) {
146- if ( isPackageSpace ) this . _moduleIds_todo [ d ] = 2 ;
147- } else {
148- this . _moduleIds_todo [ d ] = isPackageSpace ? 2 : 1
131+ id . forEach ( d => this . _moduleId_done . add ( d ) ) ;
149132 }
150133 }
151134
152135 _capture ( tracedUnit ) {
153136 this . _unitsMap [ tracedUnit . path ] = tracedUnit ;
154137
155138 // mark as done.
156- if ( tracedUnit . packageName ) {
157- this . _didPackageModule ( tracedUnit . moduleId ) ;
158- this . _didPackageModule ( tracedUnit . defined ) ;
159- } else {
160- this . _didUserModule ( tracedUnit . moduleId ) ;
161- this . _didUserModule ( tracedUnit . defined ) ;
162- }
139+ this . _addToDone ( tracedUnit . moduleId ) ;
140+ this . _addToDone ( tracedUnit . defined ) ;
163141
164- // mark todo. beware we didn't check whether the id is in _user/package_modules_done .
142+ // mark todo. beware we didn't check whether the id is in _moduleId_done .
165143 // they will be checked during resolve phase.
166- tracedUnit . deps . forEach ( d => this . _addTodo ( d , tracedUnit . packageName ) ) ;
144+
145+ // console.log('_capture ' + tracedUnit.moduleId + ' deps ' + tracedUnit.deps);
146+ tracedUnit . deps . forEach ( d => this . _moduleIds_todo . add ( d ) ) ;
167147
168148 const bundle = this . bundleOf ( tracedUnit ) ;
169149 // mark related bundle dirty
@@ -218,7 +198,7 @@ export default class Bundler {
218198 // to some browser replacement.
219199 // e.g. readable-stream/readable -> readable-stream/readable-browser
220200 _ensureNpmAlias ( tracedUnit , id ) {
221- if ( this . _package_modules_done . has ( id ) ) return ;
201+ if ( this . _moduleId_done . has ( id ) ) return ;
222202
223203 const defined = tracedUnit . defined ;
224204 let toId ;
@@ -237,7 +217,7 @@ export default class Bundler {
237217 if ( toId !== id && toId !== tracedUnit . packageName ) {
238218 const aliasResult = alias ( id , toId ) ;
239219 mergeTransformed ( tracedUnit , aliasResult ) ;
240- this . _didPackageModule ( aliasResult . defined ) ;
220+ this . _addToDone ( aliasResult . defined ) ;
241221 }
242222 }
243223
@@ -253,31 +233,21 @@ export default class Bundler {
253233 }
254234
255235 resolve ( ) {
256- // todo is always to be resolved in package space.
257236 let todo = [ ] ;
258-
259237 return this . _supportInjectCssIfNeeded ( )
260238 . then ( ( ) => this . _resolvePrependsAndAppends ( ) )
261239 . then ( ( ) => this . _resolveExplicitDepsIfNeeded ( ) )
262240 . then ( ( ) => {
263241 const consults = [ ] ;
264- const rawTodo = JSON . parse ( JSON . stringify ( this . _moduleIds_todo ) ) ;
265- this . _moduleIds_todo = Object . create ( null ) ;
242+ const rawTodo = Array . from ( this . _moduleIds_todo ) ;
243+ this . _moduleIds_todo . clear ( ) ;
244+
266245
267- Object . keys ( rawTodo ) . forEach ( id => {
268- const isPackageSpace = rawTodo [ id ] === 2 ;
246+ // console.log('_moduleIds_todo', this._moduleIds_todo);
247+ rawTodo . forEach ( id => {
269248 const parsedId = parse ( id ) ;
270249 const possibleIds = nodejsIds ( parsedId . bareId ) ;
271-
272- const isAvailabe = possibleIds . some ( id => {
273- if ( isPackageSpace ) {
274- return this . _package_modules_done . has ( id ) ;
275- } else {
276- return this . _user_modules_done . has ( id ) || this . _package_modules_done . has ( id ) ;
277- }
278- } ) ;
279-
280- if ( isAvailabe ) return ;
250+ if ( possibleIds . some ( id => this . _moduleId_done . has ( id ) ) ) return ;
281251
282252 const j = new Promise ( resolve => {
283253 resolve ( this . _onRequire && this . _onRequire ( parsedId . bareId , parsedId ) ) ;
@@ -297,8 +267,7 @@ export default class Bundler {
297267 return this . capture ( {
298268 path : '__on_require__/' + parsedId . bareId + ( parsedId . ext ? '' : '.js' ) ,
299269 contents : result ,
300- moduleId : parsedId . bareId ,
301- packageName : isPackageSpace ? parsedId . parts [ 0 ] : ''
270+ moduleId : parsedId . bareId
302271 } ) ;
303272 }
304273
@@ -356,7 +325,7 @@ export default class Bundler {
356325 return p ;
357326 } )
358327 . then ( ( ) => {
359- if ( Object . keys ( this . _moduleIds_todo ) . length ) {
328+ if ( this . _moduleIds_todo . size ) {
360329 return this . resolve ( ) ;
361330 }
362331 } ) ;
0 commit comments