Skip to content

Commit

Permalink
Remove type: 'module from new Worker exprs
Browse files Browse the repository at this point in the history
Fixes #12686.
  • Loading branch information
RReverser authored and sokra committed Mar 12, 2021
1 parent be352e8 commit ea2cdeb
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 99 deletions.
10 changes: 7 additions & 3 deletions lib/dependencies/WorkerDependency.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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)}` : ""
}`
);
}
};
Expand Down
199 changes: 103 additions & 96 deletions lib/dependencies/WorkerPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -95,8 +95,7 @@ class WorkerPlugin {
) {
return;
}
const arg1Value = parser.evaluateExpression(arg1);
return [arg1Value, [arg1.range[0], arg2.range[1]]];
return parser.evaluateExpression(arg1);
};

/**
Expand Down Expand Up @@ -140,111 +139,119 @@ 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);
const {
options: importOptions,
errors: commentErrors
} = parser.parseCommentOptions(expr.range);
/** @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
} = parser.parseCommentOptions(expr.range);

if (commentErrors) {
for (const e of commentErrors) {
const { comment } = e;
parser.state.module.addWarning(
new CommentCompilationWarning(
`Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
comment.loc
)
);
}
if (commentErrors) {
for (const e of commentErrors) {
const { comment } = e;
parser.state.module.addWarning(
new CommentCompilationWarning(
`Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
comment.loc
)
);
}
}

/** @type {EntryOptions} */
let entryOptions = {};
/** @type {EntryOptions} */
let entryOptions = {};

if (importOptions) {
if (importOptions.webpackIgnore !== undefined) {
if (typeof importOptions.webpackIgnore !== "boolean") {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
expr.loc
)
);
} else {
if (importOptions.webpackIgnore) {
return false;
}
}
}
if (importOptions.webpackEntryOptions !== undefined) {
if (
typeof importOptions.webpackEntryOptions !== "object" ||
importOptions.webpackEntryOptions === null
) {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackEntryOptions\` expected a object, but received: ${importOptions.webpackEntryOptions}.`,
expr.loc
)
);
} else {
Object.assign(
entryOptions,
importOptions.webpackEntryOptions
);
if (importOptions) {
if (importOptions.webpackIgnore !== undefined) {
if (typeof importOptions.webpackIgnore !== "boolean") {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
expr.loc
)
);
} else {
if (importOptions.webpackIgnore) {
return false;
}
}
if (importOptions.webpackChunkName !== undefined) {
if (typeof importOptions.webpackChunkName !== "string") {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`,
expr.loc
)
);
} else {
entryOptions.name = importOptions.webpackChunkName;
}
}
if (importOptions.webpackEntryOptions !== undefined) {
if (
typeof importOptions.webpackEntryOptions !== "object" ||
importOptions.webpackEntryOptions === null
) {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackEntryOptions\` expected a object, but received: ${importOptions.webpackEntryOptions}.`,
expr.loc
)
);
} else {
Object.assign(
entryOptions,
importOptions.webpackEntryOptions
);
}
}

if (
!Object.prototype.hasOwnProperty.call(entryOptions, "name") &&
options &&
options.name
) {
entryOptions.name = options.name;
if (importOptions.webpackChunkName !== undefined) {
if (typeof importOptions.webpackChunkName !== "string") {
parser.state.module.addWarning(
new UnsupportedFeatureWarning(
`\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`,
expr.loc
)
);
} else {
entryOptions.name = importOptions.webpackChunkName;
}
}
}

if (!entryOptions.runtime) {
entryOptions.runtime = `${cachedContextify(
parser.state.module.identifier()
)}|${formatLocation(expr.loc)}`;
}
if (
!Object.prototype.hasOwnProperty.call(entryOptions, "name") &&
options &&
options.name
) {
entryOptions.name = options.name;
}

const block = new AsyncDependenciesBlock({
name: entryOptions.name,
entryOptions: {
chunkLoading: this._chunkLoading,
wasmLoading: this._wasmLoading,
...entryOptions
}
});
block.loc = expr.loc;
const dep = new WorkerDependency(url.string, range);
dep.loc = expr.loc;
block.addDependency(dep);
parser.state.module.addBlock(block);
parser.walkExpression(expr.callee);
if (arg2) parser.walkExpression(arg2);
return true;
if (!entryOptions.runtime) {
entryOptions.runtime = `${cachedContextify(
parser.state.module.identifier()
)}|${formatLocation(expr.loc)}`;
}

const block = new AsyncDependenciesBlock({
name: entryOptions.name,
entryOptions: {
chunkLoading: this._chunkLoading,
wasmLoading: this._wasmLoading,
...entryOptions
}
});
block.loc = expr.loc;
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("()")) {
Expand Down

0 comments on commit ea2cdeb

Please sign in to comment.