1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const DependencyTemplate = require("../DependencyTemplate");
|
9 | const InitFragment = require("../InitFragment");
|
10 | const makeSerializable = require("../util/makeSerializable");
|
11 | const NullDependency = require("./NullDependency");
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | class CachedConstDependency extends NullDependency {
|
24 | constructor(expression, range, identifier) {
|
25 | super();
|
26 |
|
27 | this.expression = expression;
|
28 | this.range = range;
|
29 | this.identifier = identifier;
|
30 | this._hashUpdate = undefined;
|
31 | }
|
32 |
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | updateHash(hash, context) {
|
40 | if (this._hashUpdate === undefined)
|
41 | this._hashUpdate = "" + this.identifier + this.range + this.expression;
|
42 | hash.update(this._hashUpdate);
|
43 | }
|
44 |
|
45 | serialize(context) {
|
46 | const { write } = context;
|
47 |
|
48 | write(this.expression);
|
49 | write(this.range);
|
50 | write(this.identifier);
|
51 |
|
52 | super.serialize(context);
|
53 | }
|
54 |
|
55 | deserialize(context) {
|
56 | const { read } = context;
|
57 |
|
58 | this.expression = read();
|
59 | this.range = read();
|
60 | this.identifier = read();
|
61 |
|
62 | super.deserialize(context);
|
63 | }
|
64 | }
|
65 |
|
66 | makeSerializable(
|
67 | CachedConstDependency,
|
68 | "webpack/lib/dependencies/CachedConstDependency"
|
69 | );
|
70 |
|
71 | CachedConstDependency.Template = class CachedConstDependencyTemplate extends (
|
72 | DependencyTemplate
|
73 | ) {
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | apply(
|
81 | dependency,
|
82 | source,
|
83 | { runtimeTemplate, dependencyTemplates, initFragments }
|
84 | ) {
|
85 | const dep = (dependency);
|
86 |
|
87 | initFragments.push(
|
88 | new InitFragment(
|
89 | `var ${dep.identifier} = ${dep.expression};\n`,
|
90 | InitFragment.STAGE_CONSTANTS,
|
91 | 0,
|
92 | `const ${dep.identifier}`
|
93 | )
|
94 | );
|
95 |
|
96 | if (typeof dep.range === "number") {
|
97 | source.insert(dep.range, dep.identifier);
|
98 |
|
99 | return;
|
100 | }
|
101 |
|
102 | source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
|
103 | }
|
104 | };
|
105 |
|
106 | module.exports = CachedConstDependency;
|