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-08-28 17:50:33 +08:00
|
|
|
/** @typedef {import("../ChunkGraph")} ChunkGraph */
|
2018-07-30 23:08:51 +08:00
|
|
|
/** @typedef {import("../Dependency")} Dependency */
|
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 */
|
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
|
|
|
|
* @param {TODO} range the source range
|
|
|
|
* @param {string[]=} runtimeRequirements runtime requirements
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2018-07-25 15:33:48 +08:00
|
|
|
/**
|
|
|
|
* Update the hash
|
|
|
|
* @param {Hash} hash hash to be updated
|
2018-08-28 17:50:33 +08:00
|
|
|
* @param {ChunkGraph} chunkGraph chunk graph
|
2018-07-25 15:33:48 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-08-28 17:50:33 +08:00
|
|
|
updateHash(hash, chunkGraph) {
|
2017-01-04 04:40:14 +08:00
|
|
|
hash.update(this.range + "");
|
|
|
|
hash.update(this.expression + "");
|
2019-10-07 17:16:38 +08:00
|
|
|
if (this.runtimeRequirements)
|
|
|
|
hash.update(Array.from(this.runtimeRequirements).join() + "");
|
2017-01-04 04:40:14 +08:00
|
|
|
}
|
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);
|
|
|
|
}
|
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");
|
|
|
|
|
2018-07-23 23:33:29 +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
|
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;
|