123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- "use strict";
- const AsyncDependenciesBlock = require("../AsyncDependenciesBlock");
- const ExternalModule = require("../ExternalModule");
- const { STAGE_ADVANCED } = require("../OptimizationStages");
- const memoize = require("../util/memoize");
- const { forEachRuntime } = require("../util/runtime");
- const getModuleFederationPlugin = memoize(() =>
- require("./ModuleFederationPlugin")
- );
- const PLUGIN_NAME = "HoistContainerReferences";
- class HoistContainerReferences {
-
- apply(compiler) {
- compiler.hooks.thisCompilation.tap(PLUGIN_NAME, compilation => {
- const hooks =
- getModuleFederationPlugin().getCompilationHooks(compilation);
- const depsToTrace = new Set();
- const entryExternalsToHoist = new Set();
- hooks.addContainerEntryDependency.tap(PLUGIN_NAME, dep => {
- depsToTrace.add(dep);
- });
- hooks.addFederationRuntimeDependency.tap(PLUGIN_NAME, dep => {
- depsToTrace.add(dep);
- });
- compilation.hooks.addEntry.tap(PLUGIN_NAME, entryDep => {
- if (entryDep.type === "entry") {
- entryExternalsToHoist.add(entryDep);
- }
- });
-
- compilation.hooks.optimizeChunks.tap(
- {
- name: PLUGIN_NAME,
-
- stage: STAGE_ADVANCED + 1
- },
- chunks => {
- this.hoistModulesInChunks(
- compilation,
- depsToTrace,
- entryExternalsToHoist
- );
- }
- );
- });
- }
-
- hoistModulesInChunks(compilation, depsToTrace, entryExternalsToHoist) {
- const { chunkGraph, moduleGraph } = compilation;
-
- for (const dep of entryExternalsToHoist) {
- const entryModule = moduleGraph.getModule(dep);
- if (!entryModule) continue;
-
- const allReferencedModules = getAllReferencedModules(
- compilation,
- entryModule,
- "external",
- false
- );
- const containerRuntimes = chunkGraph.getModuleRuntimes(entryModule);
- const runtimes = new Set();
- for (const runtimeSpec of containerRuntimes) {
- forEachRuntime(runtimeSpec, runtimeKey => {
- if (runtimeKey) {
- runtimes.add(runtimeKey);
- }
- });
- }
- for (const runtime of runtimes) {
- const runtimeChunk = compilation.namedChunks.get(runtime);
- if (!runtimeChunk) continue;
- for (const module of allReferencedModules) {
- if (!chunkGraph.isModuleInChunk(module, runtimeChunk)) {
- chunkGraph.connectChunkAndModule(runtimeChunk, module);
- }
- }
- }
- this.cleanUpChunks(compilation, allReferencedModules);
- }
-
- for (const dep of depsToTrace) {
- const containerEntryModule = moduleGraph.getModule(dep);
- if (!containerEntryModule) continue;
- const allReferencedModules = getAllReferencedModules(
- compilation,
- containerEntryModule,
- "initial",
- false
- );
- const allRemoteReferences = getAllReferencedModules(
- compilation,
- containerEntryModule,
- "external",
- false
- );
- for (const remote of allRemoteReferences) {
- allReferencedModules.add(remote);
- }
- const containerRuntimes =
- chunkGraph.getModuleRuntimes(containerEntryModule);
- const runtimes = new Set();
- for (const runtimeSpec of containerRuntimes) {
- forEachRuntime(runtimeSpec, runtimeKey => {
- if (runtimeKey) {
- runtimes.add(runtimeKey);
- }
- });
- }
- for (const runtime of runtimes) {
- const runtimeChunk = compilation.namedChunks.get(runtime);
- if (!runtimeChunk) continue;
- for (const module of allReferencedModules) {
- if (!chunkGraph.isModuleInChunk(module, runtimeChunk)) {
- chunkGraph.connectChunkAndModule(runtimeChunk, module);
- }
- }
- }
- this.cleanUpChunks(compilation, allReferencedModules);
- }
- }
-
- cleanUpChunks(compilation, modules) {
- const { chunkGraph } = compilation;
- for (const module of modules) {
- for (const chunk of chunkGraph.getModuleChunks(module)) {
- if (!chunk.hasRuntime()) {
- chunkGraph.disconnectChunkAndModule(chunk, module);
- if (
- chunkGraph.getNumberOfChunkModules(chunk) === 0 &&
- chunkGraph.getNumberOfEntryModules(chunk) === 0
- ) {
- chunkGraph.disconnectChunk(chunk);
- compilation.chunks.delete(chunk);
- if (chunk.name) {
- compilation.namedChunks.delete(chunk.name);
- }
- }
- }
- }
- }
- modules.clear();
- }
- }
- function getAllReferencedModules(compilation, module, type, includeInitial) {
- const collectedModules = new Set(includeInitial ? [module] : []);
- const visitedModules = new WeakSet([module]);
- const stack = [module];
- while (stack.length > 0) {
- const currentModule = stack.pop();
- if (!currentModule) continue;
- const outgoingConnections =
- compilation.moduleGraph.getOutgoingConnections(currentModule);
- if (outgoingConnections) {
- for (const connection of outgoingConnections) {
- const connectedModule = connection.module;
-
- if (!connectedModule || visitedModules.has(connectedModule)) {
- continue;
- }
-
- if (type === "initial") {
- const parentBlock = compilation.moduleGraph.getParentBlock(
-
- (connection.dependency)
- );
- if (parentBlock instanceof AsyncDependenciesBlock) {
- continue;
- }
- }
-
- if (type === "external") {
- if (connection.module instanceof ExternalModule) {
- collectedModules.add(connectedModule);
- }
- } else {
-
- collectedModules.add(connectedModule);
- }
-
- visitedModules.add(connectedModule);
- stack.push(connectedModule);
- }
- }
- }
- return collectedModules;
- }
- module.exports = HoistContainerReferences;
|