UNPKG

2.31 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const makeSerializable = require("../util/makeSerializable");
9const NullDependency = require("./NullDependency");
10
11/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
12/** @typedef {import("../ChunkGraph")} ChunkGraph */
13/** @typedef {import("../Dependency")} Dependency */
14/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
15/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
16/** @typedef {import("../ModuleGraph")} ModuleGraph */
17/** @typedef {import("../util/Hash")} Hash */
18
19class RuntimeRequirementsDependency extends NullDependency {
20 /**
21 * @param {string[]} runtimeRequirements runtime requirements
22 */
23 constructor(runtimeRequirements) {
24 super();
25 this.runtimeRequirements = new Set(runtimeRequirements);
26 this._hashUpdate = undefined;
27 }
28
29 /**
30 * Update the hash
31 * @param {Hash} hash hash to be updated
32 * @param {UpdateHashContext} context context
33 * @returns {void}
34 */
35 updateHash(hash, context) {
36 if (this._hashUpdate === undefined) {
37 this._hashUpdate = Array.from(this.runtimeRequirements).join() + "";
38 }
39 hash.update(this._hashUpdate);
40 }
41
42 serialize(context) {
43 const { write } = context;
44 write(this.runtimeRequirements);
45 super.serialize(context);
46 }
47
48 deserialize(context) {
49 const { read } = context;
50 this.runtimeRequirements = read();
51 super.deserialize(context);
52 }
53}
54
55makeSerializable(
56 RuntimeRequirementsDependency,
57 "webpack/lib/dependencies/RuntimeRequirementsDependency"
58);
59
60RuntimeRequirementsDependency.Template = class RuntimeRequirementsDependencyTemplate extends (
61 NullDependency.Template
62) {
63 /**
64 * @param {Dependency} dependency the dependency for which the template should be applied
65 * @param {ReplaceSource} source the current replace source which can be modified
66 * @param {DependencyTemplateContext} templateContext the context object
67 * @returns {void}
68 */
69 apply(dependency, source, { runtimeRequirements }) {
70 const dep = /** @type {RuntimeRequirementsDependency} */ (dependency);
71 for (const req of dep.runtimeRequirements) {
72 runtimeRequirements.add(req);
73 }
74 }
75};
76
77module.exports = RuntimeRequirementsDependency;