| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra, Zackary Jackson @ScriptedAlchemy, Marais Rossouw @maraisr
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const { OriginalSource, RawSource } = require("webpack-sources");
|
|---|
| 9 | const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
|
|---|
| 10 | const Module = require("../Module");
|
|---|
| 11 | const {
|
|---|
| 12 | JAVASCRIPT_TYPE,
|
|---|
| 13 | JAVASCRIPT_TYPES
|
|---|
| 14 | } = require("../ModuleSourceTypeConstants");
|
|---|
| 15 | const { JAVASCRIPT_MODULE_TYPE_DYNAMIC } = require("../ModuleTypeConstants");
|
|---|
| 16 | const RuntimeGlobals = require("../RuntimeGlobals");
|
|---|
| 17 | const Template = require("../Template");
|
|---|
| 18 | const StaticExportsDependency = require("../dependencies/StaticExportsDependency");
|
|---|
| 19 | const makeSerializable = require("../util/makeSerializable");
|
|---|
| 20 | const ContainerExposedDependency = require("./ContainerExposedDependency");
|
|---|
| 21 |
|
|---|
| 22 | /** @typedef {import("../config/defaults").WebpackOptionsNormalizedWithDefaults} WebpackOptions */
|
|---|
| 23 | /** @typedef {import("../Compilation")} Compilation */
|
|---|
| 24 | /** @typedef {import("../Module").BuildCallback} BuildCallback */
|
|---|
| 25 | /** @typedef {import("../Module").CodeGenerationContext} CodeGenerationContext */
|
|---|
| 26 | /** @typedef {import("../Module").CodeGenerationResult} CodeGenerationResult */
|
|---|
| 27 | /** @typedef {import("../Module").LibIdentOptions} LibIdentOptions */
|
|---|
| 28 | /** @typedef {import("../Module").LibIdent} LibIdent */
|
|---|
| 29 | /** @typedef {import("../Module").NeedBuildCallback} NeedBuildCallback */
|
|---|
| 30 | /** @typedef {import("../Module").NeedBuildContext} NeedBuildContext */
|
|---|
| 31 | /** @typedef {import("../Module").Sources} Sources */
|
|---|
| 32 | /** @typedef {import("../Module").SourceTypes} SourceTypes */
|
|---|
| 33 | /** @typedef {import("../RequestShortener")} RequestShortener */
|
|---|
| 34 | /** @typedef {import("../ResolverFactory").ResolverWithOptions} ResolverWithOptions */
|
|---|
| 35 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */
|
|---|
| 36 | /** @typedef {import("../serialization/ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */
|
|---|
| 37 | /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * Defines the expose options type used by this module.
|
|---|
| 41 | * @typedef {object} ExposeOptions
|
|---|
| 42 | * @property {string[]} import requests to exposed modules (last one is exported)
|
|---|
| 43 | * @property {string} name custom chunk name for the exposed module
|
|---|
| 44 | */
|
|---|
| 45 |
|
|---|
| 46 | /** @typedef {[string, ExposeOptions][]} ExposesList */
|
|---|
| 47 |
|
|---|
| 48 | class ContainerEntryModule extends Module {
|
|---|
| 49 | /**
|
|---|
| 50 | * Creates an instance of ContainerEntryModule.
|
|---|
| 51 | * @param {string} name container entry name
|
|---|
| 52 | * @param {ExposesList} exposes list of exposed modules
|
|---|
| 53 | * @param {string} shareScope name of the share scope
|
|---|
| 54 | */
|
|---|
| 55 | constructor(name, exposes, shareScope) {
|
|---|
| 56 | super(JAVASCRIPT_MODULE_TYPE_DYNAMIC, null);
|
|---|
| 57 | /** @type {string} */
|
|---|
| 58 | this._name = name;
|
|---|
| 59 | /** @type {ExposesList} */
|
|---|
| 60 | this._exposes = exposes;
|
|---|
| 61 | /** @type {string} */
|
|---|
| 62 | this._shareScope = shareScope;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Returns the source types this module can generate.
|
|---|
| 67 | * @returns {SourceTypes} types available (do not mutate)
|
|---|
| 68 | */
|
|---|
| 69 | getSourceTypes() {
|
|---|
| 70 | return JAVASCRIPT_TYPES;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /**
|
|---|
| 74 | * Returns the unique identifier used to reference this module.
|
|---|
| 75 | * @returns {string} a unique identifier of the module
|
|---|
| 76 | */
|
|---|
| 77 | identifier() {
|
|---|
| 78 | return `container entry (${this._shareScope}) ${JSON.stringify(
|
|---|
| 79 | this._exposes
|
|---|
| 80 | )}`;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * Returns a human-readable identifier for this module.
|
|---|
| 85 | * @param {RequestShortener} requestShortener the request shortener
|
|---|
| 86 | * @returns {string} a user readable identifier of the module
|
|---|
| 87 | */
|
|---|
| 88 | readableIdentifier(requestShortener) {
|
|---|
| 89 | return "container entry";
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /**
|
|---|
| 93 | * Gets the library identifier.
|
|---|
| 94 | * @param {LibIdentOptions} options options
|
|---|
| 95 | * @returns {LibIdent | null} an identifier for library inclusion
|
|---|
| 96 | */
|
|---|
| 97 | libIdent(options) {
|
|---|
| 98 | return `${this.layer ? `(${this.layer})/` : ""}webpack/container/entry/${
|
|---|
| 99 | this._name
|
|---|
| 100 | }`;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * Checks whether the module needs to be rebuilt for the current build state.
|
|---|
| 105 | * @param {NeedBuildContext} context context info
|
|---|
| 106 | * @param {NeedBuildCallback} callback callback function, returns true, if the module needs a rebuild
|
|---|
| 107 | * @returns {void}
|
|---|
| 108 | */
|
|---|
| 109 | needBuild(context, callback) {
|
|---|
| 110 | return callback(null, !this.buildMeta);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /**
|
|---|
| 114 | * Builds the module using the provided compilation context.
|
|---|
| 115 | * @param {WebpackOptions} options webpack options
|
|---|
| 116 | * @param {Compilation} compilation the compilation
|
|---|
| 117 | * @param {ResolverWithOptions} resolver the resolver
|
|---|
| 118 | * @param {InputFileSystem} fs the file system
|
|---|
| 119 | * @param {BuildCallback} callback callback function
|
|---|
| 120 | * @returns {void}
|
|---|
| 121 | */
|
|---|
| 122 | build(options, compilation, resolver, fs, callback) {
|
|---|
| 123 | this.buildMeta = {};
|
|---|
| 124 | this.buildInfo = {
|
|---|
| 125 | strict: true,
|
|---|
| 126 | topLevelDeclarations: new Set(["moduleMap", "get", "init"])
|
|---|
| 127 | };
|
|---|
| 128 | this.buildMeta.exportsType = "namespace";
|
|---|
| 129 |
|
|---|
| 130 | this.clearDependenciesAndBlocks();
|
|---|
| 131 |
|
|---|
| 132 | for (const [name, options] of this._exposes) {
|
|---|
| 133 | const block = new AsyncDependenciesBlock(
|
|---|
| 134 | {
|
|---|
| 135 | name: options.name
|
|---|
| 136 | },
|
|---|
| 137 | { name },
|
|---|
| 138 | options.import[options.import.length - 1]
|
|---|
| 139 | );
|
|---|
| 140 | let idx = 0;
|
|---|
| 141 | for (const request of options.import) {
|
|---|
| 142 | const dep = new ContainerExposedDependency(name, request);
|
|---|
| 143 | dep.loc = {
|
|---|
| 144 | name,
|
|---|
| 145 | index: idx++
|
|---|
| 146 | };
|
|---|
| 147 |
|
|---|
| 148 | block.addDependency(dep);
|
|---|
| 149 | }
|
|---|
| 150 | this.addBlock(block);
|
|---|
| 151 | }
|
|---|
| 152 | this.addDependency(new StaticExportsDependency(["get", "init"], false));
|
|---|
| 153 |
|
|---|
| 154 | callback();
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /**
|
|---|
| 158 | * Generates code and runtime requirements for this module.
|
|---|
| 159 | * @param {CodeGenerationContext} context context for code generation
|
|---|
| 160 | * @returns {CodeGenerationResult} result
|
|---|
| 161 | */
|
|---|
| 162 | codeGeneration({ moduleGraph, chunkGraph, runtimeTemplate }) {
|
|---|
| 163 | /** @type {Sources} */
|
|---|
| 164 | const sources = new Map();
|
|---|
| 165 | const runtimeRequirements = new Set([
|
|---|
| 166 | RuntimeGlobals.definePropertyGetters,
|
|---|
| 167 | RuntimeGlobals.hasOwnProperty,
|
|---|
| 168 | RuntimeGlobals.exports
|
|---|
| 169 | ]);
|
|---|
| 170 | /** @type {string[]} */
|
|---|
| 171 | const getters = [];
|
|---|
| 172 |
|
|---|
| 173 | for (const block of this.blocks) {
|
|---|
| 174 | const { dependencies } = block;
|
|---|
| 175 |
|
|---|
| 176 | const modules = dependencies.map((dependency) => {
|
|---|
| 177 | const dep = /** @type {ContainerExposedDependency} */ (dependency);
|
|---|
| 178 | return {
|
|---|
| 179 | name: dep.exposedName,
|
|---|
| 180 | module: moduleGraph.getModule(dep),
|
|---|
| 181 | request: dep.userRequest
|
|---|
| 182 | };
|
|---|
| 183 | });
|
|---|
| 184 |
|
|---|
| 185 | /** @type {string} */
|
|---|
| 186 | let str;
|
|---|
| 187 |
|
|---|
| 188 | if (modules.some((m) => !m.module)) {
|
|---|
| 189 | str = runtimeTemplate.throwMissingModuleErrorBlock({
|
|---|
| 190 | request: modules.map((m) => m.request).join(", ")
|
|---|
| 191 | });
|
|---|
| 192 | } else {
|
|---|
| 193 | str = `return ${runtimeTemplate.blockPromise({
|
|---|
| 194 | block,
|
|---|
| 195 | message: "",
|
|---|
| 196 | chunkGraph,
|
|---|
| 197 | runtimeRequirements
|
|---|
| 198 | })}.then(${runtimeTemplate.returningFunction(
|
|---|
| 199 | runtimeTemplate.returningFunction(
|
|---|
| 200 | `(${modules
|
|---|
| 201 | .map(({ module, request }) =>
|
|---|
| 202 | runtimeTemplate.moduleRaw({
|
|---|
| 203 | module,
|
|---|
| 204 | chunkGraph,
|
|---|
| 205 | request,
|
|---|
| 206 | weak: false,
|
|---|
| 207 | runtimeRequirements
|
|---|
| 208 | })
|
|---|
| 209 | )
|
|---|
| 210 | .join(", ")})`
|
|---|
| 211 | )
|
|---|
| 212 | )});`;
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | getters.push(
|
|---|
| 216 | `${JSON.stringify(modules[0].name)}: ${runtimeTemplate.basicFunction(
|
|---|
| 217 | "",
|
|---|
| 218 | str
|
|---|
| 219 | )}`
|
|---|
| 220 | );
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | const source = Template.asString([
|
|---|
| 224 | "var moduleMap = {",
|
|---|
| 225 | Template.indent(getters.join(",\n")),
|
|---|
| 226 | "};",
|
|---|
| 227 | `var get = ${runtimeTemplate.basicFunction("module, getScope", [
|
|---|
| 228 | `${RuntimeGlobals.currentRemoteGetScope} = getScope;`,
|
|---|
| 229 | // reusing the getScope variable to avoid creating a new var (and module is also used later)
|
|---|
| 230 | "getScope = (",
|
|---|
| 231 | Template.indent([
|
|---|
| 232 | `${RuntimeGlobals.hasOwnProperty}(moduleMap, module)`,
|
|---|
| 233 | Template.indent([
|
|---|
| 234 | "? moduleMap[module]()",
|
|---|
| 235 | `: Promise.resolve().then(${runtimeTemplate.basicFunction(
|
|---|
| 236 | "",
|
|---|
| 237 | "throw new Error('Module \"' + module + '\" does not exist in container.');"
|
|---|
| 238 | )})`
|
|---|
| 239 | ])
|
|---|
| 240 | ]),
|
|---|
| 241 | ");",
|
|---|
| 242 | `${RuntimeGlobals.currentRemoteGetScope} = undefined;`,
|
|---|
| 243 | "return getScope;"
|
|---|
| 244 | ])};`,
|
|---|
| 245 | `var init = ${runtimeTemplate.basicFunction("shareScope, initScope", [
|
|---|
| 246 | `if (!${RuntimeGlobals.shareScopeMap}) return;`,
|
|---|
| 247 | `var name = ${JSON.stringify(this._shareScope)}`,
|
|---|
| 248 | `var oldScope = ${RuntimeGlobals.shareScopeMap}[name];`,
|
|---|
| 249 | 'if(oldScope && oldScope !== shareScope) throw new Error("Container initialization failed as it has already been initialized with a different share scope");',
|
|---|
| 250 | `${RuntimeGlobals.shareScopeMap}[name] = shareScope;`,
|
|---|
| 251 | `return ${RuntimeGlobals.initializeSharing}(name, initScope);`
|
|---|
| 252 | ])};`,
|
|---|
| 253 | "",
|
|---|
| 254 | "// This exports getters to disallow modifications",
|
|---|
| 255 | `${RuntimeGlobals.definePropertyGetters}(exports, {`,
|
|---|
| 256 | Template.indent([
|
|---|
| 257 | `get: ${runtimeTemplate.returningFunction("get")},`,
|
|---|
| 258 | `init: ${runtimeTemplate.returningFunction("init")}`
|
|---|
| 259 | ]),
|
|---|
| 260 | "});"
|
|---|
| 261 | ]);
|
|---|
| 262 |
|
|---|
| 263 | sources.set(
|
|---|
| 264 | JAVASCRIPT_TYPE,
|
|---|
| 265 | this.useSourceMap || this.useSimpleSourceMap
|
|---|
| 266 | ? new OriginalSource(source, "webpack/container-entry")
|
|---|
| 267 | : new RawSource(source)
|
|---|
| 268 | );
|
|---|
| 269 |
|
|---|
| 270 | return {
|
|---|
| 271 | sources,
|
|---|
| 272 | runtimeRequirements
|
|---|
| 273 | };
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | /**
|
|---|
| 277 | * Returns the estimated size for the requested source type.
|
|---|
| 278 | * @param {string=} type the source type for which the size should be estimated
|
|---|
| 279 | * @returns {number} the estimated size of the module (must be non-zero)
|
|---|
| 280 | */
|
|---|
| 281 | size(type) {
|
|---|
| 282 | return 42;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | /**
|
|---|
| 286 | * Serializes this instance into the provided serializer context.
|
|---|
| 287 | * @param {ObjectSerializerContext} context context
|
|---|
| 288 | */
|
|---|
| 289 | serialize(context) {
|
|---|
| 290 | const { write } = context;
|
|---|
| 291 | write(this._name);
|
|---|
| 292 | write(this._exposes);
|
|---|
| 293 | write(this._shareScope);
|
|---|
| 294 | super.serialize(context);
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | /**
|
|---|
| 298 | * Restores this instance from the provided deserializer context.
|
|---|
| 299 | * @param {ObjectDeserializerContext} context context
|
|---|
| 300 | * @returns {ContainerEntryModule} deserialized container entry module
|
|---|
| 301 | */
|
|---|
| 302 | static deserialize(context) {
|
|---|
| 303 | const { read } = context;
|
|---|
| 304 | const obj = new ContainerEntryModule(read(), read(), read());
|
|---|
| 305 | obj.deserialize(context);
|
|---|
| 306 | return obj;
|
|---|
| 307 | }
|
|---|
| 308 | }
|
|---|
| 309 |
|
|---|
| 310 | makeSerializable(
|
|---|
| 311 | ContainerEntryModule,
|
|---|
| 312 | "webpack/lib/container/ContainerEntryModule"
|
|---|
| 313 | );
|
|---|
| 314 |
|
|---|
| 315 | module.exports = ContainerEntryModule;
|
|---|