Skip to content

Commit

Permalink
chore(modern-js-plugin): use bundlerChain (#3526)
Browse files Browse the repository at this point in the history
  • Loading branch information
2heal1 authored Mar 3, 2025
1 parent 3c8bd83 commit 1010f96
Show file tree
Hide file tree
Showing 24 changed files with 2,127 additions and 1,549 deletions.
7 changes: 7 additions & 0 deletions .changeset/poor-mails-shave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@module-federation/rsbuild-plugin': patch
'@module-federation/modern-js': patch
'@module-federation/modernjsapp': patch
---

chore(modern-js-plugin): use bundlerChain instead of tools.webpack or tools.rspack
14 changes: 7 additions & 7 deletions .github/workflows/e2e-modern-ssr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ jobs:
lsof -ti tcp:3050,3051,3052,3053,3054,3055,3056 | xargs -r kill &&
pnpm run app:modern:dev &
sleep 30 &&
npx wait-on http://127.0.0.1:3050/ &&
npx wait-on http://127.0.0.1:3051/ &&
npx wait-on http://127.0.0.1:3052/ &&
npx wait-on http://127.0.0.1:3053/ &&
npx wait-on http://127.0.0.1:3054/ &&
npx wait-on http://127.0.0.1:3055/ &&
npx wait-on http://127.0.0.1:3056/ &&
npx wait-on http://127.0.0.1:3050/mf-manifest.json &&
npx wait-on http://127.0.0.1:3051/mf-manifest.json &&
npx wait-on http://127.0.0.1:3052/mf-manifest.json &&
npx wait-on http://127.0.0.1:3053/mf-manifest.json &&
npx wait-on http://127.0.0.1:3054/mf-manifest.json &&
npx wait-on http://127.0.0.1:3055/mf-manifest.json &&
npx wait-on http://127.0.0.1:3056/mf-manifest.json &&
npx nx run modernjs-ssr-host:e2e &&
lsof -ti tcp:3050,3051,3052,3053,3054,3055,3056 | xargs kill
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/dynamic-nested-remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.64.0",
"@modern-js/runtime": "2.65.1",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.64.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.64.0",
"@modern-js/tsconfig": "2.65.1",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/dynamic-remote-new-version/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.64.0",
"@modern-js/runtime": "2.65.1",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.64.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.64.0",
"@modern-js/tsconfig": "2.65.1",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/dynamic-remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.64.0",
"@modern-js/runtime": "2.65.1",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.64.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.64.0",
"@modern-js/tsconfig": "2.65.1",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/host/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.64.0",
"@modern-js/runtime": "2.65.1",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.64.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.64.0",
"@modern-js/tsconfig": "2.65.1",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/modernjs-ssr/nested-remote/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default defineConfig({
},
plugins: [
appTools({
bundler: 'experimental-rspack',
bundler: 'rspack',
}),
moduleFederationPlugin(),
],
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/nested-remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.64.0",
"@modern-js/runtime": "2.65.1",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.64.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.64.0",
"@modern-js/tsconfig": "2.65.1",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/remote-new-version/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.64.0",
"@modern-js/runtime": "2.65.1",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.64.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.64.0",
"@modern-js/tsconfig": "2.65.1",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs-ssr/remote/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.64.0",
"@modern-js/runtime": "2.65.1",
"@module-federation/modern-js": "workspace:*",
"antd": "4.24.15",
"react": "~18.3.1",
"react-dom": "~18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.64.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.64.0",
"@modern-js/tsconfig": "2.65.1",
"@types/jest": "~29.5.0",
"@types/node": "~16.11.7",
"@types/react": "~18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions apps/modernjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
],
"dependencies": {
"@babel/runtime": "7.26.0",
"@modern-js/runtime": "2.64.0",
"@modern-js/runtime": "2.65.1",
"@module-federation/enhanced": "workspace:*",
"react": "18.3.1",
"react-dom": "18.3.1"
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.59.0",
"@modern-js/app-tools": "2.64.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/tsconfig": "2.64.0",
"@modern-js/tsconfig": "2.65.1",
"@types/jest": "~29.5.0",
"@types/node": "~20.12.12",
"@types/react": "~18.2.0",
Expand Down
10 changes: 5 additions & 5 deletions packages/chrome-devtools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
},
"dependencies": {
"@arco-design/web-react": "^2.64.1",
"@modern-js/runtime": "2.64.0",
"@modern-js/runtime": "2.65.1",
"@module-federation/sdk": "workspace:*",
"ahooks": "^3.7.10",
"dagre": "^0.8.5",
Expand All @@ -67,12 +67,12 @@
},
"devDependencies": {
"@modern-js-app/eslint-config": "2.54.6",
"@modern-js/app-tools": "2.64.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/builder-webpack-provider": "2.46.1",
"@modern-js/eslint-config": "2.59.0",
"@modern-js/module-tools": "2.64.0",
"@modern-js/storybook": "2.64.0",
"@modern-js/tsconfig": "2.64.0",
"@modern-js/module-tools": "2.65.1",
"@modern-js/storybook": "2.65.1",
"@modern-js/tsconfig": "2.65.1",
"@module-federation/runtime": "workspace:*",
"@playwright/test": "1.49.1",
"@storybook/addon-essentials": "^8",
Expand Down
1 change: 1 addition & 0 deletions packages/manifest/src/StatsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class StatsPlugin implements WebpackPluginInstance {
return;
}
const res = this._statsManager.validate(compiler);

if (!res) {
return;
}
Expand Down
14 changes: 7 additions & 7 deletions packages/modernjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@
"author": "hanric <[email protected]>",
"license": "MIT",
"dependencies": {
"@modern-js/node-bundle-require": "2.64.0",
"@modern-js/node-bundle-require": "2.65.1",
"@module-federation/rsbuild-plugin": "workspace:*",
"@modern-js/utils": "2.64.0",
"@modern-js/utils": "2.65.1",
"@module-federation/enhanced": "workspace:*",
"@module-federation/node": "workspace:*",
"@module-federation/sdk": "workspace:*",
Expand All @@ -98,11 +98,11 @@
"react-error-boundary": "4.1.2"
},
"devDependencies": {
"@modern-js/app-tools": "2.64.0",
"@modern-js/core": "2.64.0",
"@modern-js/module-tools": "2.64.0",
"@modern-js/runtime": "2.64.0",
"@modern-js/tsconfig": "2.64.0",
"@modern-js/app-tools": "2.65.1",
"@modern-js/core": "2.65.1",
"@modern-js/module-tools": "2.65.1",
"@modern-js/runtime": "2.65.1",
"@modern-js/tsconfig": "2.65.1",
"@module-federation/manifest": "workspace:*"
},
"peerDependencies": {
Expand Down
94 changes: 26 additions & 68 deletions packages/modernjs/src/cli/configPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import path from 'path';
import type {
CliPluginFuture,
AppTools,
UserConfig,
Bundler,
} from '@modern-js/app-tools';
import type { BundlerConfig } from '../interfaces/bundler';
import type { CliPluginFuture, AppTools } from '@modern-js/app-tools';
import type { InternalModernPluginOptions } from '../types';
import {
patchBundlerConfig,
Expand All @@ -14,7 +8,6 @@ import {
patchMFConfig,
addMyTypes2Ignored,
} from './utils';
import { moduleFederationPlugin } from '@module-federation/sdk';

export function setEnv(enableSSR: boolean) {
if (enableSSR) {
Expand All @@ -23,34 +16,6 @@ export function setEnv(enableSSR: boolean) {
}
}

export function modifyBundlerConfig<T extends Bundler>(options: {
bundlerType: Bundler;
mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions;
config: BundlerConfig<T>;
isServer: boolean;
modernjsConfig: UserConfig<AppTools>;
remoteIpStrategy?: 'ipv4' | 'inherit';
}) {
const {
mfConfig,
config,
isServer,
modernjsConfig,
remoteIpStrategy = 'ipv4',
bundlerType,
} = options;

patchMFConfig(mfConfig, isServer, remoteIpStrategy);

patchBundlerConfig({
bundlerType,
bundlerConfig: config,
isServer,
modernjsConfig,
mfConfig,
});
}

export const moduleFederationConfigPlugin = (
userConfig: InternalModernPluginOptions,
): CliPluginFuture<AppTools> => ({
Expand All @@ -66,15 +31,36 @@ export const moduleFederationConfigPlugin = (
userConfig.ssrConfig || JSON.parse(JSON.stringify(mfConfig));
userConfig.ssrConfig = ssrConfig;
userConfig.csrConfig = csrConfig;
const enableSSR =
userConfig.userConfig?.ssr ?? Boolean(modernjsConfig?.server?.ssr);

api.modifyBundlerChain((chain, { isServer }) => {
// @ts-expect-error chain type is not correct
addMyTypes2Ignored(chain, isServer ? ssrConfig : csrConfig);

const targetMFConfig = isServer ? ssrConfig : csrConfig;
patchMFConfig(
targetMFConfig,
isServer,
userConfig.remoteIpStrategy || 'ipv4',
);

patchBundlerConfig({
// @ts-expect-error chain type is not correct
chain,
isServer,
modernjsConfig,
mfConfig,
enableSSR,
});

userConfig.distOutputDir =
chain.output.get('path') || path.resolve(process.cwd(), 'dist');
});
api.config(() => {
const bundlerType =
api.getAppContext().bundlerType === 'rspack' ? 'rspack' : 'webpack';
const ipv4 = getIPV4();
const enableSSR =
userConfig.userConfig?.ssr === false
? false
: Boolean(modernjsConfig?.server?.ssr);

if (userConfig.remoteIpStrategy === undefined) {
if (!enableSSR) {
Expand All @@ -86,34 +72,6 @@ export const moduleFederationConfigPlugin = (

return {
tools: {
bundlerChain(chain, { isServer }) {
addMyTypes2Ignored(chain, isServer ? ssrConfig : csrConfig);
},
rspack(config, { isServer }) {
modifyBundlerConfig({
bundlerType,
mfConfig: isServer ? ssrConfig : csrConfig,
config,
isServer,
modernjsConfig,
remoteIpStrategy: userConfig.remoteIpStrategy,
});
userConfig.distOutputDir =
config.output?.path || path.resolve(process.cwd(), 'dist');
},
webpack(config, { isServer }) {
modifyBundlerConfig({
bundlerType,
mfConfig: isServer ? ssrConfig : csrConfig,
config,
isServer,
modernjsConfig,
remoteIpStrategy: userConfig.remoteIpStrategy,
});

userConfig.distOutputDir =
config.output?.path || path.resolve(process.cwd(), 'dist');
},
devServer: {
headers: {
'Access-Control-Allow-Origin': '*',
Expand Down
Loading

0 comments on commit 1010f96

Please sign in to comment.