Skip to content

Commit f0ac43a

Browse files
authored
chore: fix inline script variable name collision (#5789)
1 parent 0170977 commit f0ac43a

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

scripts/compilation/Inliner.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,6 @@ module.exports = class Inliner {
195195
continue;
196196
}
197197

198-
console.log("Rewriting", relativePath, "as index re-export stub.");
199-
200198
const depth = relativePath.split("/").length - 1;
201199
const indexRelativePath =
202200
(depth === 0
@@ -252,23 +250,24 @@ module.exports = class Inliner {
252250
const packageName = requireStatement[3].replace("/", "\\/");
253251

254252
const original = this.indexContents.match(
255-
new RegExp(`var import_${variableSuffix} = require\\(\"${packageName}\"\\);`)
253+
new RegExp(`var (import_${variableSuffix}(\d+)?) = require\\(\"${packageName}\"\\);`)
256254
);
255+
257256
if (original) {
258257
let redundancyIndex = 0;
259258
let misses = 0;
259+
const originalVariable = original[1];
260260

261261
// perform an incremental replacement instead of a global (\d+) replacement
262262
// to be safe.
263263
while (true) {
264264
const redundantRequire = `var import_${variableSuffix}${redundancyIndex} = require\\("${packageName}"\\);`;
265-
const redundantVariable = `import_${variableSuffix}${redundancyIndex}`;
265+
const redundantVariable = `import_${variableSuffix}${redundancyIndex}(\\.)`;
266266

267267
if (this.indexContents.match(new RegExp(redundantRequire))) {
268-
console.log("Replacing var", redundantVariable);
269268
this.indexContents = this.indexContents
270269
.replace(new RegExp(redundantRequire, "g"), "")
271-
.replace(new RegExp(redundantVariable, "g"), `import_${variableSuffix}`);
270+
.replace(new RegExp(redundantVariable, "g"), `${originalVariable}$1`);
272271
} else if (misses++ > 10) {
273272
break;
274273
}

0 commit comments

Comments
 (0)