[79a0317] | 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 { JS_TYPES } = require("./ModuleSourceTypesConstants");
|
---|
| 11 | const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("./ModuleTypeConstants");
|
---|
| 12 | const RuntimeGlobals = require("./RuntimeGlobals");
|
---|
| 13 | const DelegatedSourceDependency = require("./dependencies/DelegatedSourceDependency");
|
---|
| 14 | const StaticExportsDependency = require("./dependencies/StaticExportsDependency");
|
---|
| 15 | const makeSerializable = require("./util/makeSerializable");
|
---|
| 16 |
|
---|
| 17 | /** @typedef {import("webpack-sources").Source} Source */
|
---|
| 18 | /** @typedef {import("../declarations/WebpackOptions").WebpackOptionsNormalized} WebpackOptions */
|
---|
| 19 | /** @typedef {import("./ChunkGraph")} ChunkGraph */
|
---|
| 20 | /** @typedef {import("./Compilation")} Compilation */
|
---|
| 21 | /** @typedef {import("./Dependency").UpdateHashContext} UpdateHashContext */
|
---|
| 22 | /** @typedef {import("./DependencyTemplates")} DependencyTemplates */
|
---|
| 23 | /** @typedef {import("./Generator").SourceTypes} SourceTypes */
|
---|
| 24 | /** @typedef {import("./LibManifestPlugin").ManifestModuleData} ManifestModuleData */
|
---|
| 25 | /** @typedef {import("./Module").CodeGenerationContext} CodeGenerationContext */
|
---|
| 26 | /** @typedef {import("./Module").CodeGenerationResult} CodeGenerationResult */
|
---|
| 27 | /** @typedef {import("./Module").LibIdentOptions} LibIdentOptions */
|
---|
| 28 | /** @typedef {import("./Module").NeedBuildContext} NeedBuildContext */
|
---|
| 29 | /** @typedef {import("./Module").SourceContext} SourceContext */
|
---|
| 30 | /** @typedef {import("./RequestShortener")} RequestShortener */
|
---|
| 31 | /** @typedef {import("./ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
---|
| 32 | /** @typedef {import("./RuntimeTemplate")} RuntimeTemplate */
|
---|
| 33 | /** @typedef {import("./WebpackError")} WebpackError */
|
---|
| 34 | /** @typedef {import("./dependencies/ModuleDependency")} ModuleDependency */
|
---|
| 35 | /** @typedef {import("./serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
---|
| 36 | /** @typedef {import("./serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
---|
| 37 | /** @typedef {import("./util/Hash")} Hash */
|
---|
| 38 | /** @typedef {import("./util/fs").InputFileSystem} InputFileSystem */
|
---|
| 39 |
|
---|
| 40 | /** @typedef {string} SourceRequest */
|
---|
| 41 | /** @typedef {"require" | "object"} Type */
|
---|
| 42 | /** @typedef {TODO} Data */
|
---|
| 43 |
|
---|
| 44 | const RUNTIME_REQUIREMENTS = new Set([
|
---|
| 45 | RuntimeGlobals.module,
|
---|
| 46 | RuntimeGlobals.require
|
---|
| 47 | ]);
|
---|
| 48 |
|
---|
| 49 | class DelegatedModule extends Module {
|
---|
| 50 | /**
|
---|
| 51 | * @param {SourceRequest} sourceRequest source request
|
---|
| 52 | * @param {Data} data data
|
---|
| 53 | * @param {Type} type type
|
---|
| 54 | * @param {string} userRequest user request
|
---|
| 55 | * @param {string | Module} originalRequest original request
|
---|
| 56 | */
|
---|
| 57 | constructor(sourceRequest, data, type, userRequest, originalRequest) {
|
---|
| 58 | super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, null);
|
---|
| 59 |
|
---|
| 60 | // Info from Factory
|
---|
| 61 | this.sourceRequest = sourceRequest;
|
---|
| 62 | this.request = data.id;
|
---|
| 63 | this.delegationType = type;
|
---|
| 64 | this.userRequest = userRequest;
|
---|
| 65 | this.originalRequest = originalRequest;
|
---|
| 66 | /** @type {ManifestModuleData | undefined} */
|
---|
| 67 | this.delegateData = data;
|
---|
| 68 |
|
---|
| 69 | // Build info
|
---|
| 70 | this.delegatedSourceDependency = undefined;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /**
|
---|
| 74 | * @returns {SourceTypes} types available (do not mutate)
|
---|
| 75 | */
|
---|
| 76 | getSourceTypes() {
|
---|
| 77 | return JS_TYPES;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * @param {LibIdentOptions} options options
|
---|
| 82 | * @returns {string | null} an identifier for library inclusion
|
---|
| 83 | */
|
---|
| 84 | libIdent(options) {
|
---|
| 85 | return typeof this.originalRequest === "string"
|
---|
| 86 | ? this.originalRequest
|
---|
| 87 | : this.originalRequest.libIdent(options);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | /**
|
---|
| 91 | * @returns {string} a unique identifier of the module
|
---|
| 92 | */
|
---|
| 93 | identifier() {
|
---|
| 94 | return `delegated ${JSON.stringify(this.request)} from ${
|
---|
| 95 | this.sourceRequest
|
---|
| 96 | }`;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /**
|
---|
| 100 | * @param {RequestShortener} requestShortener the request shortener
|
---|
| 101 | * @returns {string} a user readable identifier of the module
|
---|
| 102 | */
|
---|
| 103 | readableIdentifier(requestShortener) {
|
---|
| 104 | return `delegated ${this.userRequest} from ${this.sourceRequest}`;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | /**
|
---|
| 108 | * @param {NeedBuildContext} context context info
|
---|
| 109 | * @param {function((WebpackError | null)=, boolean=): void} callback callback function, returns true, if the module needs a rebuild
|
---|
| 110 | * @returns {void}
|
---|
| 111 | */
|
---|
| 112 | needBuild(context, callback) {
|
---|
| 113 | return callback(null, !this.buildMeta);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /**
|
---|
| 117 | * @param {WebpackOptions} options webpack options
|
---|
| 118 | * @param {Compilation} compilation the compilation
|
---|
| 119 | * @param {ResolverWithOptions} resolver the resolver
|
---|
| 120 | * @param {InputFileSystem} fs the file system
|
---|
| 121 | * @param {function(WebpackError=): void} callback callback function
|
---|
| 122 | * @returns {void}
|
---|
| 123 | */
|
---|
| 124 | build(options, compilation, resolver, fs, callback) {
|
---|
| 125 | const delegateData = /** @type {ManifestModuleData} */ (this.delegateData);
|
---|
| 126 | this.buildMeta = { ...delegateData.buildMeta };
|
---|
| 127 | this.buildInfo = {};
|
---|
| 128 | this.dependencies.length = 0;
|
---|
| 129 | this.delegatedSourceDependency = new DelegatedSourceDependency(
|
---|
| 130 | this.sourceRequest
|
---|
| 131 | );
|
---|
| 132 | this.addDependency(this.delegatedSourceDependency);
|
---|
| 133 | this.addDependency(
|
---|
| 134 | new StaticExportsDependency(delegateData.exports || true, false)
|
---|
| 135 | );
|
---|
| 136 | callback();
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /**
|
---|
| 140 | * @param {CodeGenerationContext} context context for code generation
|
---|
| 141 | * @returns {CodeGenerationResult} result
|
---|
| 142 | */
|
---|
| 143 | codeGeneration({ runtimeTemplate, moduleGraph, chunkGraph }) {
|
---|
| 144 | const dep = /** @type {DelegatedSourceDependency} */ (this.dependencies[0]);
|
---|
| 145 | const sourceModule = moduleGraph.getModule(dep);
|
---|
| 146 | let str;
|
---|
| 147 |
|
---|
| 148 | if (!sourceModule) {
|
---|
| 149 | str = runtimeTemplate.throwMissingModuleErrorBlock({
|
---|
| 150 | request: this.sourceRequest
|
---|
| 151 | });
|
---|
| 152 | } else {
|
---|
| 153 | str = `module.exports = (${runtimeTemplate.moduleExports({
|
---|
| 154 | module: sourceModule,
|
---|
| 155 | chunkGraph,
|
---|
| 156 | request: dep.request,
|
---|
| 157 | runtimeRequirements: new Set()
|
---|
| 158 | })})`;
|
---|
| 159 |
|
---|
| 160 | switch (this.delegationType) {
|
---|
| 161 | case "require":
|
---|
| 162 | str += `(${JSON.stringify(this.request)})`;
|
---|
| 163 | break;
|
---|
| 164 | case "object":
|
---|
| 165 | str += `[${JSON.stringify(this.request)}]`;
|
---|
| 166 | break;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | str += ";";
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | const sources = new Map();
|
---|
| 173 | if (this.useSourceMap || this.useSimpleSourceMap) {
|
---|
| 174 | sources.set("javascript", new OriginalSource(str, this.identifier()));
|
---|
| 175 | } else {
|
---|
| 176 | sources.set("javascript", new RawSource(str));
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | return {
|
---|
| 180 | sources,
|
---|
| 181 | runtimeRequirements: RUNTIME_REQUIREMENTS
|
---|
| 182 | };
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /**
|
---|
| 186 | * @param {string=} type the source type for which the size should be estimated
|
---|
| 187 | * @returns {number} the estimated size of the module (must be non-zero)
|
---|
| 188 | */
|
---|
| 189 | size(type) {
|
---|
| 190 | return 42;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /**
|
---|
| 194 | * @param {Hash} hash the hash used to track dependencies
|
---|
| 195 | * @param {UpdateHashContext} context context
|
---|
| 196 | * @returns {void}
|
---|
| 197 | */
|
---|
| 198 | updateHash(hash, context) {
|
---|
| 199 | hash.update(this.delegationType);
|
---|
| 200 | hash.update(JSON.stringify(this.request));
|
---|
| 201 | super.updateHash(hash, context);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | /**
|
---|
| 205 | * @param {ObjectSerializerContext} context context
|
---|
| 206 | */
|
---|
| 207 | serialize(context) {
|
---|
| 208 | const { write } = context;
|
---|
| 209 | // constructor
|
---|
| 210 | write(this.sourceRequest);
|
---|
| 211 | write(this.delegateData);
|
---|
| 212 | write(this.delegationType);
|
---|
| 213 | write(this.userRequest);
|
---|
| 214 | write(this.originalRequest);
|
---|
| 215 | super.serialize(context);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | /**
|
---|
| 219 | * @param {ObjectDeserializerContext} context context\
|
---|
| 220 | * @returns {DelegatedModule} DelegatedModule
|
---|
| 221 | */
|
---|
| 222 | static deserialize(context) {
|
---|
| 223 | const { read } = context;
|
---|
| 224 | const obj = new DelegatedModule(
|
---|
| 225 | read(), // sourceRequest
|
---|
| 226 | read(), // delegateData
|
---|
| 227 | read(), // delegationType
|
---|
| 228 | read(), // userRequest
|
---|
| 229 | read() // originalRequest
|
---|
| 230 | );
|
---|
| 231 | obj.deserialize(context);
|
---|
| 232 | return obj;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /**
|
---|
| 236 | * Assuming this module is in the cache. Update the (cached) module with
|
---|
| 237 | * the fresh module from the factory. Usually updates internal references
|
---|
| 238 | * and properties.
|
---|
| 239 | * @param {Module} module fresh module
|
---|
| 240 | * @returns {void}
|
---|
| 241 | */
|
---|
| 242 | updateCacheModule(module) {
|
---|
| 243 | super.updateCacheModule(module);
|
---|
| 244 | const m = /** @type {DelegatedModule} */ (module);
|
---|
| 245 | this.delegationType = m.delegationType;
|
---|
| 246 | this.userRequest = m.userRequest;
|
---|
| 247 | this.originalRequest = m.originalRequest;
|
---|
| 248 | this.delegateData = m.delegateData;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /**
|
---|
| 252 | * Assuming this module is in the cache. Remove internal references to allow freeing some memory.
|
---|
| 253 | */
|
---|
| 254 | cleanupForCache() {
|
---|
| 255 | super.cleanupForCache();
|
---|
| 256 | this.delegateData = undefined;
|
---|
| 257 | }
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | makeSerializable(DelegatedModule, "webpack/lib/DelegatedModule");
|
---|
| 261 |
|
---|
| 262 | module.exports = DelegatedModule;
|
---|