mirror of https://github.com/webpack/webpack.git
parent
be352e86b1
commit
ea2cdeb834
|
@ -25,10 +25,12 @@ class WorkerDependency extends ModuleDependency {
|
|||
/**
|
||||
* @param {string} request request
|
||||
* @param {[number, number]} range range
|
||||
* @param {Record<string, any> | undefined} options options
|
||||
*/
|
||||
constructor(request, range) {
|
||||
constructor(request, range, options) {
|
||||
super(request);
|
||||
this.range = range;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -77,9 +79,11 @@ WorkerDependency.Template = class WorkerDependencyTemplate extends (
|
|||
source.replace(
|
||||
dep.range[0],
|
||||
dep.range[1] - 1,
|
||||
`/* worker import */ ${RuntimeGlobals.publicPath} + ${
|
||||
`new URL(/* worker import */ ${RuntimeGlobals.publicPath} + ${
|
||||
RuntimeGlobals.getChunkScriptFilename
|
||||
}(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}`
|
||||
}(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI})${
|
||||
dep.options ? `, ${JSON.stringify(dep.options)}` : ""
|
||||
}`
|
||||
);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -73,7 +73,7 @@ class WorkerPlugin {
|
|||
/**
|
||||
* @param {JavascriptParser} parser the parser
|
||||
* @param {Expression} expr expression
|
||||
* @returns {[BasicEvaluatedExpression, [number, number]]} parsed
|
||||
* @returns {BasicEvaluatedExpression} parsed
|
||||
*/
|
||||
const parseModuleUrl = (parser, expr) => {
|
||||
if (
|
||||
|
@ -95,8 +95,7 @@ class WorkerPlugin {
|
|||
) {
|
||||
return;
|
||||
}
|
||||
const arg1Value = parser.evaluateExpression(arg1);
|
||||
return [arg1Value, [arg1.range[0], arg2.range[1]]];
|
||||
return parser.evaluateExpression(arg1);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -140,13 +139,22 @@ class WorkerPlugin {
|
|||
if (expr.arguments.length === 0 || expr.arguments.length > 2)
|
||||
return;
|
||||
const [arg1, arg2] = expr.arguments;
|
||||
if (arg1.type === "SpreadElement") return;
|
||||
if (arg2 && arg2.type === "SpreadElement") return;
|
||||
const parsedUrl = parseModuleUrl(parser, arg1);
|
||||
if (!parsedUrl) return;
|
||||
const [url, range] = parsedUrl;
|
||||
if (url.isString()) {
|
||||
const options = arg2 && parseObjectLiteral(parser, arg2);
|
||||
/** @type {[number, number]} */
|
||||
const range = [arg1.range[0], (arg2 || arg1).range[1]];
|
||||
const url = parseModuleUrl(parser, arg1);
|
||||
if (!url || !url.isString()) return;
|
||||
let options;
|
||||
if (arg2) {
|
||||
options = parseObjectLiteral(parser, arg2);
|
||||
if (!options) return;
|
||||
// Remove `type: "module"` if present.
|
||||
delete options.type;
|
||||
// If the `options` is now an empty object,
|
||||
// remove it from the final bundle.
|
||||
if (Object.keys(options).length === 0) {
|
||||
options = undefined;
|
||||
}
|
||||
}
|
||||
const {
|
||||
options: importOptions,
|
||||
errors: commentErrors
|
||||
|
@ -237,14 +245,13 @@ class WorkerPlugin {
|
|||
}
|
||||
});
|
||||
block.loc = expr.loc;
|
||||
const dep = new WorkerDependency(url.string, range);
|
||||
const dep = new WorkerDependency(url.string, range, options);
|
||||
dep.loc = expr.loc;
|
||||
block.addDependency(dep);
|
||||
parser.state.module.addBlock(block);
|
||||
parser.walkExpression(expr.callee);
|
||||
if (arg2) parser.walkExpression(arg2);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
const processItem = item => {
|
||||
if (item.endsWith("()")) {
|
||||
|
|
Loading…
Reference in New Issue