2018-07-23 20:53:50 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Florent Cailhol @ooflorent
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-07-27 17:45:12 +08:00
|
|
|
const DependencyTemplate = require("../DependencyTemplate");
|
2018-07-23 20:53:50 +08:00
|
|
|
const InitFragment = require("../InitFragment");
|
2018-10-11 18:36:01 +08:00
|
|
|
const makeSerializable = require("../util/makeSerializable");
|
2018-07-23 20:53:50 +08:00
|
|
|
const NullDependency = require("./NullDependency");
|
|
|
|
|
2018-07-27 17:45:12 +08:00
|
|
|
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
|
2018-08-28 17:50:33 +08:00
|
|
|
/** @typedef {import("../ChunkGraph")} ChunkGraph */
|
2018-07-27 17:45:12 +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-27 17:45:12 +08:00
|
|
|
/** @typedef {import("../DependencyTemplates")} DependencyTemplates */
|
2018-07-17 22:42:05 +08:00
|
|
|
/** @typedef {import("../ModuleGraph")} ModuleGraph */
|
2018-07-27 17:45:12 +08:00
|
|
|
/** @typedef {import("../RuntimeTemplate")} RuntimeTemplate */
|
2019-07-17 22:02:33 +08:00
|
|
|
/** @typedef {import("../util/Hash")} Hash */
|
2018-07-27 17:45:12 +08:00
|
|
|
|
2018-07-23 20:53:50 +08:00
|
|
|
class CachedConstDependency extends NullDependency {
|
2021-09-15 02:23:41 +08:00
|
|
|
constructor(expression, range, identifier) {
|
2018-07-23 20:53:50 +08:00
|
|
|
super();
|
2018-10-11 18:36:01 +08:00
|
|
|
|
2018-07-23 20:53:50 +08:00
|
|
|
this.expression = expression;
|
|
|
|
this.range = range;
|
|
|
|
this.identifier = identifier;
|
2021-09-24 16:10:13 +08:00
|
|
|
this._hashUpdate = undefined;
|
2018-07-23 20:53:50 +08:00
|
|
|
}
|
|
|
|
|
2018-07-27 17:45:12 +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-27 17:45:12 +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)
|
|
|
|
this._hashUpdate = "" + this.identifier + this.range + this.expression;
|
|
|
|
hash.update(this._hashUpdate);
|
2018-07-23 20:53:50 +08:00
|
|
|
}
|
2018-10-11 18:36:01 +08:00
|
|
|
|
|
|
|
serialize(context) {
|
|
|
|
const { write } = context;
|
|
|
|
|
|
|
|
write(this.expression);
|
|
|
|
write(this.range);
|
|
|
|
write(this.identifier);
|
|
|
|
|
|
|
|
super.serialize(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
deserialize(context) {
|
|
|
|
const { read } = context;
|
|
|
|
|
|
|
|
this.expression = read();
|
|
|
|
this.range = read();
|
|
|
|
this.identifier = read();
|
|
|
|
|
|
|
|
super.deserialize(context);
|
|
|
|
}
|
2018-07-23 20:53:50 +08:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:01 +08:00
|
|
|
makeSerializable(
|
|
|
|
CachedConstDependency,
|
|
|
|
"webpack/lib/dependencies/CachedConstDependency"
|
|
|
|
);
|
|
|
|
|
2020-11-26 17:52:55 +08:00
|
|
|
CachedConstDependency.Template = class CachedConstDependencyTemplate extends (
|
|
|
|
DependencyTemplate
|
|
|
|
) {
|
2018-07-27 17:45:12 +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-27 17:45:12 +08:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-09-15 02:23:41 +08:00
|
|
|
apply(dependency, source, { runtimeTemplate, initFragments }) {
|
2018-07-27 17:45:12 +08:00
|
|
|
const dep = /** @type {CachedConstDependency} */ (dependency);
|
2018-10-11 18:36:01 +08:00
|
|
|
|
2018-11-16 23:40:03 +08:00
|
|
|
initFragments.push(
|
|
|
|
new InitFragment(
|
2021-09-14 21:50:03 +08:00
|
|
|
`${runtimeTemplate.supportsConst() ? "const" : "var"} ${
|
|
|
|
dep.identifier
|
|
|
|
} = ${dep.expression};\n`,
|
2018-11-16 23:40:03 +08:00
|
|
|
InitFragment.STAGE_CONSTANTS,
|
2021-09-15 02:23:41 +08:00
|
|
|
0,
|
2018-11-16 23:40:03 +08:00
|
|
|
`const ${dep.identifier}`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-07-23 20:53:50 +08:00
|
|
|
if (typeof dep.range === "number") {
|
|
|
|
source.insert(dep.range, dep.identifier);
|
2018-10-11 18:36:01 +08:00
|
|
|
|
2018-07-23 20:53:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
source.replace(dep.range[0], dep.range[1] - 1, dep.identifier);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = CachedConstDependency;
|