2013-01-31 01:49:25 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2017-01-04 04:40:14 +08:00
|
|
|
"use strict";
|
2018-07-30 23:08:51 +08:00
|
|
|
|
2018-10-09 20:30:59 +08:00
|
|
|
const makeSerializable = require("../util/makeSerializable");
|
2017-01-04 04:40:14 +08:00
|
|
|
const NullDependency = require("./NullDependency");
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2018-07-23 23:33:29 +08:00
|
|
|
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("../Dependency")} Dependency */
|
2020-07-28 00:09:48 +08:00
|
|
|
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
2018-07-18 01:38:42 +08:00
|
|
|
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
|
2018-07-17 22:42:05 +08:00
|
|
|
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
2020-10-05 22:57:31 +08:00
|
|
|
/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
|
2023-05-22 04:31:30 +08:00
|
|
|
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
|
2023-04-12 03:22:51 +08:00
|
|
|
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|
|
|
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
2019-07-17 22:02:33 +08:00
|
|
|
/** @typedef {import("../util/Hash")} Hash */
|
2018-07-23 23:33:29 +08:00
|
|
|
|
2017-01-04 04:40:14 +08:00
|
|
|
class ConstDependency extends NullDependency {
|
2018-12-06 02:02:22 +08:00
|
|
|
/**
|
|
|
|
* @param {string} expression the expression
|
2023-06-13 01:24:59 +08:00
|
|
|
* @param {number | Range} range the source range
|
|
|
|
* @param {(string[] | null)=} runtimeRequirements runtime requirements
|
2018-12-06 02:02:22 +08:00
|
|
|
*/
|
2018-11-06 02:03:12 +08:00
|
|
|
constructor(expression, range, runtimeRequirements) {
|
2017-01-04 04:40:14 +08:00
|
|
|
super();
|
|
|
|
this.expression = expression;
|
|
|
|
this.range = range;
|
2018-11-06 02:03:12 +08:00
|
|
|
this.runtimeRequirements = runtimeRequirements
|
|
|
|
? new Set(runtimeRequirements)
|
|
|
|
: null;
|
2021-09-24 16:10:13 +08:00
|
|
|
this._hashUpdate = undefined;
|
2018-11-06 02:03:12 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 15:33:48 +08:00
|
|
|
/**
|
|
|
|
* Update the hash
|
|
|
|
* @param {Hash} hash hash to be updated
|
2020-07-28 00:09:48 +08:00
|
|
|
* @param {UpdateHashContext} context context
|
2018-07-25 15:33:48 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2020-07-28 00:09:48 +08:00
|
|
|
updateHash(hash, context) {
|
2021-09-24 16:10:13 +08:00
|
|
|
if (this._hashUpdate === undefined) {
|
2024-07-31 10:39:30 +08:00
|
|
|
let hashUpdate = `${this.range}|${this.expression}`;
|
2021-09-24 16:10:13 +08:00
|
|
|
if (this.runtimeRequirements) {
|
|
|
|
for (const item of this.runtimeRequirements) {
|
|
|
|
hashUpdate += "|";
|
|
|
|
hashUpdate += item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._hashUpdate = hashUpdate;
|
|
|
|
}
|
|
|
|
hash.update(this._hashUpdate);
|
2017-01-04 04:40:14 +08:00
|
|
|
}
|
2018-10-09 20:30:59 +08:00
|
|
|
|
2020-10-05 22:57:31 +08:00
|
|
|
/**
|
|
|
|
* @param {ModuleGraph} moduleGraph the module graph
|
|
|
|
* @returns {ConnectionState} how this dependency connects the module to referencing modules
|
|
|
|
*/
|
|
|
|
getModuleEvaluationSideEffectsState(moduleGraph) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-12 03:22:51 +08:00
|
|
|
/**
|
|
|
|
* @param {ObjectSerializerContext} context context
|
|
|
|
*/
|
2018-10-09 20:30:59 +08:00
|
|
|
serialize(context) {
|
|
|
|
const { write } = context;
|
|
|
|
write(this.expression);
|
|
|
|
write(this.range);
|
2018-11-06 02:03:12 +08:00
|
|
|
write(this.runtimeRequirements);
|
2018-10-09 20:30:59 +08:00
|
|
|
super.serialize(context);
|
|
|
|
}
|
|
|
|
|
2023-04-12 03:22:51 +08:00
|
|
|
/**
|
|
|
|
* @param {ObjectDeserializerContext} context context
|
|
|
|
*/
|
2018-10-09 20:30:59 +08:00
|
|
|
deserialize(context) {
|
|
|
|
const { read } = context;
|
|
|
|
this.expression = read();
|
|
|
|
this.range = read();
|
2018-11-06 02:03:12 +08:00
|
|
|
this.runtimeRequirements = read();
|
2018-10-09 20:30:59 +08:00
|
|
|
super.deserialize(context);
|
|
|
|
}
|
2017-01-04 04:40:14 +08:00
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2018-10-09 20:30:59 +08:00
|
|
|
makeSerializable(ConstDependency, "webpack/lib/dependencies/ConstDependency");
|
|
|
|
|
2020-11-26 17:52:55 +08:00
|
|
|
ConstDependency.Template = class ConstDependencyTemplate extends (
|
|
|
|
NullDependency.Template
|
|
|
|
) {
|
2018-07-23 23:33:29 +08:00
|
|
|
/**
|
|
|
|
* @param {Dependency} dependency the dependency for which the template should be applied
|
|
|
|
* @param {ReplaceSource} source the current replace source which can be modified
|
2018-07-18 01:38:42 +08:00
|
|
|
* @param {DependencyTemplateContext} templateContext the context object
|
2018-07-23 23:33:29 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-07-18 01:38:42 +08:00
|
|
|
apply(dependency, source, templateContext) {
|
2018-07-23 23:33:29 +08:00
|
|
|
const dep = /** @type {ConstDependency} */ (dependency);
|
2018-11-15 00:31:32 +08:00
|
|
|
if (dep.runtimeRequirements) {
|
|
|
|
for (const req of dep.runtimeRequirements) {
|
|
|
|
templateContext.runtimeRequirements.add(req);
|
|
|
|
}
|
|
|
|
}
|
2018-02-25 09:00:20 +08:00
|
|
|
if (typeof dep.range === "number") {
|
2017-01-04 04:40:14 +08:00
|
|
|
source.insert(dep.range, dep.expression);
|
|
|
|
return;
|
|
|
|
}
|
2013-01-31 01:49:25 +08:00
|
|
|
|
2015-04-24 05:55:50 +08:00
|
|
|
source.replace(dep.range[0], dep.range[1] - 1, dep.expression);
|
2017-01-04 04:40:14 +08:00
|
|
|
}
|
2017-01-11 17:51:58 +08:00
|
|
|
};
|
2015-09-03 20:17:11 +08:00
|
|
|
|
2017-01-04 04:40:14 +08:00
|
|
|
module.exports = ConstDependency;
|