Remove `type: 'module` from `new Worker` exprs

Fixes #12686.
This commit is contained in:
Ingvar Stepanyan 2021-02-22 16:25:24 +00:00 committed by Tobias Koppers
parent be352e86b1
commit ea2cdeb834
2 changed files with 113 additions and 102 deletions

View File

@ -25,10 +25,12 @@ class WorkerDependency extends ModuleDependency {
/** /**
* @param {string} request request * @param {string} request request
* @param {[number, number]} range range * @param {[number, number]} range range
* @param {Record<string, any> | undefined} options options
*/ */
constructor(request, range) { constructor(request, range, options) {
super(request); super(request);
this.range = range; this.range = range;
this.options = options;
} }
/** /**
@ -77,9 +79,11 @@ WorkerDependency.Template = class WorkerDependencyTemplate extends (
source.replace( source.replace(
dep.range[0], dep.range[0],
dep.range[1] - 1, dep.range[1] - 1,
`/* worker import */ ${RuntimeGlobals.publicPath} + ${ `new URL(/* worker import */ ${RuntimeGlobals.publicPath} + ${
RuntimeGlobals.getChunkScriptFilename RuntimeGlobals.getChunkScriptFilename
}(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}` }(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI})${
dep.options ? `, ${JSON.stringify(dep.options)}` : ""
}`
); );
} }
}; };

View File

@ -73,7 +73,7 @@ class WorkerPlugin {
/** /**
* @param {JavascriptParser} parser the parser * @param {JavascriptParser} parser the parser
* @param {Expression} expr expression * @param {Expression} expr expression
* @returns {[BasicEvaluatedExpression, [number, number]]} parsed * @returns {BasicEvaluatedExpression} parsed
*/ */
const parseModuleUrl = (parser, expr) => { const parseModuleUrl = (parser, expr) => {
if ( if (
@ -95,8 +95,7 @@ class WorkerPlugin {
) { ) {
return; return;
} }
const arg1Value = parser.evaluateExpression(arg1); return parser.evaluateExpression(arg1);
return [arg1Value, [arg1.range[0], arg2.range[1]]];
}; };
/** /**
@ -140,13 +139,22 @@ class WorkerPlugin {
if (expr.arguments.length === 0 || expr.arguments.length > 2) if (expr.arguments.length === 0 || expr.arguments.length > 2)
return; return;
const [arg1, arg2] = expr.arguments; const [arg1, arg2] = expr.arguments;
if (arg1.type === "SpreadElement") return; /** @type {[number, number]} */
if (arg2 && arg2.type === "SpreadElement") return; const range = [arg1.range[0], (arg2 || arg1).range[1]];
const parsedUrl = parseModuleUrl(parser, arg1); const url = parseModuleUrl(parser, arg1);
if (!parsedUrl) return; if (!url || !url.isString()) return;
const [url, range] = parsedUrl; let options;
if (url.isString()) { if (arg2) {
const options = arg2 && parseObjectLiteral(parser, 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 { const {
options: importOptions, options: importOptions,
errors: commentErrors errors: commentErrors
@ -237,14 +245,13 @@ class WorkerPlugin {
} }
}); });
block.loc = expr.loc; block.loc = expr.loc;
const dep = new WorkerDependency(url.string, range); const dep = new WorkerDependency(url.string, range, options);
dep.loc = expr.loc; dep.loc = expr.loc;
block.addDependency(dep); block.addDependency(dep);
parser.state.module.addBlock(block); parser.state.module.addBlock(block);
parser.walkExpression(expr.callee); parser.walkExpression(expr.callee);
if (arg2) parser.walkExpression(arg2); if (arg2) parser.walkExpression(arg2);
return true; return true;
}
}; };
const processItem = item => { const processItem = item => {
if (item.endsWith("()")) { if (item.endsWith("()")) {