Skip to content

Commit

Permalink
Use unique placeholders for each match (#83)
Browse files Browse the repository at this point in the history
* Use unique placeholders for each match - fixes #54 #57 #80

* Refactor pattern flow.
  • Loading branch information
vseventer authored May 26, 2020
1 parent 3dadd60 commit 5f1b1df
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 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,
rndPlaceholder,
});

return rndPlaceholder;
Expand All @@ -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);

Expand Down

0 comments on commit 5f1b1df

Please sign in to comment.