UNPKG

2.85 kBJavaScriptView Raw
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Florent Cailhol @ooflorent
4*/
5
6"use strict";
7
8const DependencyTemplate = require("../DependencyTemplate");
9const InitFragment = require("../InitFragment");
10const makeSerializable = require("../util/makeSerializable");
11const NullDependency = require("./NullDependency");
12
13/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
14/** @typedef {import("../ChunkGraph")} ChunkGraph */
15/** @typedef {import("../Dependency")} Dependency */
16/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
17/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
18/** @typedef {import("../DependencyTemplates")} DependencyTemplates */
19/** @typedef {import("../ModuleGraph")} ModuleGraph */
20/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
21/** @typedef {import("../util/Hash")} Hash */
22
23class 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 * Update the hash
35 * @param {Hash} hash hash to be updated
36 * @param {UpdateHashContext} context context
37 * @returns {void}
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
66makeSerializable(
67 CachedConstDependency,
68 "webpack/lib/dependencies/CachedConstDependency"
69);
70
71CachedConstDependency.Template = class CachedConstDependencyTemplate extends (
72 DependencyTemplate
73) {
74 /**
75 * @param {Dependency} dependency the dependency for which the template should be applied
76 * @param {ReplaceSource} source the current replace source which can be modified
77 * @param {DependencyTemplateContext} templateContext the context object
78 * @returns {void}
79 */
80 apply(
81 dependency,
82 source,
83 { runtimeTemplate, dependencyTemplates, initFragments }
84 ) {
85 const dep = /** @type {CachedConstDependency} */ (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
106module.exports = CachedConstDependency;