Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
"type": "node-terminal"
},
{
"command": "pnpm run sync:pullMFTypes",
"name": "Run sync:pullMFTypes",
"command": "pnpm run sync:webpack:types",
"name": "Run sync:webpack:types",
"request": "launch",
"type": "node-terminal"
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"serve:website": "nx run website-new:serve",
"build:website": "NX_TUI=false nx run website-new:build",
"extract-i18n:website": "nx run website:extract-i18n",
"sync:pullMFTypes": "concurrently \"node ./packages/enhanced/pullts.js\"",
"sync:webpack:types": "node ./scripts/sync-webpack-unbundled-types.mjs",
"app:next:dev": "nx run-many --target=serve --configuration=development -p 3000-home,3001-shop,3002-checkout",
"app:next:build": "nx run-many --target=build --parallel=2 --configuration=production -p 3000-home,3001-shop,3002-checkout",
"app:next:prod": "nx run-many --target=serve --configuration=production -p 3000-home,3001-shop,3002-checkout",
Expand Down
5 changes: 4 additions & 1 deletion packages/enhanced/src/lib/container/AsyncBoundaryPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,11 @@ class AsyncEntryStartupPlugin {
source: sources.Source,
) {
const { Template } = compiler.webpack;
const experiments = compiler.options?.experiments as
| { topLevelAwait?: boolean; outputModule?: boolean }
| undefined;
if (
compiler.options?.experiments?.topLevelAwait &&
experiments?.topLevelAwait &&
compiler.options?.experiments?.outputModule
) {
return Template.asString([
Expand Down
9 changes: 5 additions & 4 deletions packages/enhanced/src/lib/container/ModuleFederationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import ContainerPlugin from './ContainerPlugin';
import ContainerReferencePlugin from './ContainerReferencePlugin';
import FederationRuntimePlugin from './runtime/FederationRuntimePlugin';
import { RemoteEntryPlugin } from '@module-federation/rspack/remote-entry-plugin';
import { ExternalsType } from 'webpack/declarations/WebpackOptions';
import StartupChunkDependenciesPlugin from '../startup/MfStartupChunkDependenciesPlugin';
import FederationModulesPlugin from './runtime/FederationModulesPlugin';
import { createSchemaValidation } from '../../utils';
Expand Down Expand Up @@ -165,8 +164,10 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
const remoteType =
options.remoteType ||
(options.library && isValidExternalsType(options.library.type)
? (options.library.type as ExternalsType)
: ('script' as ExternalsType));
? (options.library.type as moduleFederationPlugin.ExternalsType)
: ('script' as moduleFederationPlugin.ExternalsType));
const containerRemoteType =
remoteType as moduleFederationPlugin.ExternalsType;

const useContainerPlugin =
options.exposes &&
Expand Down Expand Up @@ -219,7 +220,7 @@ class ModuleFederationPlugin implements WebpackPluginInstance {
: Object.keys(remotes).length > 0)
) {
new ContainerReferencePlugin({
remoteType,
remoteType: containerRemoteType,
shareScope,
remotes,
}).apply(compiler);
Expand Down
33 changes: 23 additions & 10 deletions packages/enhanced/src/lib/container/RemoteRuntimeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class RemoteRuntimeModule extends RuntimeModule {
];

for (const chunk of allChunks) {
if (chunk.id === null || chunk.id === undefined) {
continue;
}
const modules = chunkGraph?.getChunkModulesIterableBySourceType(
chunk,
'remote',
Expand All @@ -79,11 +82,19 @@ class RemoteRuntimeModule extends RuntimeModule {
? // @ts-ignore
chunkGraph.getModuleId(externalModule)
: undefined;
if (id !== undefined) {
if (id !== undefined && id !== null) {
//@ts-ignore
remotes.push(id);

idToExternalAndNameMapping[id] = [shareScope, name, externalModuleId];
const normalizedExternalModuleId =
externalModuleId === null || externalModuleId === undefined
? undefined
: externalModuleId;
idToExternalAndNameMapping[id] = [
shareScope,
name,
normalizedExternalModuleId,
];
const remoteModules: ExternalModule[] = [];
// FallbackModule has requests
if ('requests' in externalModule && externalModule.requests) {
Expand Down Expand Up @@ -119,14 +130,16 @@ class RemoteRuntimeModule extends RuntimeModule {
externalType: remoteModule.externalType,
name: remoteModule.externalType === 'script' ? remoteName : '',
});
moduleIdToRemoteDataMapping[id] = {
shareScope: shareScope as string,
name,
externalModuleId: externalModuleId as string,
// Preserve the extracted remote name so lazy updates can
// rebuild idToRemoteMap via updateRemoteOptions.
remoteName: remoteName,
};
if (externalModuleId !== null && externalModuleId !== undefined) {
moduleIdToRemoteDataMapping[id] = {
shareScope: shareScope as string,
name,
externalModuleId: externalModuleId as string | number,
// Preserve the extracted remote name so lazy updates can
// rebuild idToRemoteMap via updateRemoteOptions.
remoteName: remoteName,
};
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,12 @@ class CustomRuntimePlugin {
entryChunk,
),
)[0];
this.entryModule =
const entryModuleId =
childCompilation.chunkGraph.getModuleId(entryModule);
this.entryModule =
entryModuleId === null || entryModuleId === undefined
? undefined
: entryModuleId;
}

onceForCompilationMap.set(compilation, source);
Expand Down
12 changes: 12 additions & 0 deletions packages/enhanced/src/lib/sharing/ConsumeSharedRuntimeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class ConsumeSharedRuntimeModule extends RuntimeModule {
const compilation: Compilation = this.compilation!;
const chunkGraph: ChunkGraph = this.chunkGraph!;
const { runtimeTemplate, codeGenerationResults } = compilation;
if (!codeGenerationResults) {
return null;
}
const chunkToModuleMapping: Record<string, any> = {};
const moduleIdToSourceMapping: Map<string | number, string> = new Map();
const initialConsumes: (string | number)[] = [];
Expand All @@ -48,19 +51,28 @@ class ConsumeSharedRuntimeModule extends RuntimeModule {
const module: ConsumeSharedModule = m as unknown as ConsumeSharedModule;
// @ts-ignore
const id = chunkGraph.getModuleId(module);
if (id === null || id === undefined) {
continue;
}
list.push(id);
const moduleGetter = codeGenerationResults.getSource(
// @ts-ignore
module,
chunk.runtime,
'consume-shared',
);
if (!moduleGetter) {
continue;
}
const shareOption = codeGenerationResults.getData(
// @ts-ignore
module,
chunk.runtime,
'consume-shared',
);
if (!shareOption) {
continue;
}
moduleIdToSourceMapping.set(
id,
Template.asString([
Expand Down
3 changes: 3 additions & 0 deletions packages/enhanced/src/lib/sharing/ShareRuntimeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class ShareRuntimeModule extends RuntimeModule {
throw new Error('Compilation is undefined');
}
const { runtimeTemplate, codeGenerationResults } = compilation;
if (!codeGenerationResults) {
return null;
}
const chunkGraph: ChunkGraph | undefined = this.chunkGraph;
if (!chunkGraph) {
throw new Error('ChunkGraph is undefined');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,18 @@ export default class IndependentSharedPlugin {

// 创建独立的 webpack compiler 实例
const webpack = parentCompiler.webpack;
const compiler = webpack.webpack(compilerConfig);
const compiler = webpack.webpack(compilerConfig as any);

// 设置文件系统
compiler.inputFileSystem = parentCompiler.inputFileSystem;
compiler.outputFileSystem = parentCompiler.outputFileSystem;
compiler.intermediateFileSystem = parentCompiler.intermediateFileSystem;
if (parentCompiler.inputFileSystem) {
compiler.inputFileSystem = parentCompiler.inputFileSystem;
}
if (parentCompiler.outputFileSystem) {
compiler.outputFileSystem = parentCompiler.outputFileSystem;
}
if (parentCompiler.intermediateFileSystem) {
compiler.intermediateFileSystem = parentCompiler.intermediateFileSystem;
}

const { currentShare, shareRequestsMap } = extraOptions || {};

Expand Down
2 changes: 1 addition & 1 deletion packages/enhanced/src/lib/sharing/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ function getDescriptionFile(
);
}
const filePath = join(fs, directory, descriptionFiles[i]);
readJson(fs, filePath, (err, data: object) => {
readJson(fs, filePath, (err, data?: Record<string, any>) => {
if (err) {
if ('code' in err && err.code === 'ENOENT') {
i++;
Expand Down
2 changes: 1 addition & 1 deletion packages/manifest/src/ModuleHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
composeKeyWithSeparator,
moduleFederationPlugin,
} from '@module-federation/sdk';
import type { StatsModule } from 'webpack';
import type { StatsModule } from 'webpack/lib/stats/DefaultStatsFactoryPlugin';
import path from 'path';
import {
ContainerManager,
Expand Down
6 changes: 5 additions & 1 deletion packages/manifest/src/StatsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import {
StatsMetaDataWithPublicPath,
StatsShared,
} from '@module-federation/sdk';
import { Compilation, Compiler, StatsCompilation, StatsModule } from 'webpack';
import { Compilation, Compiler } from 'webpack';
import type {
StatsCompilation,
StatsModule,
} from 'webpack/lib/stats/DefaultStatsFactoryPlugin';
import {
isDev,
getAssetsByChunk,
Expand Down
6 changes: 5 additions & 1 deletion packages/manifest/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Chunk, Compilation, StatsCompilation, StatsModule } from 'webpack';
import { Chunk, Compilation } from 'webpack';
import type {
StatsCompilation,
StatsModule,
} from 'webpack/lib/stats/DefaultStatsFactoryPlugin';
import path from 'path';
import fs from 'fs';
import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type {
WebpackOptionsNormalized,
Compiler,
ExternalItemFunctionData,
} from 'webpack';
import type { WebpackOptionsNormalized, Compiler } from 'webpack';
import type { moduleFederationPlugin } from '@module-federation/sdk';
import path from 'path';
import InvertedContainerPlugin from '../container/InvertedContainerPlugin';
Expand Down Expand Up @@ -114,12 +110,12 @@ export function handleServerExternals(

if (functionIndex !== -1) {
const originalExternals = compiler.options.externals[functionIndex] as (
data: ExternalItemFunctionData,
data: any,
callback: any,
) => undefined | string;
) => undefined | string | Promise<undefined | string>;

compiler.options.externals[functionIndex] = async function (
ctx: ExternalItemFunctionData,
ctx: any,
callback: any,
) {
const fromNext = await originalExternals(ctx, callback);
Expand Down Expand Up @@ -156,7 +152,7 @@ export function handleServerExternals(
return fromNext;
}
return;
};
} as any;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class InvertedContainerRuntimeModule extends RuntimeModule {
}

const runtimeChunk = compilation.options.optimization?.runtimeChunk;
if (runtimeChunk === 'single' || typeof runtimeChunk === 'object') {
if (typeof runtimeChunk === 'object' && runtimeChunk !== null) {
const logger = compilation.getLogger('InvertedContainerRuntimeModule');
logger.info(
'Runtime chunk is set to single. Consider adding runtime: false to your ModuleFederationPlugin configuration to prevent runtime conflicts.',
'Runtime chunk is configured. Consider adding runtime: false to your ModuleFederationPlugin configuration to prevent runtime conflicts.',
);
}

Expand Down
23 changes: 13 additions & 10 deletions packages/node/src/plugins/RemotePublicPathRuntimeModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@ class AutoPublicPathRuntimeModule extends RuntimeModule {
hash: compilation?.hash || 'XXXX',
});

const chunkName = compilation?.getPath(
javascript.JavascriptModulesPlugin.getChunkFilenameTemplate(
this.chunk,
compilation?.outputOptions,
),
{
chunk: this.chunk,
contentHashType: 'javascript',
},
);
const currentChunk = this.chunk;
const chunkName =
currentChunk &&
compilation?.getPath(
javascript.JavascriptModulesPlugin.getChunkFilenameTemplate(
currentChunk,
compilation?.outputOptions,
),
{
chunk: currentChunk,
contentHashType: 'javascript',
},
);

let undoPath: string | null = null;
if (chunkName && path) {
Expand Down
Loading
Loading