| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { OriginalSource, RawSource } = require("webpack-sources");
|
|---|
| 9 | const Module = require("../Module");
|
|---|
| 10 | const {
|
|---|
| 11 | JAVASCRIPT_TYPE,
|
|---|
| 12 | JAVASCRIPT_TYPES
|
|---|
| 13 | } = require("../ModuleSourceTypeConstants");
|
|---|
| 14 | const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("../ModuleTypeConstants");
|
|---|
| 15 | const RuntimeGlobals = require("../RuntimeGlobals");
|
|---|
| 16 | const DelegatedSourceDependency = require("../dependencies/DelegatedSourceDependency");
|
|---|
| 17 | const StaticExportsDependency = require("../dependencies/StaticExportsDependency");
|
|---|
| 18 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 19 |
|
|---|
| 20 | /** @typedef {import("../../declarations/plugins/dll/DllReferencePlugin").DllReferencePluginOptions} DllReferencePluginOptions */
|
|---|
| 21 | /** @typedef {import("../config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
|
|---|
| 22 | /** @typedef {import("../Compilation")} Compilation */
|
|---|
| 23 | /** @typedef {import("../Dependency").UpdateHashContext} UpdateHashContext */
|
|---|
| 24 | /** @typedef {import("../Generator").SourceTypes} SourceTypes */
|
|---|
| 25 | /** @typedef {import("./LibManifestPlugin").ManifestModuleData} ManifestModuleData */
|
|---|
| 26 | /** @typedef {import("../Module").ModuleId} ModuleId */
|
|---|
| 27 | /** @typedef {import("../Module").BuildCallback} BuildCallback */
|
|---|
| 28 | /** @typedef {import("../Module").BuildMeta} BuildMeta */
|
|---|
| 29 | /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
|
|---|
| 30 | /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
|
|---|
| 31 | /** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
|
|---|
| 32 | /** @typedef {import("../Module").LibIdent} LibIdent */
|
|---|
| 33 | /** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
|
|---|
| 34 | /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
|
|---|
| 35 | /** @typedef {import("../Module").Sources} Sources */
|
|---|
| 36 | /** @typedef {import("../Module").RuntimeRequirements} RuntimeRequirements */
|
|---|
| 37 | /** @typedef {import("../RequestShortener")} RequestShortener */
|
|---|
| 38 | /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
|---|
| 39 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 40 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 41 | /** @typedef {import("../dependencies/StaticExportsDependency").Exports} Exports */
|
|---|
| 42 | /** @typedef {import("../util/Hash")} Hash */
|
|---|
| 43 | /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
|
|---|
| 44 |
|
|---|
| 45 | /** @typedef {string} DelegatedModuleSourceRequest */
|
|---|
| 46 |
|
|---|
| 47 | /** @typedef {NonNullable<DllReferencePluginOptions["type"]>} DelegatedModuleType */
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Defines the delegated module data type used by this module.
|
|---|
| 51 | * @typedef {object} DelegatedModuleData
|
|---|
| 52 | * @property {BuildMeta=} buildMeta build meta
|
|---|
| 53 | * @property {Exports=} exports exports
|
|---|
| 54 | * @property {ModuleId} id module id
|
|---|
| 55 | */
|
|---|
| 56 |
|
|---|
| 57 | const RUNTIME_REQUIREMENTS = new Set([
|
|---|
| 58 | RuntimeGlobals.module,
|
|---|
| 59 | RuntimeGlobals.require
|
|---|
| 60 | ]);
|
|---|
| 61 |
|
|---|
| 62 | class DelegatedModule extends Module {
|
|---|
| 63 | /**
|
|---|
| 64 | * Creates an instance of DelegatedModule.
|
|---|
| 65 | * @param {DelegatedModuleSourceRequest} sourceRequest source request
|
|---|
| 66 | * @param {DelegatedModuleData} data data
|
|---|
| 67 | * @param {DelegatedModuleType} type type
|
|---|
| 68 | * @param {string} userRequest user request
|
|---|
| 69 | * @param {string | Module} originalRequest original request
|
|---|
| 70 | */
|
|---|
| 71 | constructor(sourceRequest, data, type, userRequest, originalRequest) {
|
|---|
| 72 | super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, null);
|
|---|
| 73 |
|
|---|
| 74 | // Info from Factory
|
|---|
| 75 | this.sourceRequest = sourceRequest;
|
|---|
| 76 | this.request = data.id;
|
|---|
| 77 | this.delegationType = type;
|
|---|
| 78 | this.userRequest = userRequest;
|
|---|
| 79 | this.originalRequest = originalRequest;
|
|---|
| 80 | this.delegateData = data;
|
|---|
| 81 |
|
|---|
| 82 | // Build info
|
|---|
| 83 | /** @type {undefined | DelegatedSourceDependency} */
|
|---|
| 84 | this.delegatedSourceDependency = undefined;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | /**
|
|---|
| 88 | * Returns the source types this module can generate.
|
|---|
| 89 | * @returns {SourceTypes} types available (do not mutate)
|
|---|
| 90 | */
|
|---|
| 91 | getSourceTypes() {
|
|---|
| 92 | return JAVASCRIPT_TYPES;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Gets the library identifier.
|
|---|
| 97 | * @param {LibIdentOptions} options options
|
|---|
| 98 | * @returns {LibIdent | null} an identifier for library inclusion
|
|---|
| 99 | */
|
|---|
| 100 | libIdent(options) {
|
|---|
| 101 | return typeof this.originalRequest === "string"
|
|---|
| 102 | ? this.originalRequest
|
|---|
| 103 | : this.originalRequest.libIdent(options);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * Returns the unique identifier used to reference this module.
|
|---|
| 108 | * @returns {string} a unique identifier of the module
|
|---|
| 109 | */
|
|---|
| 110 | identifier() {
|
|---|
| 111 | return `delegated ${JSON.stringify(this.request)} from ${
|
|---|
| 112 | this.sourceRequest
|
|---|
| 113 | }`;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /**
|
|---|
| 117 | * Returns a human-readable identifier for this module.
|
|---|
| 118 | * @param {RequestShortener} requestShortener the request shortener
|
|---|
| 119 | * @returns {string} a user readable identifier of the module
|
|---|
| 120 | */
|
|---|
| 121 | readableIdentifier(requestShortener) {
|
|---|
| 122 | return `delegated ${this.userRequest} from ${this.sourceRequest}`;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * Checks whether the module needs to be rebuilt for the current build state.
|
|---|
| 127 | * @param {NeedBuildContext} context context info
|
|---|
| 128 | * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
|
|---|
| 129 | * @returns {void}
|
|---|
| 130 | */
|
|---|
| 131 | needBuild(context, callback) {
|
|---|
| 132 | return callback(null, !this.buildMeta);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /**
|
|---|
| 136 | * Builds the module using the provided compilation context.
|
|---|
| 137 | * @param {WebpackOptions} options webpack options
|
|---|
| 138 | * @param {Compilation} compilation the compilation
|
|---|
| 139 | * @param {ResolverWithOptions} resolver the resolver
|
|---|
| 140 | * @param {InputFileSystem} fs the file system
|
|---|
| 141 | * @param {BuildCallback} callback callback function
|
|---|
| 142 | * @returns {void}
|
|---|
| 143 | */
|
|---|
| 144 | build(options, compilation, resolver, fs, callback) {
|
|---|
| 145 | const delegateData = /** @type {ManifestModuleData} */ (this.delegateData);
|
|---|
| 146 | this.buildMeta = { ...delegateData.buildMeta };
|
|---|
| 147 | this.buildInfo = {};
|
|---|
| 148 | this.dependencies.length = 0;
|
|---|
| 149 | this.delegatedSourceDependency = new DelegatedSourceDependency(
|
|---|
| 150 | this.sourceRequest
|
|---|
| 151 | );
|
|---|
| 152 | this.addDependency(this.delegatedSourceDependency);
|
|---|
| 153 | this.addDependency(
|
|---|
| 154 | new StaticExportsDependency(delegateData.exports || true, false)
|
|---|
| 155 | );
|
|---|
| 156 | callback();
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * Generates code and runtime requirements for this module.
|
|---|
| 161 | * @param {CodeGenerationContext} context context for code generation
|
|---|
| 162 | * @returns {CodeGenerationResult} result
|
|---|
| 163 | */
|
|---|
| 164 | codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
|
|---|
| 165 | const dep = /** @type {DelegatedSourceDependency} */ (this.dependencies[0]);
|
|---|
| 166 | const sourceModule = moduleGraph.getModule(dep);
|
|---|
| 167 | /** @type {string} */
|
|---|
| 168 | let str;
|
|---|
| 169 |
|
|---|
| 170 | if (!sourceModule) {
|
|---|
| 171 | str = runtimeTemplate.throwMissingModuleErrorBlock({
|
|---|
| 172 | request: this.sourceRequest
|
|---|
| 173 | });
|
|---|
| 174 | } else {
|
|---|
| 175 | str = `module.exports = (${runtimeTemplate.moduleExports({
|
|---|
| 176 | module: sourceModule,
|
|---|
| 177 | chunkGraph,
|
|---|
| 178 | request: dep.request,
|
|---|
| 179 | /** @type {RuntimeRequirements} */
|
|---|
| 180 | runtimeRequirements: new Set()
|
|---|
| 181 | })})`;
|
|---|
| 182 |
|
|---|
| 183 | switch (this.delegationType) {
|
|---|
| 184 | case "require":
|
|---|
| 185 | str += `(${JSON.stringify(this.request)})`;
|
|---|
| 186 | break;
|
|---|
| 187 | case "object":
|
|---|
| 188 | str += `[${JSON.stringify(this.request)}]`;
|
|---|
| 189 | break;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | str += ";";
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /** @type {Sources} */
|
|---|
| 196 | const sources = new Map();
|
|---|
| 197 | if (this.useSourceMap || this.useSimpleSourceMap) {
|
|---|
| 198 | sources.set(JAVASCRIPT_TYPE, new OriginalSource(str, this.identifier()));
|
|---|
| 199 | } else {
|
|---|
| 200 | sources.set(JAVASCRIPT_TYPE, new RawSource(str));
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | return {
|
|---|
| 204 | sources,
|
|---|
| 205 | runtimeRequirements: RUNTIME_REQUIREMENTS
|
|---|
| 206 | };
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /**
|
|---|
| 210 | * Returns the estimated size for the requested source type.
|
|---|
| 211 | * @param {string=} type the source type for which the size should be estimated
|
|---|
| 212 | * @returns {number} the estimated size of the module (must be non-zero)
|
|---|
| 213 | */
|
|---|
| 214 | size(type) {
|
|---|
| 215 | return 42;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | /**
|
|---|
| 219 | * Updates the hash with the data contributed by this instance.
|
|---|
| 220 | * @param {Hash} hash the hash used to track dependencies
|
|---|
| 221 | * @param {UpdateHashContext} context context
|
|---|
| 222 | * @returns {void}
|
|---|
| 223 | */
|
|---|
| 224 | updateHash(hash, context) {
|
|---|
| 225 | hash.update(this.delegationType);
|
|---|
| 226 | hash.update(JSON.stringify(this.request));
|
|---|
| 227 | super.updateHash(hash, context);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | /**
|
|---|
| 231 | * Serializes this instance into the provided serializer context.
|
|---|
| 232 | * @param {ObjectSerializerContext} context context
|
|---|
| 233 | */
|
|---|
| 234 | serialize(context) {
|
|---|
| 235 | const { write } = context;
|
|---|
| 236 | // constructor
|
|---|
| 237 | write(this.sourceRequest);
|
|---|
| 238 | write(this.delegateData);
|
|---|
| 239 | write(this.delegationType);
|
|---|
| 240 | write(this.userRequest);
|
|---|
| 241 | write(this.originalRequest);
|
|---|
| 242 | super.serialize(context);
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | /**
|
|---|
| 246 | * Restores this instance from the provided deserializer context.
|
|---|
| 247 | * @param {ObjectDeserializerContext} context context\
|
|---|
| 248 | * @returns {DelegatedModule} DelegatedModule
|
|---|
| 249 | */
|
|---|
| 250 | static deserialize(context) {
|
|---|
| 251 | const { read } = context;
|
|---|
| 252 | const obj = new DelegatedModule(
|
|---|
| 253 | read(), // sourceRequest
|
|---|
| 254 | read(), // delegateData
|
|---|
| 255 | read(), // delegationType
|
|---|
| 256 | read(), // userRequest
|
|---|
| 257 | read() // originalRequest
|
|---|
| 258 | );
|
|---|
| 259 | obj.deserialize(context);
|
|---|
| 260 | return obj;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | /**
|
|---|
| 264 | * Assuming this module is in the cache. Update the (cached) module with
|
|---|
| 265 | * the fresh module from the factory. Usually updates internal references
|
|---|
| 266 | * and properties.
|
|---|
| 267 | * @param {Module} module fresh module
|
|---|
| 268 | * @returns {void}
|
|---|
| 269 | */
|
|---|
| 270 | updateCacheModule(module) {
|
|---|
| 271 | super.updateCacheModule(module);
|
|---|
| 272 | const m = /** @type {DelegatedModule} */ (module);
|
|---|
| 273 | this.delegationType = m.delegationType;
|
|---|
| 274 | this.userRequest = m.userRequest;
|
|---|
| 275 | this.originalRequest = m.originalRequest;
|
|---|
| 276 | this.delegateData = m.delegateData;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | /**
|
|---|
| 280 | * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
|
|---|
| 281 | */
|
|---|
| 282 | cleanupForCache() {
|
|---|
| 283 | super.cleanupForCache();
|
|---|
| 284 | this.delegateData =
|
|---|
| 285 | /** @type {EXPECTED_ANY} */
|
|---|
| 286 | (undefined);
|
|---|
| 287 | }
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | makeSerializable(DelegatedModule, "webpack/lib/dll/DelegatedModule");
|
|---|
| 291 |
|
|---|
| 292 | module.exports = DelegatedModule;
|
|---|