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