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