@@ -195,8 +195,6 @@ module.exports = class Inliner {
195
195
continue ;
196
196
}
197
197
198
- console . log ( "Rewriting" , relativePath , "as index re-export stub." ) ;
199
-
200
198
const depth = relativePath . split ( "/" ) . length - 1 ;
201
199
const indexRelativePath =
202
200
( depth === 0
@@ -252,23 +250,24 @@ module.exports = class Inliner {
252
250
const packageName = requireStatement [ 3 ] . replace ( "/" , "\\/" ) ;
253
251
254
252
const original = this . indexContents . match (
255
- new RegExp ( `var import_${ variableSuffix } = require\\(\"${ packageName } \"\\);` )
253
+ new RegExp ( `var ( import_${ variableSuffix } (\d+)?) = require\\(\"${ packageName } \"\\);` )
256
254
) ;
255
+
257
256
if ( original ) {
258
257
let redundancyIndex = 0 ;
259
258
let misses = 0 ;
259
+ const originalVariable = original [ 1 ] ;
260
260
261
261
// perform an incremental replacement instead of a global (\d+) replacement
262
262
// to be safe.
263
263
while ( true ) {
264
264
const redundantRequire = `var import_${ variableSuffix } ${ redundancyIndex } = require\\("${ packageName } "\\);` ;
265
- const redundantVariable = `import_${ variableSuffix } ${ redundancyIndex } ` ;
265
+ const redundantVariable = `import_${ variableSuffix } ${ redundancyIndex } (\\.) ` ;
266
266
267
267
if ( this . indexContents . match ( new RegExp ( redundantRequire ) ) ) {
268
- console . log ( "Replacing var" , redundantVariable ) ;
269
268
this . indexContents = this . indexContents
270
269
. replace ( new RegExp ( redundantRequire , "g" ) , "" )
271
- . replace ( new RegExp ( redundantVariable , "g" ) , `import_ ${ variableSuffix } ` ) ;
270
+ . replace ( new RegExp ( redundantVariable , "g" ) , `${ originalVariable } $1 ` ) ;
272
271
} else if ( misses ++ > 10 ) {
273
272
break ;
274
273
}
0 commit comments