Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/extractLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -147,16 +148,17 @@ function evalDependencyGraph({loaderContext, src, filename, publicPath = ""}) {
return exports;
}

const rndPlaceholder = "__EXTRACT_LOADER_PLACEHOLDER__" + rndNumber() + rndNumber();
const extractPlaceholder = "__EXTRACT_LOADER_PLACEHOLDER__" + curVmDepth + "__" + counterNumber() + "__";

newDependencies.push({
absolutePath,
absoluteRequest: loaders + absolutePath + query,
rndPlaceholder,
extractPlaceholder,
});

return rndPlaceholder;
return extractPlaceholder;
},
__EXTRACT_LOADER_PLACEHOLDER__DEPTH__: curVmDepth + 1,
});

script.runInNewContext(sandbox);
Expand All @@ -170,7 +172,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);
Expand All @@ -183,13 +185,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
Expand Down