| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
- */
- "use strict";
- const makeSerializable = require("../util/makeSerializable");
- const NullDependency = require("./NullDependency");
- /** @import { ReplaceSource } from "webpack-sources" */
- /** @import { RawRuntimeRequirements } from "./NullDependency" */
- /** @import Dependency, { UpdateHashContext } from "../Dependency" */
- /** @import { DependencyTemplateContext } from "../DependencyTemplate" */
- /** @import ModuleGraph from "../ModuleGraph" */
- /** @import { ConnectionState } from "../ModuleGraphConnection" */
- /** @import { Range } from "../javascript/JavascriptParser" */
- /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[string, number | Range, Set<string> | null]>} ObjectDeserializerContext */
- /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[string, number | Range, Set<string> | null]>} ObjectSerializerContext */
- /** @import Hash from "../util/Hash" */
- class ConstDependency extends NullDependency {
- /**
- * Creates an instance of ConstDependency.
- * @param {string} expression the expression
- * @param {number | Range} range the source range
- * @param {RawRuntimeRequirements | null=} runtimeRequirements runtime requirements
- */
- constructor(expression, range, runtimeRequirements) {
- super();
- /** @type {string} */
- this.expression = expression;
- this.range = range;
- /** @type {Set<string> | null} */
- this.runtimeRequirements = runtimeRequirements
- ? new Set(runtimeRequirements)
- : null;
- /** @type {undefined | string} */
- this._hashUpdate = undefined;
- }
- /**
- * Updates the hash with the data contributed by this instance.
- * @param {Hash} hash hash to be updated
- * @param {UpdateHashContext} context context
- * @returns {void}
- */
- updateHash(hash, context) {
- if (this._hashUpdate === undefined) {
- let hashUpdate = `${this.range}|${this.expression}`;
- if (this.runtimeRequirements) {
- for (const item of this.runtimeRequirements) {
- hashUpdate += "|";
- hashUpdate += item;
- }
- }
- this._hashUpdate = hashUpdate;
- }
- hash.update(this._hashUpdate);
- }
- /**
- * Gets module evaluation side effects state.
- * @param {ModuleGraph} moduleGraph the module graph
- * @returns {ConnectionState} how this dependency connects the module to referencing modules
- */
- getModuleEvaluationSideEffectsState(moduleGraph) {
- return false;
- }
- /**
- * Serializes this instance into the provided serializer context.
- * @param {ObjectSerializerContext} context context
- */
- serialize(context) {
- // chain writes so each position is type-checked against the tuple
- context
- .write(this.expression)
- .write(this.range)
- .write(this.runtimeRequirements);
- super.serialize(context);
- }
- /**
- * Restores this instance from the provided deserializer context.
- * @param {ObjectDeserializerContext} context context
- */
- deserialize(context) {
- // advance the cursor via `rest` so each read is type-checked by position
- this.expression = context.read();
- const c1 = context.rest;
- this.range = c1.read();
- const c2 = c1.rest;
- this.runtimeRequirements = c2.read();
- super.deserialize(c2.rest);
- }
- }
- makeSerializable(ConstDependency, "webpack/lib/dependencies/ConstDependency");
- ConstDependency.Template = class ConstDependencyTemplate extends (
- NullDependency.Template
- ) {
- /**
- * Applies the plugin by registering its hooks on the compiler.
- * @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);
- }
- }
- if (typeof dep.range === "number") {
- source.insert(dep.range, dep.expression);
- return;
- }
- source.replace(dep.range[0], dep.range[1] - 1, dep.expression);
- }
- };
- module.exports = ConstDependency;
|