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