From f0ac43af9bbe6a0fd6080a013da9344ecb02abee Mon Sep 17 00:00:00 2001 From: George Fu Date: Tue, 13 Feb 2024 12:29:59 -0500 Subject: [PATCH] chore: fix inline script variable name collision (#5789) --- scripts/compilation/Inliner.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/scripts/compilation/Inliner.js b/scripts/compilation/Inliner.js index 9b4d1d3477fb..2d48448764ec 100644 --- a/scripts/compilation/Inliner.js +++ b/scripts/compilation/Inliner.js @@ -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 @@ -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; }