Skip to content

Commit eac5d8c

Browse files
authored
Merge pull request #16671 from thomastay/main
Add a new output option, workerPublicPath
2 parents 8ac9616 + 8c6a1a4 commit eac5d8c

21 files changed

Lines changed: 171 additions & 10 deletions

declarations/WebpackOptions.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,10 @@ export type UniqueName = string;
569569
* The filename of WebAssembly modules as relative path inside the 'output.path' directory.
570570
*/
571571
export type WebassemblyModuleFilename = string;
572+
/**
573+
* Worker public path. Much like the public path, this sets the location where the worker script file is intended to be found. If not set, webpack will use the publicPath. Don't set this option unless your worker scripts are located at a different path from your other script files.
574+
*/
575+
export type WorkerPublicPath = string;
572576
/**
573577
* The number of parallel processed modules in the compilation.
574578
*/
@@ -2164,6 +2168,10 @@ export interface Output {
21642168
* The method of loading chunks (methods included by default are 'jsonp' (web), 'import' (ESM), 'importScripts' (WebWorker), 'require' (sync node.js), 'async-node' (async node.js), but others might be added by plugins).
21652169
*/
21662170
workerChunkLoading?: ChunkLoading;
2171+
/**
2172+
* Worker public path. Much like the public path, this sets the location where the worker script file is intended to be found. If not set, webpack will use the publicPath. Don't set this option unless your worker scripts are located at a different path from your other script files.
2173+
*/
2174+
workerPublicPath?: WorkerPublicPath;
21672175
/**
21682176
* The method of loading WebAssembly Modules (methods included by default are 'fetch' (web/WebWorker), 'async-node' (node.js), but others might be added by plugins).
21692177
*/
@@ -3349,6 +3357,10 @@ export interface OutputNormalized {
33493357
* The method of loading chunks (methods included by default are 'jsonp' (web), 'import' (ESM), 'importScripts' (WebWorker), 'require' (sync node.js), 'async-node' (async node.js), but others might be added by plugins).
33503358
*/
33513359
workerChunkLoading?: ChunkLoading;
3360+
/**
3361+
* Worker public path. Much like the public path, this sets the location where the worker script file is intended to be found. If not set, webpack will use the publicPath. Don't set this option unless your worker scripts are located at a different path from your other script files.
3362+
*/
3363+
workerPublicPath?: WorkerPublicPath;
33523364
/**
33533365
* The method of loading WebAssembly Modules (methods included by default are 'fetch' (web/WebWorker), 'async-node' (node.js), but others might be added by plugins).
33543366
*/

lib/WebpackOptionsApply.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,8 @@ class WebpackOptionsApply extends OptionsApply {
391391
new WorkerPlugin(
392392
options.output.workerChunkLoading,
393393
options.output.workerWasmLoading,
394-
options.output.module
394+
options.output.module,
395+
options.output.workerPublicPath
395396
).apply(compiler);
396397

397398
new DefaultStatsFactoryPlugin().apply(compiler);

lib/config/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ const applyOutputDefaults = (
916916
? "auto"
917917
: ""
918918
);
919+
D(output, "workerPublicPath", "");
919920
D(output, "chunkLoadTimeout", 120000);
920921
D(output, "hashFunction", futureDefaults ? "xxhash64" : "md4");
921922
D(output, "hashDigest", "hex");

lib/config/normalization.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ const getNormalizedWebpackOptions = config => {
369369
uniqueName: output.uniqueName,
370370
wasmLoading: output.wasmLoading,
371371
webassemblyModuleFilename: output.webassemblyModuleFilename,
372+
workerPublicPath: output.workerPublicPath,
372373
workerChunkLoading: output.workerChunkLoading,
373374
workerWasmLoading: output.workerWasmLoading
374375
};

lib/dependencies/WorkerDependency.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ class WorkerDependency extends ModuleDependency {
2525
/**
2626
* @param {string} request request
2727
* @param {[number, number]} range range
28+
* @param {Object} workerDependencyOptions options
29+
* @param {string} workerDependencyOptions.publicPath public path for the worker
2830
*/
29-
constructor(request, range) {
31+
constructor(request, range, workerDependencyOptions) {
3032
super(request);
3133
this.range = range;
34+
// If options are updated, don't forget to update the hash and serialization functions
35+
this.options = workerDependencyOptions;
36+
/** Cache the hash */
37+
this._hashUpdate = undefined;
3238
}
3339

3440
/**
@@ -48,6 +54,31 @@ class WorkerDependency extends ModuleDependency {
4854
get category() {
4955
return "worker";
5056
}
57+
58+
/**
59+
* Update the hash
60+
* @param {Hash} hash hash to be updated
61+
* @param {UpdateHashContext} context context
62+
* @returns {void}
63+
*/
64+
updateHash(hash, context) {
65+
if (this._hashUpdate === undefined) {
66+
this._hashUpdate = JSON.stringify(this.options);
67+
}
68+
hash.update(this._hashUpdate);
69+
}
70+
71+
serialize(context) {
72+
const { write } = context;
73+
write(this.options);
74+
super.serialize(context);
75+
}
76+
77+
deserialize(context) {
78+
const { read } = context;
79+
this.options = read();
80+
super.deserialize(context);
81+
}
5182
}
5283

5384
WorkerDependency.Template = class WorkerDependencyTemplate extends (
@@ -69,6 +100,10 @@ WorkerDependency.Template = class WorkerDependencyTemplate extends (
69100
chunkGraph.getBlockChunkGroup(block)
70101
);
71102
const chunk = entrypoint.getEntrypointChunk();
103+
// We use the workerPublicPath option if provided, else we fallback to the RuntimeGlobal publicPath
104+
const workerImportBaseUrl = dep.options.publicPath
105+
? `"${dep.options.publicPath}"`
106+
: RuntimeGlobals.publicPath;
72107

73108
runtimeRequirements.add(RuntimeGlobals.publicPath);
74109
runtimeRequirements.add(RuntimeGlobals.baseURI);
@@ -77,7 +112,7 @@ WorkerDependency.Template = class WorkerDependencyTemplate extends (
77112
source.replace(
78113
dep.range[0],
79114
dep.range[1] - 1,
80-
`/* worker import */ ${RuntimeGlobals.publicPath} + ${
115+
`/* worker import */ ${workerImportBaseUrl} + ${
81116
RuntimeGlobals.getChunkScriptFilename
82117
}(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}`
83118
);

lib/dependencies/WorkerPlugin.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ const DEFAULT_SYNTAX = [
4848
const workerIndexMap = new WeakMap();
4949

5050
class WorkerPlugin {
51-
constructor(chunkLoading, wasmLoading, module) {
51+
constructor(chunkLoading, wasmLoading, module, workerPublicPath) {
5252
this._chunkLoading = chunkLoading;
5353
this._wasmLoading = wasmLoading;
5454
this._module = module;
55+
this._workerPublicPath = workerPublicPath;
5556
}
5657
/**
5758
* Apply the plugin
@@ -298,7 +299,9 @@ class WorkerPlugin {
298299
}
299300
});
300301
block.loc = expr.loc;
301-
const dep = new WorkerDependency(url.string, range);
302+
const dep = new WorkerDependency(url.string, range, {
303+
publicPath: this._workerPublicPath
304+
});
302305
dep.loc = expr.loc;
303306
block.addDependency(dep);
304307
parser.state.module.addBlock(block);

schemas/WebpackOptions.check.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

schemas/WebpackOptions.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,6 +3241,9 @@
32413241
"workerChunkLoading": {
32423242
"$ref": "#/definitions/ChunkLoading"
32433243
},
3244+
"workerPublicPath": {
3245+
"$ref": "#/definitions/WorkerPublicPath"
3246+
},
32443247
"workerWasmLoading": {
32453248
"$ref": "#/definitions/WasmLoading"
32463249
}
@@ -3397,6 +3400,9 @@
33973400
"workerChunkLoading": {
33983401
"$ref": "#/definitions/ChunkLoading"
33993402
},
3403+
"workerPublicPath": {
3404+
"$ref": "#/definitions/WorkerPublicPath"
3405+
},
34003406
"workerWasmLoading": {
34013407
"$ref": "#/definitions/WasmLoading"
34023408
}
@@ -5271,6 +5277,10 @@
52715277
}
52725278
},
52735279
"required": ["apply"]
5280+
},
5281+
"WorkerPublicPath": {
5282+
"description": "Worker public path. Much like the public path, this sets the location where the worker script file is intended to be found. If not set, webpack will use the publicPath. Don't set this option unless your worker scripts are located at a different path from your other script files.",
5283+
"type": "string"
52745284
}
52755285
},
52765286
"title": "WebpackOptions",

test/Defaults.unittest.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ describe("snapshots", () => {
360360
"wasmLoading": "fetch",
361361
"webassemblyModuleFilename": "[hash].module.wasm",
362362
"workerChunkLoading": "import-scripts",
363+
"workerPublicPath": "",
363364
"workerWasmLoading": "fetch",
364365
},
365366
"parallelism": 100,
@@ -1303,8 +1304,9 @@ describe("snapshots", () => {
13031304
+ "wasmLoading": "async-node",
13041305
@@ ... @@
13051306
- "workerChunkLoading": "import-scripts",
1306-
- "workerWasmLoading": "fetch",
13071307
+ "workerChunkLoading": "require",
1308+
@@ ... @@
1309+
- "workerWasmLoading": "fetch",
13081310
+ "workerWasmLoading": "async-node",
13091311
@@ ... @@
13101312
- "aliasFields": Array [
@@ -1447,8 +1449,9 @@ describe("snapshots", () => {
14471449
+ "wasmLoading": "async-node",
14481450
@@ ... @@
14491451
- "workerChunkLoading": "import-scripts",
1450-
- "workerWasmLoading": "fetch",
14511452
+ "workerChunkLoading": "require",
1453+
@@ ... @@
1454+
- "workerWasmLoading": "fetch",
14521455
+ "workerWasmLoading": "async-node",
14531456
@@ ... @@
14541457
- "aliasFields": Array [
@@ -1573,8 +1576,9 @@ describe("snapshots", () => {
15731576
+ "wasmLoading": "async-node",
15741577
@@ ... @@
15751578
- "workerChunkLoading": "import-scripts",
1576-
- "workerWasmLoading": "fetch",
15771579
+ "workerChunkLoading": "require",
1580+
@@ ... @@
1581+
- "workerWasmLoading": "fetch",
15781582
+ "workerWasmLoading": "async-node",
15791583
@@ ... @@
15801584
- "aliasFields": Array [

test/Validation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ describe("Validation", () => {
498498
expect(msg).toMatchInlineSnapshot(`
499499
"Invalid configuration object. Webpack has been initialized using a configuration object that does not match the API schema.
500500
- configuration.output has an unknown property 'ecmaVersion'. These properties are valid:
501-
object { assetModuleFilename?, asyncChunks?, auxiliaryComment?, charset?, chunkFilename?, chunkFormat?, chunkLoadTimeout?, chunkLoading?, chunkLoadingGlobal?, clean?, compareBeforeEmit?, crossOriginLoading?, cssChunkFilename?, cssFilename?, devtoolFallbackModuleFilenameTemplate?, devtoolModuleFilenameTemplate?, devtoolNamespace?, enabledChunkLoadingTypes?, enabledLibraryTypes?, enabledWasmLoadingTypes?, environment?, filename?, globalObject?, hashDigest?, hashDigestLength?, hashFunction?, hashSalt?, hotUpdateChunkFilename?, hotUpdateGlobal?, hotUpdateMainFilename?, iife?, importFunctionName?, importMetaName?, library?, libraryExport?, libraryTarget?, module?, path?, pathinfo?, publicPath?, scriptType?, sourceMapFilename?, sourcePrefix?, strictModuleErrorHandling?, strictModuleExceptionHandling?, trustedTypes?, umdNamedDefine?, uniqueName?, wasmLoading?, webassemblyModuleFilename?, workerChunkLoading?, workerWasmLoading? }
501+
object { assetModuleFilename?, asyncChunks?, auxiliaryComment?, charset?, chunkFilename?, chunkFormat?, chunkLoadTimeout?, chunkLoading?, chunkLoadingGlobal?, clean?, compareBeforeEmit?, crossOriginLoading?, cssChunkFilename?, cssFilename?, devtoolFallbackModuleFilenameTemplate?, devtoolModuleFilenameTemplate?, devtoolNamespace?, enabledChunkLoadingTypes?, enabledLibraryTypes?, enabledWasmLoadingTypes?, environment?, filename?, globalObject?, hashDigest?, hashDigestLength?, hashFunction?, hashSalt?, hotUpdateChunkFilename?, hotUpdateGlobal?, hotUpdateMainFilename?, iife?, importFunctionName?, importMetaName?, library?, libraryExport?, libraryTarget?, module?, path?, pathinfo?, publicPath?, scriptType?, sourceMapFilename?, sourcePrefix?, strictModuleErrorHandling?, strictModuleExceptionHandling?, trustedTypes?, umdNamedDefine?, uniqueName?, wasmLoading?, webassemblyModuleFilename?, workerChunkLoading?, workerPublicPath?, workerWasmLoading? }
502502
-> Options affecting the output of the compilation. \`output\` options tell webpack how to write the compiled files to disk.
503503
Did you mean output.environment (output.ecmaVersion was a temporary configuration option during webpack 5 beta)?"
504504
`)

0 commit comments

Comments
 (0)