Source code

Revision control

Copy as Markdown

Other Tools

/*
Author Tobias Koppers @sokra
*/
"use strict";
const makeSerializable = require("../util/makeSerializable");
const NullDependency = require("./NullDependency");
/** @typedef {import("webpack-sources").ReplaceSource} ReplaceSource */
/** @typedef {import("../Dependency")} Dependency */
/** @typedef {import("../DependencyTemplate").DependencyTemplateContext} DependencyTemplateContext */
/** @typedef {import("../javascript/JavascriptParser").Range} Range */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext<[LocalModule, Range | undefined, boolean]>} ObjectDeserializerContext */
/** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext<[LocalModule, Range | undefined, boolean]>} ObjectSerializerContext */
/** @typedef {import("./LocalModule")} LocalModule */
class LocalModuleDependency extends NullDependency {
/**
* Creates an instance of LocalModuleDependency.
* @param {LocalModule} localModule local module
* @param {Range | undefined} range range
* @param {boolean} callNew true, when the local module should be called with new
*/
constructor(localModule, range, callNew) {
super();
/** @type {LocalModule} */
this.localModule = localModule;
this.range = range;
/** @type {boolean} */
this.callNew = callNew;
}
/**
* Serializes this instance into the provided serializer context.
* @param {ObjectSerializerContext} context context
*/
serialize(context) {
context.write(this.localModule).write(this.range).write(this.callNew);
super.serialize(context);
}
/**
* Restores this instance from the provided deserializer context.
* @param {ObjectDeserializerContext} context context
*/
deserialize(context) {
this.localModule = context.read();
const c1 = context.rest;
this.range = c1.read();
const c2 = c1.rest;
this.callNew = c2.read();
super.deserialize(c2.rest);
}
}
makeSerializable(
LocalModuleDependency,
"webpack/lib/dependencies/LocalModuleDependency"
);
LocalModuleDependency.Template = class LocalModuleDependencyTemplate 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 {LocalModuleDependency} */ (dependency);
if (!dep.range) return;
const moduleInstance = dep.callNew
? `new (function () { return ${dep.localModule.variableName()}; })()`
: dep.localModule.variableName();
source.replace(dep.range[0], dep.range[1] - 1, moduleInstance);
}
};
module.exports = LocalModuleDependency;