[79a0317] | 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("../javascript/JavascriptParser").Range} Range */
|
---|
| 19 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 20 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 21 | /** @typedef {import("../util/Hash")} Hash */
|
---|
| 22 |
|
---|
| 23 | class ConstDependency extends NullDependency {
|
---|
| 24 | /**
|
---|
| 25 | * @param {string} expression the expression
|
---|
| 26 | * @param {number | Range} range the source range
|
---|
| 27 | * @param {(string[] | null)=} runtimeRequirements runtime requirements
|
---|
| 28 | */
|
---|
| 29 | constructor(expression, range, runtimeRequirements) {
|
---|
| 30 | super();
|
---|
| 31 | this.expression = expression;
|
---|
| 32 | this.range = range;
|
---|
| 33 | this.runtimeRequirements = runtimeRequirements
|
---|
| 34 | ? new Set(runtimeRequirements)
|
---|
| 35 | : null;
|
---|
| 36 | this._hashUpdate = undefined;
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Update the hash
|
---|
| 41 | * @param {Hash} hash hash to be updated
|
---|
| 42 | * @param {UpdateHashContext} context context
|
---|
| 43 | * @returns {void}
|
---|
| 44 | */
|
---|
| 45 | updateHash(hash, context) {
|
---|
| 46 | if (this._hashUpdate === undefined) {
|
---|
| 47 | let hashUpdate = `${this.range}|${this.expression}`;
|
---|
| 48 | if (this.runtimeRequirements) {
|
---|
| 49 | for (const item of this.runtimeRequirements) {
|
---|
| 50 | hashUpdate += "|";
|
---|
| 51 | hashUpdate += item;
|
---|
| 52 | }
|
---|
| 53 | }
|
---|
| 54 | this._hashUpdate = hashUpdate;
|
---|
| 55 | }
|
---|
| 56 | hash.update(this._hashUpdate);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * @param {ModuleGraph} moduleGraph the module graph
|
---|
| 61 | * @returns {ConnectionState} how this dependency connects the module to referencing modules
|
---|
| 62 | */
|
---|
| 63 | getModuleEvaluationSideEffectsState(moduleGraph) {
|
---|
| 64 | return false;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * @param {ObjectSerializerContext} context context
|
---|
| 69 | */
|
---|
| 70 | serialize(context) {
|
---|
| 71 | const { write } = context;
|
---|
| 72 | write(this.expression);
|
---|
| 73 | write(this.range);
|
---|
| 74 | write(this.runtimeRequirements);
|
---|
| 75 | super.serialize(context);
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * @param {ObjectDeserializerContext} context context
|
---|
| 80 | */
|
---|
| 81 | deserialize(context) {
|
---|
| 82 | const { read } = context;
|
---|
| 83 | this.expression = read();
|
---|
| 84 | this.range = read();
|
---|
| 85 | this.runtimeRequirements = read();
|
---|
| 86 | super.deserialize(context);
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | makeSerializable(ConstDependency, "webpack/lib/dependencies/ConstDependency");
|
---|
| 91 |
|
---|
| 92 | ConstDependency.Template = class ConstDependencyTemplate extends (
|
---|
| 93 | NullDependency.Template
|
---|
| 94 | ) {
|
---|
| 95 | /**
|
---|
| 96 | * @param {Dependency} dependency the dependency for which the template should be applied
|
---|
| 97 | * @param {ReplaceSource} source the current replace source which can be modified
|
---|
| 98 | * @param {DependencyTemplateContext} templateContext the context object
|
---|
| 99 | * @returns {void}
|
---|
| 100 | */
|
---|
| 101 | apply(dependency, source, templateContext) {
|
---|
| 102 | const dep = /** @type {ConstDependency} */ (dependency);
|
---|
| 103 | if (dep.runtimeRequirements) {
|
---|
| 104 | for (const req of dep.runtimeRequirements) {
|
---|
| 105 | templateContext.runtimeRequirements.add(req);
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | if (typeof dep.range === "number") {
|
---|
| 109 | source.insert(dep.range, dep.expression);
|
---|
| 110 | return;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | source.replace(dep.range[0], dep.range[1] - 1, dep.expression);
|
---|
| 114 | }
|
---|
| 115 | };
|
---|
| 116 |
|
---|
| 117 | module.exports = ConstDependency;
|
---|