| 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("../Dependency").ReferencedExports} ReferencedExports */
|
|---|
| 16 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
|---|
| 17 | /** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
|---|
| 18 | /** @typedef {import("../Entrypoint")} Entrypoint */
|
|---|
| 19 | /** @typedef {import("../ModuleGraph")} ModuleGraph */
|
|---|
| 20 | /** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
|---|
| 21 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 22 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 23 | /** @typedef {import("../util/Hash")} Hash */
|
|---|
| 24 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Represents the worker dependency runtime component.
|
|---|
| 28 | * @typedef {object} WorkerDependencyOptions
|
|---|
| 29 | * @property {string=} publicPath public path for the worker
|
|---|
| 30 | * @property {boolean=} needNewUrl true when need generate `new URL(...)`, otherwise false
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | class WorkerDependency extends ModuleDependency {
|
|---|
| 34 | /**
|
|---|
| 35 | * Creates an instance of WorkerDependency.
|
|---|
| 36 | * @param {string} request request
|
|---|
| 37 | * @param {Range} range range
|
|---|
| 38 | * @param {WorkerDependencyOptions} workerDependencyOptions options
|
|---|
| 39 | */
|
|---|
| 40 | constructor(request, range, workerDependencyOptions) {
|
|---|
| 41 | super(request);
|
|---|
| 42 | this.range = range;
|
|---|
| 43 | // If options are updated, don't forget to update the hash and serialization functions
|
|---|
| 44 | /** @type {WorkerDependencyOptions} */
|
|---|
| 45 | this.options = workerDependencyOptions;
|
|---|
| 46 | /** Cache the hash */
|
|---|
| 47 | /** @type {undefined | string} */
|
|---|
| 48 | this._hashUpdate = undefined;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /**
|
|---|
| 52 | * Returns list of exports referenced by this dependency
|
|---|
| 53 | * @param {ModuleGraph} moduleGraph module graph
|
|---|
| 54 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
|---|
| 55 | * @returns {ReferencedExports} referenced exports
|
|---|
| 56 | */
|
|---|
| 57 | getReferencedExports(moduleGraph, runtime) {
|
|---|
| 58 | return Dependency.NO_EXPORTS_REFERENCED;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | get type() {
|
|---|
| 62 | return "new Worker()";
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | get category() {
|
|---|
| 66 | return "worker";
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Updates the hash with the data contributed by this instance.
|
|---|
| 71 | * @param {Hash} hash hash to be updated
|
|---|
| 72 | * @param {UpdateHashContext} context context
|
|---|
| 73 | * @returns {void}
|
|---|
| 74 | */
|
|---|
| 75 | updateHash(hash, context) {
|
|---|
| 76 | if (this._hashUpdate === undefined) {
|
|---|
| 77 | this._hashUpdate = JSON.stringify(this.options);
|
|---|
| 78 | }
|
|---|
| 79 | hash.update(this._hashUpdate);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /**
|
|---|
| 83 | * Serializes this instance into the provided serializer context.
|
|---|
| 84 | * @param {ObjectSerializerContext} context context
|
|---|
| 85 | */
|
|---|
| 86 | serialize(context) {
|
|---|
| 87 | const { write } = context;
|
|---|
| 88 | write(this.options);
|
|---|
| 89 | super.serialize(context);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /**
|
|---|
| 93 | * Restores this instance from the provided deserializer context.
|
|---|
| 94 | * @param {ObjectDeserializerContext} context context
|
|---|
| 95 | */
|
|---|
| 96 | deserialize(context) {
|
|---|
| 97 | const { read } = context;
|
|---|
| 98 | this.options = read();
|
|---|
| 99 | super.deserialize(context);
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | WorkerDependency.Template = class WorkerDependencyTemplate extends (
|
|---|
| 104 | ModuleDependency.Template
|
|---|
| 105 | ) {
|
|---|
| 106 | /**
|
|---|
| 107 | * Applies the plugin by registering its hooks on the compiler.
|
|---|
| 108 | * @param {Dependency} dependency the dependency for which the template should be applied
|
|---|
| 109 | * @param {ReplaceSource} source the current replace source which can be modified
|
|---|
| 110 | * @param {DependencyTemplateContext} templateContext the context object
|
|---|
| 111 | * @returns {void}
|
|---|
| 112 | */
|
|---|
| 113 | apply(dependency, source, templateContext) {
|
|---|
| 114 | const { chunkGraph, moduleGraph, runtimeRequirements } = templateContext;
|
|---|
| 115 | const dep = /** @type {WorkerDependency} */ (dependency);
|
|---|
| 116 | const block = /** @type {AsyncDependenciesBlock} */ (
|
|---|
| 117 | moduleGraph.getParentBlock(dependency)
|
|---|
| 118 | );
|
|---|
| 119 | const entrypoint = /** @type {Entrypoint} */ (
|
|---|
| 120 | chunkGraph.getBlockChunkGroup(block)
|
|---|
| 121 | );
|
|---|
| 122 | const chunk = entrypoint.getEntrypointChunk();
|
|---|
| 123 | // We use the workerPublicPath option if provided, else we fallback to the RuntimeGlobal publicPath
|
|---|
| 124 | const workerImportBaseUrl = dep.options.publicPath
|
|---|
| 125 | ? `"${dep.options.publicPath}"`
|
|---|
| 126 | : RuntimeGlobals.publicPath;
|
|---|
| 127 |
|
|---|
| 128 | runtimeRequirements.add(RuntimeGlobals.publicPath);
|
|---|
| 129 | runtimeRequirements.add(RuntimeGlobals.baseURI);
|
|---|
| 130 | runtimeRequirements.add(RuntimeGlobals.getChunkScriptFilename);
|
|---|
| 131 |
|
|---|
| 132 | const workerImportStr = `/* worker import */ ${workerImportBaseUrl} + ${
|
|---|
| 133 | RuntimeGlobals.getChunkScriptFilename
|
|---|
| 134 | }(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}`;
|
|---|
| 135 |
|
|---|
| 136 | source.replace(
|
|---|
| 137 | dep.range[0],
|
|---|
| 138 | dep.range[1] - 1,
|
|---|
| 139 | dep.options.needNewUrl ? `new URL(${workerImportStr})` : workerImportStr
|
|---|
| 140 | );
|
|---|
| 141 | }
|
|---|
| 142 | };
|
|---|
| 143 |
|
|---|
| 144 | makeSerializable(WorkerDependency, "webpack/lib/dependencies/WorkerDependency");
|
|---|
| 145 |
|
|---|
| 146 | module.exports = WorkerDependency;
|
|---|