[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Ivan Kopeykin @vankop
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const Dependency = require("../Dependency");
|
---|
| 9 | const RuntimeGlobals = require("../RuntimeGlobals");
|
---|
| 10 | const makeSerializable = require("../util/makeSerializable");
|
---|
| 11 | const ModuleDependency = require("./ModuleDependency");
|
---|
| 12 |
|
---|
| 13 | /** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
---|
| 14 | /** @typedef {import("../AsyncDependenciesBlock")} AsyncDependenciesBlock */
|
---|
| 15 | /** @typedef {import("../ChunkGraph")} ChunkGraph */
|
---|
| 16 | /** @typedef {import("../Dependency").ReferencedExport} ReferencedExport */
|
---|
| 17 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
---|
| 18 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
---|
| 19 | /** @typedef {import("../Entrypoint")} Entrypoint */
|
---|
| 20 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
---|
| 21 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
---|
| 22 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 23 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 24 | /** @typedef {import("../util/Hash")} Hash */
|
---|
| 25 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
| 26 |
|
---|
| 27 | class WorkerDependency extends ModuleDependency {
|
---|
| 28 | /**
|
---|
| 29 | * @param {string} request request
|
---|
| 30 | * @param {Range} range range
|
---|
| 31 | * @param {object} workerDependencyOptions options
|
---|
| 32 | * @param {string=} workerDependencyOptions.publicPath public path for the worker
|
---|
| 33 | */
|
---|
| 34 | constructor(request, range, workerDependencyOptions) {
|
---|
| 35 | super(request);
|
---|
| 36 | this.range = range;
|
---|
| 37 | // If options are updated, don't forget to update the hash and serialization functions
|
---|
| 38 | this.options = workerDependencyOptions;
|
---|
| 39 | /** Cache the hash */
|
---|
| 40 | this._hashUpdate = undefined;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * Returns list of exports referenced by this dependency
|
---|
| 45 | * @param {ModuleGraph} moduleGraph module graph
|
---|
| 46 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
---|
| 47 | * @returns {(string[] | ReferencedExport)[]} referenced exports
|
---|
| 48 | */
|
---|
| 49 | getReferencedExports(moduleGraph, runtime) {
|
---|
| 50 | return Dependency.NO_EXPORTS_REFERENCED;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | get type() {
|
---|
| 54 | return "new Worker()";
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | get category() {
|
---|
| 58 | return "worker";
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * Update the hash
|
---|
| 63 | * @param {Hash} hash hash to be updated
|
---|
| 64 | * @param {UpdateHashContext} context context
|
---|
| 65 | * @returns {void}
|
---|
| 66 | */
|
---|
| 67 | updateHash(hash, context) {
|
---|
| 68 | if (this._hashUpdate === undefined) {
|
---|
| 69 | this._hashUpdate = JSON.stringify(this.options);
|
---|
| 70 | }
|
---|
| 71 | hash.update(this._hashUpdate);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * @param {ObjectSerializerContext} context context
|
---|
| 76 | */
|
---|
| 77 | serialize(context) {
|
---|
| 78 | const { write } = context;
|
---|
| 79 | write(this.options);
|
---|
| 80 | super.serialize(context);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * @param {ObjectDeserializerContext} context context
|
---|
| 85 | */
|
---|
| 86 | deserialize(context) {
|
---|
| 87 | const { read } = context;
|
---|
| 88 | this.options = read();
|
---|
| 89 | super.deserialize(context);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | WorkerDependency.Template = class WorkerDependencyTemplate extends (
|
---|
| 94 | ModuleDependency.Template
|
---|
| 95 | ) {
|
---|
| 96 | /**
|
---|
| 97 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 98 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 99 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 100 | * @returns {void}
|
---|
| 101 | */
|
---|
| 102 | apply(dependency, source, templateContext) {
|
---|
| 103 | const { chunkGraph, moduleGraph, runtimeRequirements } = templateContext;
|
---|
| 104 | const dep = /** @type {WorkerDependency} */ (dependency);
|
---|
| 105 | const block = /** @type {AsyncDependenciesBlock} */ (
|
---|
| 106 | moduleGraph.getParentBlock(dependency)
|
---|
| 107 | );
|
---|
| 108 | const entrypoint = /** @type {Entrypoint} */ (
|
---|
| 109 | chunkGraph.getBlockChunkGroup(block)
|
---|
| 110 | );
|
---|
| 111 | const chunk = entrypoint.getEntrypointChunk();
|
---|
| 112 | // We use the workerPublicPath option if provided, else we fallback to the RuntimeGlobal publicPath
|
---|
| 113 | const workerImportBaseUrl = dep.options.publicPath
|
---|
| 114 | ? `"${dep.options.publicPath}"`
|
---|
| 115 | : RuntimeGlobals.publicPath;
|
---|
| 116 |
|
---|
| 117 | runtimeRequirements.add(RuntimeGlobals.publicPath);
|
---|
| 118 | runtimeRequirements.add(RuntimeGlobals.baseURI);
|
---|
| 119 | runtimeRequirements.add(RuntimeGlobals.getChunkScriptFilename);
|
---|
| 120 |
|
---|
| 121 | source.replace(
|
---|
| 122 | dep.range[0],
|
---|
| 123 | dep.range[1] - 1,
|
---|
| 124 | `/* worker import */ ${workerImportBaseUrl} + ${
|
---|
| 125 | RuntimeGlobals.getChunkScriptFilename
|
---|
| 126 | }(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}`
|
---|
| 127 | );
|
---|
| 128 | }
|
---|
| 129 | };
|
---|
| 130 |
|
---|
| 131 | makeSerializable(WorkerDependency, "webpack/lib/dependencies/WorkerDependency");
|
---|
| 132 |
|
---|
| 133 | module.exports = WorkerDependency;
|
---|