From 5f1b1dfb4f594ad92f5369a2615d8ff70b52becc Mon Sep 17 00:00:00 2001 From: Mark van Seventer Date: Tue, 26 May 2020 02:53:43 -0400 Subject: [PATCH] Use unique placeholders for each match (#83) * Use unique placeholders for each match - fixes #54 #57 #80 * Refactor pattern flow. --- src/extractLoader.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/extractLoader.js b/src/extractLoader.js index 1ceea3f..947ed37 100644 --- a/src/extractLoader.js +++ b/src/extractLoader.js @@ -94,9 +94,6 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) { } async function evalModule(src, filename) { - const rndPlaceholder = "__EXTRACT_LOADER_PLACEHOLDER__" + rndNumber() + rndNumber(); - const rndPlaceholderPattern = new RegExp(rndPlaceholder, "g"); - src = babel.transform(src, { babelrc: false, presets: [ @@ -150,9 +147,12 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) { return exports; } + const rndPlaceholder = "__EXTRACT_LOADER_PLACEHOLDER__" + rndNumber() + rndNumber(); + newDependencies.push({ absolutePath, absoluteRequest: loaders + absolutePath + query, + rndPlaceholder, }); return rndPlaceholder; @@ -169,10 +169,11 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) { }) ); const contentWithPlaceholders = extractExports(sandbox.module.exports); - const extractedContent = contentWithPlaceholders.replace( - rndPlaceholderPattern, - () => extractedDependencyContent.shift() - ); + const extractedContent = extractedDependencyContent.reduce((content, dependencyContent, idx) => { + const pattern = new RegExp(newDependencies[idx].rndPlaceholder, "g"); + + return content.replace(pattern, dependencyContent); + }, contentWithPlaceholders); moduleCache.set(filename, extractedContent);