2020-02-25 04:18:14 +08:00
|
|
|
/*
|
|
|
|
MIT License http://www.opensource.org/licenses/mit-license.php
|
|
|
|
Author Tobias Koppers @sokra
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const RuntimeGlobals = require("../RuntimeGlobals");
|
|
|
|
const RuntimeModule = require("../RuntimeModule");
|
|
|
|
const Template = require("../Template");
|
|
|
|
|
|
|
|
/** @typedef {import("./RemoteModule")} RemoteModule */
|
|
|
|
|
|
|
|
class RemoteRuntimeModule extends RuntimeModule {
|
|
|
|
constructor() {
|
|
|
|
super("remotes loading");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @returns {string} runtime code
|
|
|
|
*/
|
|
|
|
generate() {
|
|
|
|
const { runtimeTemplate, chunkGraph, moduleGraph } = this.compilation;
|
|
|
|
const chunkToRemotesMapping = {};
|
|
|
|
const idToExternalAndNameMapping = {};
|
|
|
|
for (const chunk of this.chunk.getAllAsyncChunks()) {
|
|
|
|
const modules = chunkGraph.getChunkModulesIterableBySourceType(
|
|
|
|
chunk,
|
|
|
|
"remote"
|
|
|
|
);
|
|
|
|
if (!modules) continue;
|
|
|
|
const remotes = (chunkToRemotesMapping[chunk.id] = []);
|
|
|
|
for (const m of modules) {
|
|
|
|
const module = /** @type {RemoteModule} */ (m);
|
|
|
|
const name = module.internalRequest;
|
|
|
|
const id = chunkGraph.getModuleId(module);
|
|
|
|
const externalModule = moduleGraph.getModule(module.dependencies[0]);
|
2020-05-04 21:00:23 +08:00
|
|
|
const externalModuleId =
|
|
|
|
externalModule && chunkGraph.getModuleId(externalModule);
|
|
|
|
const overridesModule = moduleGraph.getModule(module.dependencies[1]);
|
|
|
|
const overridesModuleId =
|
|
|
|
overridesModule && chunkGraph.getModuleId(overridesModule);
|
2020-02-25 04:18:14 +08:00
|
|
|
remotes.push(id);
|
2020-05-04 21:00:23 +08:00
|
|
|
idToExternalAndNameMapping[id] = [
|
|
|
|
overridesModuleId,
|
|
|
|
externalModuleId,
|
|
|
|
name
|
|
|
|
];
|
2020-02-25 04:18:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Template.asString([
|
|
|
|
`var chunkMapping = ${JSON.stringify(
|
|
|
|
chunkToRemotesMapping,
|
|
|
|
null,
|
|
|
|
"\t"
|
|
|
|
)};`,
|
|
|
|
`var idToExternalAndNameMapping = ${JSON.stringify(
|
|
|
|
idToExternalAndNameMapping,
|
|
|
|
null,
|
|
|
|
"\t"
|
|
|
|
)};`,
|
|
|
|
`${
|
|
|
|
RuntimeGlobals.ensureChunkHandlers
|
|
|
|
}.remotes = ${runtimeTemplate.basicFunction("chunkId, promises", [
|
|
|
|
`if(${RuntimeGlobals.hasOwnProperty}(chunkMapping, chunkId)) {`,
|
|
|
|
Template.indent([
|
|
|
|
`chunkMapping[chunkId].forEach(${runtimeTemplate.basicFunction("id", [
|
|
|
|
"if(__webpack_modules__[id]) return;",
|
|
|
|
"var data = idToExternalAndNameMapping[id];",
|
2020-05-04 21:00:23 +08:00
|
|
|
`promises.push(Promise.resolve(__webpack_require__(data[0])(__webpack_require__(data[1])).get(data[2])).then(${runtimeTemplate.basicFunction(
|
2020-02-25 04:18:14 +08:00
|
|
|
"factory",
|
|
|
|
[
|
|
|
|
`__webpack_modules__[id] = ${runtimeTemplate.basicFunction(
|
|
|
|
"module",
|
2020-02-27 00:16:01 +08:00
|
|
|
["module.exports = factory();"]
|
2020-02-25 04:18:14 +08:00
|
|
|
)}`
|
|
|
|
]
|
|
|
|
)}))`
|
|
|
|
])});`
|
|
|
|
]),
|
|
|
|
"}"
|
|
|
|
])}`
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = RemoteRuntimeModule;
|