1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const makeSerializable = require("../util/makeSerializable");
|
---|
9 | const 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 |
|
---|
20 | class 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 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Update the hash
|
---|
37 | * @param {Hash} hash hash to be updated
|
---|
38 | * @param {UpdateHashContext} context context
|
---|
39 | * @returns {void}
|
---|
40 | */
|
---|
41 | updateHash(hash, context) {
|
---|
42 | hash.update(this.range + "");
|
---|
43 | hash.update(this.expression + "");
|
---|
44 | if (this.runtimeRequirements)
|
---|
45 | hash.update(Array.from(this.runtimeRequirements).join() + "");
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * @param {ModuleGraph} moduleGraph the module graph
|
---|
50 | * @returns {ConnectionState} how this dependency connects the module to referencing modules
|
---|
51 | */
|
---|
52 | getModuleEvaluationSideEffectsState(moduleGraph) {
|
---|
53 | return false;
|
---|
54 | }
|
---|
55 |
|
---|
56 | serialize(context) {
|
---|
57 | const { write } = context;
|
---|
58 | write(this.expression);
|
---|
59 | write(this.range);
|
---|
60 | write(this.runtimeRequirements);
|
---|
61 | super.serialize(context);
|
---|
62 | }
|
---|
63 |
|
---|
64 | deserialize(context) {
|
---|
65 | const { read } = context;
|
---|
66 | this.expression = read();
|
---|
67 | this.range = read();
|
---|
68 | this.runtimeRequirements = read();
|
---|
69 | super.deserialize(context);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | makeSerializable(ConstDependency, "webpack/lib/dependencies/ConstDependency");
|
---|
74 |
|
---|
75 | ConstDependency.Template = class ConstDependencyTemplate extends (
|
---|
76 | NullDependency.Template
|
---|
77 | ) {
|
---|
78 | /**
|
---|
79 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
80 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
81 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
82 | * @returns {void}
|
---|
83 | */
|
---|
84 | apply(dependency, source, templateContext) {
|
---|
85 | const dep = /** @type {ConstDependency} */ (dependency);
|
---|
86 | if (dep.runtimeRequirements) {
|
---|
87 | for (const req of dep.runtimeRequirements) {
|
---|
88 | templateContext.runtimeRequirements.add(req);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | if (typeof dep.range === "number") {
|
---|
92 | source.insert(dep.range, dep.expression);
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | source.replace(dep.range[0], dep.range[1] - 1, dep.expression);
|
---|
97 | }
|
---|
98 | };
|
---|
99 |
|
---|
100 | module.exports = ConstDependency;
|
---|