webpack/lib/dependencies/ConstDependency.js

101 lines
3.0 KiB
JavaScript
Raw Normal View History

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
"use strict";
2018-07-30 23:08:51 +08:00
2018-10-09 20:30:59 +08:00
const makeSerializable = require("../util/makeSerializable");
const NullDependency = require("./NullDependency");
2013-01-31 01:49:25 +08:00
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("../ChunkGraph")} ChunkGraph */
2018-07-30 23:08:51 +08:00
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
2018-07-17 22:42:05 +08:00
/** @typedef {import("../ModuleGraph")} ModuleGraph */
/** @typedef {import("../ModuleGraphConnection").ConnectionState} ConnectionState */
2019-07-17 22:02:33 +08:00
/** @typedef {import("../util/Hash")} Hash */
class ConstDependency extends NullDependency {
/**
* @param {string} expression the expression
2020-08-03 02:09:36 +08:00
* @param {number|[number, number]} range the source range
* @param {string[]=} runtimeRequirements runtime requirements
*/
2018-11-06 02:03:12 +08:00
constructor(expression, range, runtimeRequirements) {
super();
this.expression = expression;
this.range = range;
2018-11-06 02:03:12 +08:00
this.runtimeRequirements = runtimeRequirements
? new Set(runtimeRequirements)
: null;
}
2018-07-25 15:33:48 +08:00
/**
* Update the hash
* @param {Hash} hash hash to be updated
* @param {UpdateHashContext} context context
2018-07-25 15:33:48 +08:00
* @returns {void}
*/
updateHash(hash, context) {
hash.update(this.range + "");
hash.update(this.expression + "");
if (this.runtimeRequirements)
hash.update(Array.from(this.runtimeRequirements).join() + "");
}
2018-10-09 20:30:59 +08:00
/**
* @param {ModuleGraph} moduleGraph the module graph
* @returns {ConnectionState} how this dependency connects the module to referencing modules
*/
getModuleEvaluationSideEffectsState(moduleGraph) {
return false;
}
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);
}
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);
}
}
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
) {
/**
* @param {Dependency} dependency the dependency for which the template should be applied
* @param {ReplaceSource} source the current replace source which can be modified
* @param {DependencyTemplateContext} templateContext the context object
* @returns {void}
*/
apply(dependency, source, templateContext) {
const dep = /** @type {ConstDependency} */ (dependency);
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") {
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-11 17:51:58 +08:00
};
2015-09-03 20:17:11 +08:00
module.exports = ConstDependency;