Skip to content

Commit

Permalink
Use unique placeholders for each match - fixes peerigon#54 peerigon#57
Browse files Browse the repository at this point in the history
  • Loading branch information
vseventer committed Apr 27, 2020
1 parent 3dadd60 commit 70b8cef
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/extractLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -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,
rndPlaceholderPattern: new RegExp(rndPlaceholder, "g"),
});

return rndPlaceholder;
Expand All @@ -162,16 +162,19 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) {
script.runInNewContext(sandbox);

const extractedDependencyContent = await Promise.all(
newDependencies.map(async ({absolutePath, absoluteRequest}) => {
newDependencies.map(async ({absolutePath, absoluteRequest, rndPlaceholderPattern}) => {
const src = await loadModule(absoluteRequest);

return evalModule(src, absolutePath);
return {
content: await evalModule(src, absolutePath),
rndPlaceholderPattern,
};
})
);
const contentWithPlaceholders = extractExports(sandbox.module.exports);
const extractedContent = contentWithPlaceholders.replace(
rndPlaceholderPattern,
() => extractedDependencyContent.shift()
const extractedContent = extractedDependencyContent.reduce(
(content, dependency) => content.replace(dependency.rndPlaceholderPattern, dependency.content),
contentWithPlaceholders
);

moduleCache.set(filename, extractedContent);
Expand Down

0 comments on commit 70b8cef

Please sign in to comment.