source: frontend/node_modules/webpack/lib/container/RemoteRuntimeModule.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const RuntimeGlobals = require("../RuntimeGlobals");
9const RuntimeModule = require("../RuntimeModule");
10const Template = require("../Template");
11
12/** @typedef {import("../Chunk")} Chunk */
13/** @typedef {import("../Chunk").ChunkId} ChunkId */
14/** @typedef {import("../ChunkGraph")} ChunkGraph */
15/** @typedef {import("../ChunkGraph").ModuleId} ModuleId */
16/** @typedef {import("../Compilation")} Compilation */
17/** @typedef {import("./RemoteModule")} RemoteModule */
18
19class RemoteRuntimeModule extends RuntimeModule {
20 constructor() {
21 super("remotes loading");
22 }
23
24 /**
25 * Generates runtime code for this runtime module.
26 * @returns {string | null} runtime code
27 */
28 generate() {
29 const compilation = /** @type {Compilation} */ (this.compilation);
30 const chunkGraph = /** @type {ChunkGraph} */ (this.chunkGraph);
31 const { runtimeTemplate, moduleGraph } = compilation;
32 /** @type {Record<ChunkId, ModuleId[]>} */
33 const chunkToRemotesMapping = {};
34 /** @type {Record<ModuleId, [string, string, ModuleId]>} */
35 const idToExternalAndNameMapping = {};
36 for (const chunk of /** @type {Chunk} */ (
37 this.chunk
38 ).getAllReferencedChunks()) {
39 const modules = chunkGraph.getChunkModulesIterableBySourceType(
40 chunk,
41 "remote"
42 );
43 if (!modules) continue;
44 /** @type {ModuleId[]} */
45 const remotes = (chunkToRemotesMapping[
46 /** @type {ChunkId} */
47 (chunk.id)
48 ] = []);
49 for (const m of modules) {
50 const module = /** @type {RemoteModule} */ (m);
51 const name = module.internalRequest;
52 const id = /** @type {ModuleId} */ (chunkGraph.getModuleId(module));
53 const shareScope = module.shareScope;
54 const dep = module.dependencies[0];
55 const externalModule = moduleGraph.getModule(dep);
56 const externalModuleId =
57 /** @type {ModuleId} */
58 (externalModule && chunkGraph.getModuleId(externalModule));
59 remotes.push(id);
60 idToExternalAndNameMapping[id] = [shareScope, name, externalModuleId];
61 }
62 }
63 return Template.asString([
64 `var chunkMapping = ${JSON.stringify(
65 chunkToRemotesMapping,
66 null,
67 "\t"
68 )};`,
69 `var idToExternalAndNameMapping = ${JSON.stringify(
70 idToExternalAndNameMapping,
71 null,
72 "\t"
73 )};`,
74 `${
75 RuntimeGlobals.ensureChunkHandlers
76 }.remotes = ${runtimeTemplate.basicFunction("chunkId, promises", [
77 `if(${RuntimeGlobals.hasOwnProperty}(chunkMapping, chunkId)) {`,
78 Template.indent([
79 `chunkMapping[chunkId].forEach(${runtimeTemplate.basicFunction("id", [
80 `var getScope = ${RuntimeGlobals.currentRemoteGetScope};`,
81 "if(!getScope) getScope = [];",
82 "var data = idToExternalAndNameMapping[id];",
83 "if(getScope.indexOf(data) >= 0) return;",
84 "getScope.push(data);",
85 "if(data.p) return promises.push(data.p);",
86 `var onError = ${runtimeTemplate.basicFunction("error", [
87 'if(!error) error = new Error("Container missing");',
88 'if(typeof error.message === "string")',
89 Template.indent(
90 "error.message += '\\nwhile loading \"' + data[1] + '\" from ' + data[2];"
91 ),
92 `${
93 RuntimeGlobals.moduleFactories
94 }[id] = ${runtimeTemplate.basicFunction("", ["throw error;"])}`,
95 "data.p = 0;"
96 ])};`,
97 `var handleFunction = ${runtimeTemplate.basicFunction(
98 "fn, arg1, arg2, d, next, first",
99 [
100 "try {",
101 Template.indent([
102 "var promise = fn(arg1, arg2);",
103 "if(promise && promise.then) {",
104 Template.indent([
105 `var p = promise.then(${runtimeTemplate.returningFunction(
106 "next(result, d)",
107 "result"
108 )}, onError);`,
109 "if(first) promises.push(data.p = p); else return p;"
110 ]),
111 "} else {",
112 Template.indent(["return next(promise, d, first);"]),
113 "}"
114 ]),
115 "} catch(error) {",
116 Template.indent(["onError(error);"]),
117 "}"
118 ]
119 )}`,
120 `var onExternal = ${runtimeTemplate.returningFunction(
121 `external ? handleFunction(${RuntimeGlobals.initializeSharing}, data[0], 0, external, onInitialized, first) : onError()`,
122 "external, _, first"
123 )};`,
124 `var onInitialized = ${runtimeTemplate.returningFunction(
125 "handleFunction(external.get, data[1], getScope, 0, onFactory, first)",
126 "_, external, first"
127 )};`,
128 `var onFactory = ${runtimeTemplate.basicFunction("factory", [
129 "data.p = 1;",
130 `${
131 RuntimeGlobals.moduleFactories
132 }[id] = ${runtimeTemplate.basicFunction("module", [
133 "module.exports = factory();"
134 ])}`
135 ])};`,
136 `handleFunction(${RuntimeGlobals.require}, data[2], 0, 0, onExternal, 1);`
137 ])});`
138 ]),
139 "}"
140 ])}`
141 ]);
142 }
143}
144
145module.exports = RemoteRuntimeModule;
Note: See TracBrowser for help on using the repository browser.