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("../util/Hash")} Hash */
|
---|
22 | /** @typedef {import("../util/runtime").RuntimeSpec} RuntimeSpec */
|
---|
23 |
|
---|
24 | class WorkerDependency extends ModuleDependency {
|
---|
25 | /**
|
---|
26 | * @param {string} request request
|
---|
27 | * @param {[number, number]} range range
|
---|
28 | */
|
---|
29 | constructor(request, range) {
|
---|
30 | super(request);
|
---|
31 | this.range = range;
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Returns list of exports referenced by this dependency
|
---|
36 | * @param {ModuleGraph} moduleGraph module graph
|
---|
37 | * @param {RuntimeSpec} runtime the runtime for which the module is analysed
|
---|
38 | * @returns {(string[] | ReferencedExport)[]} referenced exports
|
---|
39 | */
|
---|
40 | getReferencedExports(moduleGraph, runtime) {
|
---|
41 | return Dependency.NO_EXPORTS_REFERENCED;
|
---|
42 | }
|
---|
43 |
|
---|
44 | get type() {
|
---|
45 | return "new Worker()";
|
---|
46 | }
|
---|
47 |
|
---|
48 | get category() {
|
---|
49 | return "worker";
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | WorkerDependency.Template = class WorkerDependencyTemplate extends (
|
---|
54 | ModuleDependency.Template
|
---|
55 | ) {
|
---|
56 | /**
|
---|
57 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
58 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
59 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
60 | * @returns {void}
|
---|
61 | */
|
---|
62 | apply(dependency, source, templateContext) {
|
---|
63 | const { chunkGraph, moduleGraph, runtimeRequirements } = templateContext;
|
---|
64 | const dep = /** @type {WorkerDependency} */ (dependency);
|
---|
65 | const block = /** @type {AsyncDependenciesBlock} */ (
|
---|
66 | moduleGraph.getParentBlock(dependency)
|
---|
67 | );
|
---|
68 | const entrypoint = /** @type {Entrypoint} */ (
|
---|
69 | chunkGraph.getBlockChunkGroup(block)
|
---|
70 | );
|
---|
71 | const chunk = entrypoint.getEntrypointChunk();
|
---|
72 |
|
---|
73 | runtimeRequirements.add(RuntimeGlobals.publicPath);
|
---|
74 | runtimeRequirements.add(RuntimeGlobals.baseURI);
|
---|
75 | runtimeRequirements.add(RuntimeGlobals.getChunkScriptFilename);
|
---|
76 |
|
---|
77 | source.replace(
|
---|
78 | dep.range[0],
|
---|
79 | dep.range[1] - 1,
|
---|
80 | `/* worker import */ ${RuntimeGlobals.publicPath} + ${
|
---|
81 | RuntimeGlobals.getChunkScriptFilename
|
---|
82 | }(${JSON.stringify(chunk.id)}), ${RuntimeGlobals.baseURI}`
|
---|
83 | );
|
---|
84 | }
|
---|
85 | };
|
---|
86 |
|
---|
87 | makeSerializable(WorkerDependency, "webpack/lib/dependencies/WorkerDependency");
|
---|
88 |
|
---|
89 | module.exports = WorkerDependency;
|
---|