@module-federation/enhanced
Advanced tools
Comparing version 0.0.0-next-20240129035206 to 0.0.0-next-20240131231251
@@ -31,4 +31,3 @@ { | ||
"devDependencies": { | ||
"@module-federation/webpack-bundler-runtime": "workspace:*", | ||
"terser-webpack-plugin": "^5.3.10" | ||
"@module-federation/webpack-bundler-runtime": "workspace:*" | ||
}, | ||
@@ -35,0 +34,0 @@ "dependencies": { |
import type { Compiler, WebpackPluginInstance } from 'webpack'; | ||
import ContainerEntryModule from './ContainerEntryModule'; | ||
/** | ||
@@ -8,11 +7,7 @@ * This class is used to hoist container references in the code. | ||
export declare class HoistContainerReferences implements WebpackPluginInstance { | ||
private containerEntryModules; | ||
constructor(); | ||
getContainerEntryModules(): Set<ContainerEntryModule>; | ||
apply(compiler: Compiler): void; | ||
private chunkContainsContainerEntryModule; | ||
private clearEntry; | ||
private hoistModulesInChunk; | ||
private sortNameChunks; | ||
private getRuntimeChunks; | ||
} | ||
export default HoistContainerReferences; |
@@ -13,35 +13,10 @@ "use strict"; | ||
class HoistContainerReferences { | ||
constructor() { | ||
this.containerEntryModules = new Set(); | ||
} | ||
getContainerEntryModules() { | ||
return this.containerEntryModules; | ||
} | ||
apply(compiler) { | ||
compiler.hooks.thisCompilation.tap('HoistContainerReferences', (compilation) => { | ||
compilation.hooks.optimizeChunkModules.tap('HoistContainerReferences', (chunks) => { | ||
const realContainers = new Map(); | ||
for (const chunk of chunks) { | ||
if (this.chunkContainsContainerEntryModule(chunk, compilation)) { | ||
realContainers.set(chunk.name, chunk); | ||
} | ||
} | ||
for (const chunk of chunks) { | ||
if (realContainers.has(chunk.name)) | ||
continue; | ||
this.clearEntry(chunk, compilation, realContainers); | ||
} | ||
}); | ||
compilation.hooks.afterOptimizeChunks.tap('HoistContainerReferences', (chunks) => { | ||
const realContainers = new Map(); | ||
for (const chunk of chunks) { | ||
if (this.chunkContainsContainerEntryModule(chunk, compilation)) { | ||
realContainers.set(chunk.name, chunk); | ||
this.hoistModulesInChunk(chunk, compilation); | ||
} | ||
} | ||
for (const chunk of chunks) { | ||
if (realContainers.has(chunk.name)) | ||
continue; | ||
this.hoistModulesInChunk(chunk, compilation, realContainers); | ||
} | ||
}); | ||
@@ -53,3 +28,2 @@ }); | ||
if (module instanceof ContainerEntryModule_1.default) { | ||
this.containerEntryModules.add(module); | ||
return true; | ||
@@ -60,65 +34,19 @@ } | ||
} | ||
clearEntry(chunk, compilation, realContainers) { | ||
hoistModulesInChunk(chunk, compilation) { | ||
const chunkGraph = compilation.chunkGraph; | ||
const { runtimeChunks } = this.sortNameChunks(chunk, compilation); | ||
const removedModules = new Set(); | ||
for (const [name, entry] of compilation.entries) { | ||
if (realContainers.has(name)) | ||
continue; | ||
for (const entryDependency of entry.dependencies) { | ||
if (!entryDependency) | ||
continue; | ||
const resolvedModule = compilation.moduleGraph.getModule(entryDependency); | ||
if (!resolvedModule) | ||
continue; | ||
const context = resolvedModule.context; | ||
if (!context) | ||
continue; | ||
if (!context.endsWith('.federation')) | ||
continue; | ||
if (removedModules.has(chunk)) | ||
continue; | ||
chunkGraph.disconnectChunkAndEntryModule(chunk, resolvedModule); | ||
entry.dependencies.shift(); | ||
removedModules.add(chunk); | ||
const runtimeChunks = this.getRuntimeChunks(chunk, compilation); | ||
for (const module of chunkGraph.getChunkModulesIterable(chunk)) { | ||
for (const runtimeChunk of runtimeChunks) { | ||
chunkGraph.connectChunkAndModule(runtimeChunk, module); | ||
} | ||
} | ||
} | ||
hoistModulesInChunk(chunk, compilation, realContainers) { | ||
const chunkGraph = compilation.chunkGraph; | ||
const { runtimeChunks, namedChunks } = this.sortNameChunks(chunk, compilation); | ||
const currentChunk = chunk; | ||
for (const [name, runtime] of runtimeChunks) { | ||
if (realContainers.has(name)) | ||
continue; | ||
for (const [runtimeName, container] of realContainers) { | ||
const containerModules = chunkGraph.getChunkModulesIterable(container); | ||
for (const cm of containerModules) { | ||
chunkGraph.connectChunkAndModule(runtime, cm); | ||
} | ||
} | ||
} | ||
for (const [name, entrypoint] of namedChunks) { | ||
if (realContainers.has(name)) | ||
continue; | ||
for (const [runtimeName, container] of realContainers) { | ||
const containerModules = chunkGraph.getChunkModulesIterable(container); | ||
for (const cm of containerModules) { | ||
chunkGraph.disconnectChunkAndModule(entrypoint, cm); | ||
} | ||
} | ||
} | ||
} | ||
sortNameChunks(chunk, compilation) { | ||
const runtimeChunks = new Map(); | ||
const namedChunks = new Map(); | ||
getRuntimeChunks(chunk, compilation) { | ||
const runtimeChunks = []; | ||
for (const c of compilation.chunks) { | ||
if (c.hasRuntime() && c.name) { | ||
runtimeChunks.set(c.name, c); | ||
if (c.hasRuntime() && c !== chunk) { | ||
runtimeChunks.push(c); | ||
} | ||
else if (c.name) { | ||
namedChunks.set(c.name, c); | ||
} | ||
} | ||
return { runtimeChunks, namedChunks }; | ||
return runtimeChunks; | ||
} | ||
@@ -125,0 +53,0 @@ } |
@@ -9,6 +9,6 @@ import type { Compiler } from 'webpack'; | ||
static getTemplate(runtimePlugins: string[], bundlerRuntimePath?: string): string; | ||
prependEntry(compiler: Compiler): void; | ||
static getFilePath(containerName: string, runtimePlugins: string[], bundlerRuntimePath?: string): string; | ||
getFilePath(): string; | ||
ensureFile(): void; | ||
prependEntry(compiler: Compiler): void; | ||
injectRuntime(compiler: Compiler): void; | ||
@@ -15,0 +15,0 @@ setRuntimeAlias(compiler: Compiler): void; |
@@ -8,3 +8,2 @@ "use strict"; | ||
const FederationRuntimeModule_1 = __importDefault(require("./FederationRuntimeModule")); | ||
const FederationInitModule_1 = __importDefault(require("./FederationInitModule")); | ||
const utils_1 = require("./utils"); | ||
@@ -14,3 +13,2 @@ const fs_1 = __importDefault(require("fs")); | ||
const constant_1 = require("../constant"); | ||
const HoistContainerReferencesPlugin_1 = __importDefault(require("../HoistContainerReferencesPlugin")); | ||
const { RuntimeGlobals, Template } = require((0, normalize_webpack_path_1.normalizeWebpackPath)('webpack')); | ||
@@ -72,21 +70,2 @@ const { mkdirpSync } = require((0, normalize_webpack_path_1.normalizeWebpackPath)('webpack/lib/util/fs')); | ||
} | ||
prependEntry(compiler) { | ||
this.ensureFile(); | ||
const entryFilePath = this.getFilePath(); | ||
(0, utils_1.modifyEntry)({ | ||
compiler, | ||
prependEntry: (entry) => { | ||
Object.keys(entry).forEach((entryName) => { | ||
const entryItem = entry[entryName]; | ||
if (!entryItem.import) { | ||
// TODO: maybe set this variable as constant is better https://github.com/webpack/webpack/blob/main/lib/config/defaults.js#L176 | ||
entryItem.import = ['./src']; | ||
} | ||
if (!entryItem.import.includes(entryFilePath)) { | ||
entryItem.import.unshift(entryFilePath); | ||
} | ||
}); | ||
}, | ||
}); | ||
} | ||
static getFilePath(containerName, runtimePlugins, bundlerRuntimePath) { | ||
@@ -119,2 +98,21 @@ const hash = (0, utils_1.createHash)(`${containerName} ${FederationRuntimePlugin.getTemplate(runtimePlugins, bundlerRuntimePath)}`); | ||
} | ||
prependEntry(compiler) { | ||
this.ensureFile(); | ||
const entryFilePath = this.getFilePath(); | ||
(0, utils_1.modifyEntry)({ | ||
compiler, | ||
prependEntry: (entry) => { | ||
Object.keys(entry).forEach((entryName) => { | ||
const entryItem = entry[entryName]; | ||
if (!entryItem.import) { | ||
// TODO: maybe set this variable as constant is better https://github.com/webpack/webpack/blob/main/lib/config/defaults.js#L176 | ||
entryItem.import = ['./src']; | ||
} | ||
if (!entryItem.import.includes(entryFilePath)) { | ||
entryItem.import.unshift(entryFilePath); | ||
} | ||
}); | ||
}, | ||
}); | ||
} | ||
injectRuntime(compiler) { | ||
@@ -137,3 +135,2 @@ if (!this.options || !this.options.name) { | ||
compilation.addRuntimeModule(chunk, new FederationRuntimeModule_1.default(runtimeRequirements, name, initOptionsWithoutShared)); | ||
compilation.addRuntimeModule(chunk, new FederationInitModule_1.default(name, this.getFilePath())); | ||
}); | ||
@@ -202,6 +199,4 @@ }); | ||
this.prependEntry(compiler); | ||
this.ensureFile(); | ||
this.injectRuntime(compiler); | ||
this.setRuntimeAlias(compiler); | ||
new HoistContainerReferencesPlugin_1.default().apply(compiler); | ||
} | ||
@@ -208,0 +203,0 @@ } |
@@ -40,2 +40,3 @@ /* | ||
validate(options); | ||
//@ts-ignore | ||
this._provides = (0, options_1.parseOptions)(options.provides, (item) => { | ||
@@ -42,0 +43,0 @@ if (Array.isArray(item)) |
{ | ||
"name": "@module-federation/enhanced", | ||
"version": "0.0.0-next-20240129035206", | ||
"version": "0.0.0-next-20240131231251", | ||
"main": "./dist/src/index.js", | ||
@@ -31,9 +31,8 @@ "types": "./dist/src/index.d.ts", | ||
"devDependencies": { | ||
"terser-webpack-plugin": "^5.3.10", | ||
"@module-federation/webpack-bundler-runtime": "0.0.0-next-20240129035206" | ||
"@module-federation/webpack-bundler-runtime": "0.0.0-next-20240131231251" | ||
}, | ||
"dependencies": { | ||
"@module-federation/sdk": "0.0.0-next-20240129035206", | ||
"@module-federation/runtime-tools": "0.0.0-next-20240129035206" | ||
"@module-federation/sdk": "0.0.0-next-20240131231251", | ||
"@module-federation/runtime-tools": "0.0.0-next-20240131231251" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1
92
968173
193
17083
+ Added@module-federation/runtime@0.0.0-next-20240131231251(transitive)
+ Added@module-federation/runtime-tools@0.0.0-next-20240131231251(transitive)
+ Added@module-federation/sdk@0.0.0-next-20240131231251(transitive)
+ Added@module-federation/webpack-bundler-runtime@0.0.0-next-20240131231251(transitive)
- Removed@module-federation/runtime@0.0.0-next-20240129035206(transitive)
- Removed@module-federation/runtime-tools@0.0.0-next-20240129035206(transitive)
- Removed@module-federation/sdk@0.0.0-next-20240129035206(transitive)
- Removed@module-federation/webpack-bundler-runtime@0.0.0-next-20240129035206(transitive)
Updated@module-federation/runtime-tools@0.0.0-next-20240131231251