[79a0317] | 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("../javascript/JavascriptParser").Range} Range */
|
---|
| 22 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 23 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 24 | /** @typedef {import("../util/Hash")} Hash */
|
---|
| 25 |
|
---|
| 26 | class CachedConstDependency extends NullDependency {
|
---|
| 27 | /**
|
---|
| 28 | * @param {string} expression expression
|
---|
| 29 | * @param {Range} range range
|
---|
| 30 | * @param {string} identifier identifier
|
---|
| 31 | */
|
---|
| 32 | constructor(expression, range, identifier) {
|
---|
| 33 | super();
|
---|
| 34 |
|
---|
| 35 | this.expression = expression;
|
---|
| 36 | this.range = range;
|
---|
| 37 | this.identifier = identifier;
|
---|
| 38 | this._hashUpdate = undefined;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * @returns {string} hash update
|
---|
| 43 | */
|
---|
| 44 | _createHashUpdate() {
|
---|
| 45 | return `${this.identifier}${this.range}${this.expression}`;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * Update the hash
|
---|
| 50 | * @param {Hash} hash hash to be updated
|
---|
| 51 | * @param {UpdateHashContext} context context
|
---|
| 52 | * @returns {void}
|
---|
| 53 | */
|
---|
| 54 | updateHash(hash, context) {
|
---|
| 55 | if (this._hashUpdate === undefined) {
|
---|
| 56 | this._hashUpdate = this._createHashUpdate();
|
---|
| 57 | }
|
---|
| 58 | hash.update(this._hashUpdate);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /**
|
---|
| 62 | * @param {ObjectSerializerContext} context context
|
---|
| 63 | */
|
---|
| 64 | serialize(context) {
|
---|
| 65 | const { write } = context;
|
---|
| 66 |
|
---|
| 67 | write(this.expression);
|
---|
| 68 | write(this.range);
|
---|
| 69 | write(this.identifier);
|
---|
| 70 |
|
---|
| 71 | super.serialize(context);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * @param {ObjectDeserializerContext} context context
|
---|
| 76 | */
|
---|
| 77 | deserialize(context) {
|
---|
| 78 | const { read } = context;
|
---|
| 79 |
|
---|
| 80 | this.expression = read();
|
---|
| 81 | this.range = read();
|
---|
| 82 | this.identifier = read();
|
---|
| 83 |
|
---|
| 84 | super.deserialize(context);
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | makeSerializable(
|
---|
| 89 | CachedConstDependency,
|
---|
| 90 | "webpack/lib/dependencies/CachedConstDependency"
|
---|
| 91 | );
|
---|
| 92 |
|
---|
| 93 | CachedConstDependency.Template = class CachedConstDependencyTemplate extends (
|
---|
| 94 | DependencyTemplate
|
---|
| 95 | ) {
|
---|
| 96 | /**
|
---|
| 97 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 98 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 99 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 100 | * @returns {void}
|
---|
| 101 | */
|
---|
| 102 | apply(
|
---|
| 103 | dependency,
|
---|
| 104 | source,
|
---|
| 105 | { runtimeTemplate, dependencyTemplates, initFragments }
|
---|
| 106 | ) {
|
---|
| 107 | const dep = /** @type {CachedConstDependency} */ (dependency);
|
---|
| 108 |
|
---|
| 109 | initFragments.push(
|
---|
| 110 | new InitFragment(
|
---|
| 111 | `var ${dep.identifier} = ${dep.expression};\n`,
|
---|
| 112 | InitFragment.STAGE_CONSTANTS,
|
---|
| 113 | 0,
|
---|
| 114 | `const ${dep.identifier}`
|
---|
| 115 | )
|
---|
| 116 | );
|
---|
| 117 |
|
---|
| 118 | if (typeof dep.range === "number") {
|
---|
| 119 | source.insert(dep.range, dep.identifier);
|
---|
| 120 |
|
---|
| 121 | return;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
|
---|
| 125 | }
|
---|
| 126 | };
|
---|
| 127 |
|
---|
| 128 | module.exports = CachedConstDependency;
|
---|