From e45652d8d0be69f034f0886ea650d1cbb8844d73 Mon Sep 17 00:00:00 2001 From: Isaac Levy Date: Sun, 1 Nov 2020 08:34:16 -0500 Subject: [PATCH 1/2] Don't use random placeholders --- src/extractLoader.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/extractLoader.js b/src/extractLoader.js index 947ed37..d51a5cc 100644 --- a/src/extractLoader.js +++ b/src/extractLoader.js @@ -147,15 +147,15 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) { return exports; } - const rndPlaceholder = "__EXTRACT_LOADER_PLACEHOLDER__" + rndNumber() + rndNumber(); + const extractPlaceholder = "__EXTRACT_LOADER_PLACEHOLDER__" + counterNumber() + "__"; newDependencies.push({ absolutePath, absoluteRequest: loaders + absolutePath + query, - rndPlaceholder, + extractPlaceholder, }); - return rndPlaceholder; + return extractPlaceholder; }, }); @@ -170,7 +170,7 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) { ); const contentWithPlaceholders = extractExports(sandbox.module.exports); const extractedContent = extractedDependencyContent.reduce((content, dependencyContent, idx) => { - const pattern = new RegExp(newDependencies[idx].rndPlaceholder, "g"); + const pattern = new RegExp(newDependencies[idx].extractPlaceholder, "g"); return content.replace(pattern, dependencyContent); }, contentWithPlaceholders); @@ -183,13 +183,13 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) { return evalModule(src, filename); } +let counter = 0; + /** * @returns {string} */ -function rndNumber() { - return Math.random() - .toString() - .slice(2); +function counterNumber() { + return (counter++).toString(10); } // getPublicPath() encapsulates the complexity of reading the publicPath from the current From e70047d5cd3774fd16c5d9d0ff61fdc302c85760 Mon Sep 17 00:00:00 2001 From: Isaac Levy Date: Sun, 1 Nov 2020 18:03:01 -0500 Subject: [PATCH 2/2] Add depth counter to avoid recursive overlap --- src/extractLoader.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/extractLoader.js b/src/extractLoader.js index d51a5cc..677388d 100644 --- a/src/extractLoader.js +++ b/src/extractLoader.js @@ -107,6 +107,7 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) { plugins: [require("babel-plugin-add-module-exports")], }).code; + const curVmDepth = global.__EXTRACT_LOADER_PLACEHOLDER__DEPTH__ || 0; const script = new vm.Script(src, { filename, displayErrors: true, @@ -147,7 +148,7 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) { return exports; } - const extractPlaceholder = "__EXTRACT_LOADER_PLACEHOLDER__" + counterNumber() + "__"; + const extractPlaceholder = "__EXTRACT_LOADER_PLACEHOLDER__" + curVmDepth + "__" + counterNumber() + "__"; newDependencies.push({ absolutePath, @@ -157,6 +158,7 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) { return extractPlaceholder; }, + __EXTRACT_LOADER_PLACEHOLDER__DEPTH__: curVmDepth + 1, }); script.runInNewContext(sandbox);