Skip to content

Commit 5f67582

Browse files
authored
chore(modern-js-plugin): add disableSSR option (#3355)
1 parent 336f3d8 commit 5f67582

File tree

7 files changed

+101
-17
lines changed

7 files changed

+101
-17
lines changed

.changeset/sour-carpets-walk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/modern-js': patch
3+
---
4+
5+
chore(modern-js-plugin): add ssr option

apps/website-new/docs/en/guide/framework/modernjs.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,39 @@ Set module loading status.
169169
A fault-tolerant component that is rendered when the component fails to **load** or **render**.
170170

171171
Note: This component only renders this fault-tolerant component on the client side when **rendering** fails.
172+
173+
## Configuration
174+
175+
### ssr
176+
177+
- Type: `false`
178+
- Is it required: No
179+
- Default value: `undefined`
180+
181+
`@module-federation/modern-js` will automatically add SSR related build presets based on `server.ssr` in modern.js config.
182+
183+
If the current project only needs to load MF in the CSR, you can set `ssr: false` to help progressive migration.
184+
185+
```title='modern.config.ts'
186+
import { appTools, defineConfig } from '@modern-js/app-tools';
187+
import { moduleFederationPlugin } from '@module-federation/modern-js';
188+
189+
// https://modernjs.dev/en/configure/app/usage
190+
export default defineConfig({
191+
dev: {
192+
port: 3050,
193+
},
194+
runtime: {
195+
router: true,
196+
},
197+
server: {
198+
ssr: {
199+
mode: 'stream',
200+
},
201+
},
202+
plugins: [
203+
appTools(),
204+
moduleFederationPlugin({ ssr: false })
205+
],
206+
});
207+
```

apps/website-new/docs/zh/guide/framework/modernjs.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,39 @@ export default Product;
180180
当组件**加载****渲染**失败时,所渲染的容错组件。
181181

182182
注意:当**渲染**失败的时候,该组件仅在客户端渲染此 容错组件。
183+
184+
## 配置
185+
186+
### ssr
187+
188+
- 类型:`false`
189+
- 是否必填:否
190+
- 默认值:`undefined`
191+
192+
`@module-federation/modern-js` 会根据 modern.js config 中的 `server.ssr` 来自动添加 SSR 相关的构建预设。
193+
194+
如果当前项目仅需要在 CSR 加载 MF ,那么可以设置 `ssr: false` 来帮助渐进式迁移。
195+
196+
```title='modern.config.ts'
197+
import { appTools, defineConfig } from '@modern-js/app-tools';
198+
import { moduleFederationPlugin } from '@module-federation/modern-js';
199+
200+
// https://modernjs.dev/en/configure/app/usage
201+
export default defineConfig({
202+
dev: {
203+
port: 3050,
204+
},
205+
runtime: {
206+
router: true,
207+
},
208+
server: {
209+
ssr: {
210+
mode: 'stream',
211+
},
212+
},
213+
plugins: [
214+
appTools(),
215+
moduleFederationPlugin({ ssr: false })
216+
],
217+
});
218+
```

packages/modernjs/src/cli/configPlugin.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ export const moduleFederationConfigPlugin = (
7272
const bundlerType =
7373
useAppContext().bundlerType === 'rspack' ? 'rspack' : 'webpack';
7474
const ipv4 = getIPV4();
75-
const enableSSR = Boolean(modernjsConfig?.server?.ssr);
75+
const enableSSR =
76+
userConfig.userConfig?.ssr === false
77+
? false
78+
: Boolean(modernjsConfig?.server?.ssr);
7679

7780
if (userConfig.remoteIpStrategy === undefined) {
7881
if (!enableSSR) {

packages/modernjs/src/cli/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const moduleFederationPlugin = (
2121
distOutputDir: '',
2222
originPluginOptions: userConfig,
2323
remoteIpStrategy: userConfig?.remoteIpStrategy,
24+
userConfig,
2425
};
2526
return {
2627
name: '@modern-js/plugin-module-federation',

packages/modernjs/src/cli/ssrPlugin.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function setEnv() {
1515
}
1616

1717
export const moduleFederationSSRPlugin = (
18-
userConfig: Required<InternalModernPluginOptions>,
18+
pluginOptions: Required<InternalModernPluginOptions>,
1919
): CliPlugin<AppTools> => ({
2020
name: '@modern-js/plugin-module-federation-ssr',
2121
pre: [
@@ -25,7 +25,7 @@ export const moduleFederationSSRPlugin = (
2525
setup: async ({ useConfigContext, useAppContext }) => {
2626
const modernjsConfig = useConfigContext();
2727
const enableSSR = Boolean(modernjsConfig?.server?.ssr);
28-
if (!enableSSR) {
28+
if (!enableSSR || pluginOptions.userConfig?.ssr === false) {
2929
return {};
3030
}
3131

@@ -50,32 +50,32 @@ export const moduleFederationSSRPlugin = (
5050
// throw new Error(
5151
// `${PLUGIN_IDENTIFIER} Not support rspack ssr mode yet !`,
5252
// );
53-
if (!userConfig.nodePlugin) {
54-
userConfig.nodePlugin = new RspackModuleFederationPlugin(
55-
userConfig.ssrConfig,
53+
if (!pluginOptions.nodePlugin) {
54+
pluginOptions.nodePlugin = new RspackModuleFederationPlugin(
55+
pluginOptions.ssrConfig,
5656
);
5757
// @ts-ignore
58-
config.plugins?.push(userConfig.nodePlugin);
58+
config.plugins?.push(pluginOptions.nodePlugin);
5959
}
6060
} else {
61-
userConfig.distOutputDir =
62-
userConfig.distOutputDir ||
61+
pluginOptions.distOutputDir =
62+
pluginOptions.distOutputDir ||
6363
config.output?.path ||
6464
path.resolve(process.cwd(), 'dist');
6565
}
6666
},
6767
webpack(config, { isServer }) {
6868
if (isServer) {
69-
if (!userConfig.nodePlugin) {
70-
userConfig.nodePlugin = new ModuleFederationPlugin(
71-
userConfig.ssrConfig,
69+
if (!pluginOptions.nodePlugin) {
70+
pluginOptions.nodePlugin = new ModuleFederationPlugin(
71+
pluginOptions.ssrConfig,
7272
);
7373
// @ts-ignore
74-
config.plugins?.push(userConfig.nodePlugin);
74+
config.plugins?.push(pluginOptions.nodePlugin);
7575
}
7676
} else {
77-
userConfig.distOutputDir =
78-
userConfig.distOutputDir ||
77+
pluginOptions.distOutputDir =
78+
pluginOptions.distOutputDir ||
7979
config.output?.path ||
8080
path.resolve(process.cwd(), 'dist');
8181
}
@@ -133,11 +133,11 @@ export const moduleFederationSSRPlugin = (
133133
};
134134
},
135135
afterBuild: () => {
136-
const { nodePlugin, browserPlugin, distOutputDir } = userConfig;
136+
const { nodePlugin, browserPlugin, distOutputDir } = pluginOptions;
137137
updateStatsAndManifest(nodePlugin, browserPlugin, distOutputDir);
138138
},
139139
afterDev: () => {
140-
const { nodePlugin, browserPlugin, distOutputDir } = userConfig;
140+
const { nodePlugin, browserPlugin, distOutputDir } = pluginOptions;
141141
updateStatsAndManifest(nodePlugin, browserPlugin, distOutputDir);
142142
},
143143
};

packages/modernjs/src/types/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ModuleFederationPlugin as RspackModuleFederationPlugin } from '@mo
55
export interface PluginOptions {
66
config?: moduleFederationPlugin.ModuleFederationPluginOptions;
77
configPath?: string;
8+
ssr?: false;
89
remoteIpStrategy?: 'ipv4' | 'inherit';
910
}
1011

@@ -16,7 +17,9 @@ export interface InternalModernPluginOptions {
1617
browserPlugin?: BundlerPlugin;
1718
nodePlugin?: BundlerPlugin;
1819
remoteIpStrategy?: 'ipv4' | 'inherit';
20+
userConfig?: PluginOptions;
1921
}
22+
2023
export type BundlerPlugin =
2124
| WebpackModuleFederationPlugin
2225
| RspackModuleFederationPlugin;

0 commit comments

Comments
 (0)