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