2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
8 const { OriginalSource, RawSource } = require("webpack-sources");
9 const Module = require("./Module");
10 const RuntimeGlobals = require("./RuntimeGlobals");
11 const DelegatedSourceDependency = require("./dependencies/DelegatedSourceDependency");
12 const StaticExportsDependency = require("./dependencies/StaticExportsDependency");
13 const makeSerializable = require("./util/makeSerializable");
15 /** @typedef {import("webpack-sources").Source} Source */
16 /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
17 /** @typedef {import("./ChunkGraph")} ChunkGraph */
18 /** @typedef {import("./Compilation")} Compilation */
19 /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
20 /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
21 /** @typedef {import("./LibManifestPlugin").ManifestModuleData} ManifestModuleData */
22 /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
23 /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
24 /** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
25 /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
26 /** @typedef {import("./Module").SourceContext} SourceContext */
27 /** @typedef {import("./RequestShortener")} RequestShortener */
28 /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
29 /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
30 /** @typedef {import("./WebpackError")} WebpackError */
31 /** @typedef {import("./dependencies/ModuleDependency")} ModuleDependency */
32 /** @typedef {import("./util/Hash")} Hash */
33 /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
35 const TYPES = new Set(["javascript"]);
36 const RUNTIME_REQUIREMENTS = new Set([
37 RuntimeGlobals.module,
38 RuntimeGlobals.require
41 class DelegatedModule extends Module {
42 constructor(sourceRequest, data, type, userRequest, originalRequest) {
43 super("javascript/dynamic", null);
46 this.sourceRequest = sourceRequest;
47 this.request = data.id;
48 this.delegationType = type;
49 this.userRequest = userRequest;
50 this.originalRequest = originalRequest;
51 /** @type {ManifestModuleData} */
52 this.delegateData = data;
55 this.delegatedSourceDependency = undefined;
59 * @returns {Set<string>} types available (do not mutate)
66 * @param {LibIdentOptions} options options
67 * @returns {string | null} an identifier for library inclusion
70 return typeof this.originalRequest === "string"
71 ? this.originalRequest
72 : this.originalRequest.libIdent(options);
76 * @returns {string} a unique identifier of the module
79 return `delegated ${JSON.stringify(this.request)} from ${
85 * @param {RequestShortener} requestShortener the request shortener
86 * @returns {string} a user readable identifier of the module
88 readableIdentifier(requestShortener) {
89 return `delegated ${this.userRequest} from ${this.sourceRequest}`;
93 * @param {NeedBuildContext} context context info
94 * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
97 needBuild(context, callback) {
98 return callback(null, !this.buildMeta);
102 * @param {WebpackOptions} options webpack options
103 * @param {Compilation} compilation the compilation
104 * @param {ResolverWithOptions} resolver the resolver
105 * @param {InputFileSystem} fs the file system
106 * @param {function(WebpackError=): void} callback callback function
109 build(options, compilation, resolver, fs, callback) {
110 this.buildMeta = { ...this.delegateData.buildMeta };
112 this.dependencies.length = 0;
113 this.delegatedSourceDependency = new DelegatedSourceDependency(
116 this.addDependency(this.delegatedSourceDependency);
118 new StaticExportsDependency(this.delegateData.exports || true, false)
124 * @param {CodeGenerationContext} context context for code generation
125 * @returns {CodeGenerationResult} result
127 codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
128 const dep = /** @type {DelegatedSourceDependency} */ (this.dependencies[0]);
129 const sourceModule = moduleGraph.getModule(dep);
133 str = runtimeTemplate.throwMissingModuleErrorBlock({
134 request: this.sourceRequest
137 str = `module.exports = (${runtimeTemplate.moduleExports({
138 module: sourceModule,
140 request: dep.request,
141 runtimeRequirements: new Set()
144 switch (this.delegationType) {
146 str += `(${JSON.stringify(this.request)})`;
149 str += `[${JSON.stringify(this.request)}]`;
156 const sources = new Map();
157 if (this.useSourceMap || this.useSimpleSourceMap) {
158 sources.set("javascript", new OriginalSource(str, this.identifier()));
160 sources.set("javascript", new RawSource(str));
165 runtimeRequirements: RUNTIME_REQUIREMENTS
170 * @param {string=} type the source type for which the size should be estimated
171 * @returns {number} the estimated size of the module (must be non-zero)
178 * @param {Hash} hash the hash used to track dependencies
179 * @param {UpdateHashContext} context context
182 updateHash(hash, context) {
183 hash.update(this.delegationType);
184 hash.update(JSON.stringify(this.request));
185 super.updateHash(hash, context);
189 const { write } = context;
191 write(this.sourceRequest);
192 write(this.delegateData);
193 write(this.delegationType);
194 write(this.userRequest);
195 write(this.originalRequest);
196 super.serialize(context);
199 static deserialize(context) {
200 const { read } = context;
201 const obj = new DelegatedModule(
202 read(), // sourceRequest
203 read(), // delegateData
204 read(), // delegationType
205 read(), // userRequest
206 read() // originalRequest
208 obj.deserialize(context);
213 * Assuming this module is in the cache. Update the (cached) module with
214 * the fresh module from the factory. Usually updates internal references
216 * @param {Module} module fresh module
219 updateCacheModule(module) {
220 super.updateCacheModule(module);
221 const m = /** @type {DelegatedModule} */ (module);
222 this.delegationType = m.delegationType;
223 this.userRequest = m.userRequest;
224 this.originalRequest = m.originalRequest;
225 this.delegateData = m.delegateData;
229 * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
232 super.cleanupForCache();
233 this.delegateData = undefined;
237 makeSerializable(DelegatedModule, "webpack/lib/DelegatedModule");
239 module.exports = DelegatedModule;