Skip to content

Commit ea2cdeb

Browse files
RReversersokra
authored andcommitted
Remove type: 'module from new Worker exprs
Fixes #12686.
1 parent be352e8 commit ea2cdeb

File tree

2 files changed

+110
-99
lines changed

2 files changed

+110
-99
lines changed

lib/dependencies/WorkerDependency.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ class WorkerDependency extends ModuleDependency {
2525
/**
2626
* @param {string} request request
2727
* @param {[number, number]} range range
28+
* @param {Record<string, any> | undefined} options options
2829
*/
29-
constructor(request, range) {
30+
constructor(request, range, options) {
3031
super(request);
3132
this.range = range;
33+
this.options = options;
3234
}
3335

3436
/**
@@ -77,9 +79,11 @@ WorkerDependency.Template = class WorkerDependencyTemplate extends (
7779
source.replace(
7880
dep.range[0],
7981
dep.range[1] - 1,
80-
`/* worker import */ ${RuntimeGlobals.publicPath} + ${
82+
`new URL(/* worker import */ ${RuntimeGlobals.publicPath} + ${
8183
RuntimeGlobals.getChunkScriptFilename
82-
}(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}`
84+
}(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI})${
85+
dep.options ? `, ${JSON.stringify(dep.options)}` : ""
86+
}`
8387
);
8488
}
8589
};

lib/dependencies/WorkerPlugin.js

Lines changed: 103 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class WorkerPlugin {
7373
/**
7474
* @param {JavascriptParser} parser the parser
7575
* @param {Expression} expr expression
76-
* @returns {[BasicEvaluatedExpression, [number, number]]} parsed
76+
* @returns {BasicEvaluatedExpression} parsed
7777
*/
7878
const parseModuleUrl = (parser, expr) => {
7979
if (
@@ -95,8 +95,7 @@ class WorkerPlugin {
9595
) {
9696
return;
9797
}
98-
const arg1Value = parser.evaluateExpression(arg1);
99-
return [arg1Value, [arg1.range[0], arg2.range[1]]];
98+
return parser.evaluateExpression(arg1);
10099
};
101100

102101
/**
@@ -140,111 +139,119 @@ class WorkerPlugin {
140139
if (expr.arguments.length === 0 || expr.arguments.length > 2)
141140
return;
142141
const [arg1, arg2] = expr.arguments;
143-
if (arg1.type === "SpreadElement") return;
144-
if (arg2 && arg2.type === "SpreadElement") return;
145-
const parsedUrl = parseModuleUrl(parser, arg1);
146-
if (!parsedUrl) return;
147-
const [url, range] = parsedUrl;
148-
if (url.isString()) {
149-
const options = arg2 && parseObjectLiteral(parser, arg2);
150-
const {
151-
options: importOptions,
152-
errors: commentErrors
153-
} = parser.parseCommentOptions(expr.range);
142+
/** @type {[number, number]} */
143+
const range = [arg1.range[0], (arg2 || arg1).range[1]];
144+
const url = parseModuleUrl(parser, arg1);
145+
if (!url || !url.isString()) return;
146+
let options;
147+
if (arg2) {
148+
options = parseObjectLiteral(parser, arg2);
149+
if (!options) return;
150+
// Remove `type: "module"` if present.
151+
delete options.type;
152+
// If the `options` is now an empty object,
153+
// remove it from the final bundle.
154+
if (Object.keys(options).length === 0) {
155+
options = undefined;
156+
}
157+
}
158+
const {
159+
options: importOptions,
160+
errors: commentErrors
161+
} = parser.parseCommentOptions(expr.range);
154162

155-
if (commentErrors) {
156-
for (const e of commentErrors) {
157-
const { comment } = e;
158-
parser.state.module.addWarning(
159-
new CommentCompilationWarning(
160-
`Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
161-
comment.loc
162-
)
163-
);
164-
}
163+
if (commentErrors) {
164+
for (const e of commentErrors) {
165+
const { comment } = e;
166+
parser.state.module.addWarning(
167+
new CommentCompilationWarning(
168+
`Compilation error while processing magic comment(-s): /*${comment.value}*/: ${e.message}`,
169+
comment.loc
170+
)
171+
);
165172
}
173+
}
166174

167-
/** @type {EntryOptions} */
168-
let entryOptions = {};
175+
/** @type {EntryOptions} */
176+
let entryOptions = {};
169177

