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 */
|
2021-09-14 21:50:03 +08:00
|
|
|
/** @typedef {import("../Generator").GenerateContext} GenerateContext */
|
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-14 21:50:03 +08:00
|
|
|
/**
|
|
|
|
* @param {string} expression expression
|
|
|
|
* @param {number|[number, number]} range range
|
|
|
|
* @param {string} identifier identifier
|
|
|
|
* @param {InitFragment<GenerateContext>[]=} initFragments init fragments
|
|
|
|
*/
|
|
|
|
constructor(expression, range, identifier, initFragments) {
|
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-14 21:50:03 +08:00
|
|
|
this.initFragments = initFragments;
|
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-14 21:50:03 +08:00
|
|
|
hash.update(`${this.identifier}`);
|
|
|
|
hash.update(`${this.range}`);
|
|
|
|
hash.update(`${this.expression}`);
|
|
|
|
|
|
|
|
if (this.initFragments) {
|
|
|
|
for (const fragment of this.initFragments)
|
|
|
|
hash.update(`${fragment.key}_${fragment.position}`);
|
|
|
|
}
|
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);
|
2021-09-14 21:50:03 +08:00
|
|
|
write(this.initFragments);
|
2018-10-11 18:36:01 +08:00
|
|
|
|
|
|
|
super.serialize(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
deserialize(context) {
|
|
|
|
const { read } = context;
|
|
|
|
|
|
|
|
this.expression = read();
|
|
|
|
this.range = read();
|
|
|
|
this.identifier = read();
|
2021-09-14 21:50:03 +08:00
|
|
|
this.initFragments = read();
|
2018-10-11 18:36:01 +08:00
|
|
|
|
|
|
|
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}
|
|
|
|
*/
|
2018-11-16 23:40:03 +08:00
|
|
|
apply(
|
|
|
|
dependency,
|
|
|
|
source,
|
|
|
|
{ runtimeTemplate, dependencyTemplates, initFragments }
|
|
|
|
) {
|
2018-07-27 17:45:12 +08:00
|
|
|
const dep = /** @type {CachedConstDependency} */ (dependency);
|
2018-10-11 18:36:01 +08:00
|
|
|
|
2021-09-14 21:50:03 +08:00
|
|
|
let position = 0;
|
|
|
|
|
|
|
|
if (dep.initFragments && dep.initFragments.length > 0) {
|
|
|
|
for (const fragment of dep.initFragments) {
|
|
|
|
initFragments.push(fragment);
|
|
|
|
}
|
|
|
|
position = dep.initFragments[dep.initFragments.length - 1].position + 1;
|
|
|
|
}
|
|
|
|
|
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-14 21:50:03 +08:00
|
|
|
position,
|
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;
|