UNPKG

3.22 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("../ModuleGraphConnection").ConnectionState} ConnectionState */
18/** @typedef {import("../util/Hash")} Hash */
19
20class ConstDependency extends NullDependency {
21 /**
22 * @param {string} expression the expression
23 * @param {number|[number, number]} range the source range
24 * @param {string[]=} runtimeRequirements runtime requirements
25 */
26 constructor(expression, range, runtimeRequirements) {
27 super();
28 this.expression = expression;
29 this.range = range;
30 this.runtimeRequirements = runtimeRequirements
31 ? new Set(runtimeRequirements)
32 : null;
33 this._hashUpdate = undefined;
34 }
35
36 /**
37 * Update the hash
38 * @param {Hash} hash hash to be updated
39 * @param {UpdateHashContext} context context
40 * @returns {void}
41 */
42 updateHash(hash, context) {
43 if (this._hashUpdate === undefined) {
44 let hashUpdate = "" + this.range + "|" + this.expression;
45 if (this.runtimeRequirements) {
46 for (const item of this.runtimeRequirements) {
47 hashUpdate += "|";
48 hashUpdate += item;
49 }
50 }
51 this._hashUpdate = hashUpdate;
52 }
53 hash.update(this._hashUpdate);
54 }
55
56 /**
57 * @param {ModuleGraph} moduleGraph the module graph
58 * @returns {ConnectionState} how this dependency connects the module to referencing modules
59 */
60 getModuleEvaluationSideEffectsState(moduleGraph) {
61 return false;
62 }
63
64 serialize(context) {
65 const { write } = context;
66 write(this.expression);
67 write(this.range);
68 write(this.runtimeRequirements);
69 super.serialize(context);
70 }
71
72 deserialize(context) {
73 const { read } = context;
74 this.expression = read();
75 this.range = read();
76 this.runtimeRequirements = read();
77 super.deserialize(context);
78 }
79}
80
81makeSerializable(ConstDependency, "webpack/lib/dependencies/ConstDependency");
82
83ConstDependency.Template = class ConstDependencyTemplate extends (
84 NullDependency.Template
85) {
86 /**
87 * @param {Dependency} dependency the dependency for which the template should be applied
88 * @param {ReplaceSource} source the current replace source which can be modified
89 * @param {DependencyTemplateContext} templateContext the context object
90 * @returns {void}
91 */
92 apply(dependency, source, templateContext) {
93 const dep = /** @type {ConstDependency} */ (dependency);
94 if (dep.runtimeRequirements) {
95 for (const req of dep.runtimeRequirements) {
96 templateContext.runtimeRequirements.add(req);
97 }
98 }
99 if (typeof dep.range === "number") {
100 source.insert(dep.range, dep.expression);
101 return;
102 }
103
104 source.replace(dep.range[0], dep.range[1] - 1, dep.expression);
105 }
106};
107
108module.exports = ConstDependency;