170-
if (importOptions) {
171-
if (importOptions.webpackIgnore !== undefined) {
172-
if (typeof importOptions.webpackIgnore !== "boolean") {
173-
parser.state.module.addWarning(
174-
new UnsupportedFeatureWarning(
175-
`\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
176-
expr.loc
177-
)
178-
);
179-
} else {
180-
if (importOptions.webpackIgnore) {
181-
return false;
182-
}
183-
}
184-
}
185-
if (importOptions.webpackEntryOptions !== undefined) {
186-
if (
187-
typeof importOptions.webpackEntryOptions !== "object" ||
188-
importOptions.webpackEntryOptions === null
189-
) {
190-
parser.state.module.addWarning(
191-
new UnsupportedFeatureWarning(
192-
`\`webpackEntryOptions\` expected a object, but received: ${importOptions.webpackEntryOptions}.`,
193-
expr.loc
194-
)
195-
);
196-
} else {
197-
Object.assign(
198-
entryOptions,
199-
importOptions.webpackEntryOptions
200-
);
178+
if (importOptions) {
179+
if (importOptions.webpackIgnore !== undefined) {
180+
if (typeof importOptions.webpackIgnore !== "boolean") {
181+
parser.state.module.addWarning(
182+
new UnsupportedFeatureWarning(
183+
`\`webpackIgnore\` expected a boolean, but received: ${importOptions.webpackIgnore}.`,
184+
expr.loc
185+
)
186+
);
187+
} else {
188+
if (importOptions.webpackIgnore) {
189+
return false;
201190
}
202191
}
203-
if (importOptions.webpackChunkName !== undefined) {
204-
if (typeof importOptions.webpackChunkName !== "string") {
205-
parser.state.module.addWarning(
206-
new UnsupportedFeatureWarning(
207-
`\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`,
208-
expr.loc
209-
)
210-
);
211-
} else {
212-
entryOptions.name = importOptions.webpackChunkName;
213-
}
192+
}
193+
if (importOptions.webpackEntryOptions !== undefined) {
194+
if (
195+
typeof importOptions.webpackEntryOptions !== "object" ||
196+
importOptions.webpackEntryOptions === null
197+
) {
198+
parser.state.module.addWarning(
199+
new UnsupportedFeatureWarning(
200+
`\`webpackEntryOptions\` expected a object, but received: ${importOptions.webpackEntryOptions}.`,
201+
expr.loc
202+
)
203+
);
204+
} else {
205+
Object.assign(
206+
entryOptions,
207+
importOptions.webpackEntryOptions
208+
);
214209
}
215210
}
216-
217-
if (
218-
!Object.prototype.hasOwnProperty.call(entryOptions, "name") &&
219-
options &&
220-
options.name
221-
) {
222-
entryOptions.name = options.name;
211+
if (importOptions.webpackChunkName !== undefined) {
212+
if (typeof importOptions.webpackChunkName !== "string") {
213+
parser.state.module.addWarning(
214+
new UnsupportedFeatureWarning(
215+
`\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`,
216+
expr.loc
217+
)
218+
);
219+
} else {
220+
entryOptions.name = importOptions.webpackChunkName;
221+
}
223222
}
223+
}
224224

225-
if (!entryOptions.runtime) {
226-
entryOptions.runtime = `${cachedContextify(
227-
parser.state.module.identifier()
228-
)}|${formatLocation(expr.loc)}`;
229-
}
225+
if (
226+
!Object.prototype.hasOwnProperty.call(entryOptions, "name") &&
227+
options &&
228+
options.name
229+
) {
230+
entryOptions.name = options.name;
231+
}
230232

231-
const block = new AsyncDependenciesBlock({
232-
name: entryOptions.name,
233-
entryOptions: {
234-
chunkLoading: this._chunkLoading,
235-
wasmLoading: this._wasmLoading,
236-
...entryOptions
237-
}
238-
});
239-
block.loc = expr.loc;
240-
const dep = new WorkerDependency(url.string, range);
241-
dep.loc = expr.loc;
242-
block.addDependency(dep);
243-
parser.state.module.addBlock(block);
244-
parser.walkExpression(expr.callee);
245-
if (arg2) parser.walkExpression(arg2);
246-
return true;
233+
if (!entryOptions.runtime) {
234+
entryOptions.runtime = `${cachedContextify(
235+
parser.state.module.identifier()
236+
)}|${formatLocation(expr.loc)}`;
247237
}
238+
239+
const block = new AsyncDependenciesBlock({
240+
name: entryOptions.name,
241+
entryOptions: {
242+
chunkLoading: this._chunkLoading,
243+
wasmLoading: this._wasmLoading,
244+
...entryOptions
245+
}
246+
});
247+
block.loc = expr.loc;
248+
const dep = new WorkerDependency(url.string, range, options);
249+
dep.loc = expr.loc;
250+
block.addDependency(dep);
251+
parser.state.module.addBlock(block);
252+
parser.walkExpression(expr.callee);
253+
if (arg2) parser.walkExpression(arg2);
254+
return true;
248255
};
249256
const processItem = item => {
250257
if (item.endsWith("()")) {

0 commit comments

Comments
 (0)