Skip to content

chore: fix inline script variable name collision #5789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions scripts/compilation/Inliner.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,6 @@ module.exports = class Inliner {
continue;
}

console.log("Rewriting", relativePath, "as index re-export stub.");

const depth = relativePath.split("/").length - 1;
const indexRelativePath =
(depth === 0
Expand Down Expand Up @@ -252,23 +250,24 @@ module.exports = class Inliner {
const packageName = requireStatement[3].replace("/", "\\/");

const original = this.indexContents.match(
new RegExp(`var import_${variableSuffix} = require\\(\"${packageName}\"\\);`)
new RegExp(`var (import_${variableSuffix}(\d+)?) = require\\(\"${packageName}\"\\);`)
);

if (original) {
let redundancyIndex = 0;
let misses = 0;
const originalVariable = original[1];

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

if (this.indexContents.match(new RegExp(redundantRequire))) {
console.log("Replacing var", redundantVariable);
this.indexContents = this.indexContents
.replace(new RegExp(redundantRequire, "g"), "")
.replace(new RegExp(redundantVariable, "g"), `import_${variableSuffix}`);
.replace(new RegExp(redundantVariable, "g"), `${originalVariable}$1`);
} else if (misses++ > 10) {
break;
}
Expand Down