[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Florent Cailhol @ooflorent
|
---|
| 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 | /** @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 |
|
---|
| 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 | }
|
---|
| 31 |
|
---|
| 32 | /**
|
---|
| 33 | * Update the hash
|
---|
| 34 | * @param {Hash} hash hash to be updated
|
---|
| 35 | * @param {UpdateHashContext} context context
|
---|
| 36 | * @returns {void}
|
---|
| 37 | */
|
---|
| 38 | updateHash(hash, context) {
|
---|
| 39 | hash.update(this.identifier + "");
|
---|
| 40 | hash.update(this.range + "");
|
---|
| 41 | hash.update(this.expression + "");
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | serialize(context) {
|
---|
| 45 | const { write } = context;
|
---|
| 46 |
|
---|
| 47 | write(this.expression);
|
---|
| 48 | write(this.range);
|
---|
| 49 | write(this.identifier);
|
---|
| 50 |
|
---|
| 51 | super.serialize(context);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | deserialize(context) {
|
---|
| 55 | const { read } = context;
|
---|
| 56 |
|
---|
| 57 | this.expression = read();
|
---|
| 58 | this.range = read();
|
---|
| 59 | this.identifier = read();
|
---|
| 60 |
|
---|
| 61 | super.deserialize(context);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | makeSerializable(
|
---|
| 66 | CachedConstDependency,
|
---|
| 67 | "webpack/lib/dependencies/CachedConstDependency"
|
---|
| 68 | );
|
---|
| 69 |
|
---|
| 70 | CachedConstDependency.Template = class CachedConstDependencyTemplate extends (
|
---|
| 71 | DependencyTemplate
|
---|
| 72 | ) {
|
---|
| 73 | /**
|
---|
| 74 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 75 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 76 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 77 | * @returns {void}
|
---|
| 78 | */
|
---|
| 79 | apply(
|
---|
| 80 | dependency,
|
---|
| 81 | source,
|
---|
| 82 | { runtimeTemplate, dependencyTemplates, initFragments }
|
---|
| 83 | ) {
|
---|
| 84 | const dep = /** @type {CachedConstDependency} */ (dependency);
|
---|
| 85 |
|
---|
| 86 | initFragments.push(
|
---|
| 87 | new InitFragment(
|
---|
| 88 | `var ${dep.identifier} = ${dep.expression};\n`,
|
---|
| 89 | InitFragment.STAGE_CONSTANTS,
|
---|
| 90 | 0,
|
---|
| 91 | `const ${dep.identifier}`
|
---|
| 92 | )
|
---|
| 93 | );
|
---|
| 94 |
|
---|
| 95 | if (typeof dep.range === "number") {
|
---|
| 96 | source.insert(dep.range, dep.identifier);
|
---|
| 97 |
|
---|
| 98 | return;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
|
---|
| 102 | }
|
---|
| 103 | };
|
---|
| 104 |
|
---|
| 105 | module.exports = CachedConstDependency;
|
---|