Skip to content

Commit e56b723

Browse files
authored
fix: support rspack ssr live reload (#2801)
1 parent 57a612c commit e56b723

File tree

10 files changed

+73
-32
lines changed

10 files changed

+73
-32
lines changed

apps/modernjs-ssr/dynamic-nested-remote/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
"*": ["./@mf-types/*"]
1111
}
1212
},
13-
"include": ["src", "shared", "config", "modern.config.ts", "./@mf-types"],
13+
"include": ["src", "shared", "config", "modern.config.ts", "@mf-types"],
1414
"exclude": ["**/node_modules"]
1515
}

packages/modernjs/src/cli/configPlugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ export function modifyBundlerConfig<T extends Bundler>(options: {
3636
isServer,
3737
modernjsConfig,
3838
remoteIpStrategy = 'ipv4',
39+
bundlerType,
3940
} = options;
4041

4142
patchMFConfig(mfConfig, isServer, remoteIpStrategy);
4243

4344
patchBundlerConfig({
45+
bundlerType,
4446
bundlerConfig: config,
4547
isServer,
4648
modernjsConfig,

packages/modernjs/src/cli/ssrPlugin.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CliPlugin, AppTools } from '@modern-js/app-tools';
44
import type { InternalModernPluginOptions } from '../types';
55
import { ModuleFederationPlugin } from '@module-federation/enhanced';
66
import { ModuleFederationPlugin as RspackModuleFederationPlugin } from '@module-federation/enhanced/rspack';
7-
import { EntryChunkTrackerPlugin } from '@module-federation/node';
7+
import { UniverseEntryChunkTrackerPlugin } from '@module-federation/node';
88
import { updateStatsAndManifest } from './manifest';
99
import { MODERN_JS_SERVER_DIR, PLUGIN_IDENTIFIER } from '../constant';
1010
import { isDev } from './constant';
@@ -82,12 +82,6 @@ export const moduleFederationSSRPlugin = (
8282
// @ts-ignore
8383
config.plugins?.push(userConfig.nodePlugin);
8484
}
85-
// config.plugins?.push(
86-
// new StreamingTargetPlugin(userConfig.nodePlugin),
87-
// );
88-
if (isDev) {
89-
config.plugins?.push(new EntryChunkTrackerPlugin());
90-
}
9185
} else {
9286
userConfig.distOutputDir =
9387
userConfig.distOutputDir ||
@@ -137,6 +131,11 @@ export const moduleFederationSSRPlugin = (
137131
bundlerChain(chain, { isServer }) {
138132
if (isServer) {
139133
chain.target('async-node');
134+
if (isDev) {
135+
chain
136+
.plugin('UniverseEntryChunkTrackerPlugin')
137+
.use(UniverseEntryChunkTrackerPlugin);
138+
}
140139
}
141140
if (isDev && !isServer) {
142141
chain.externals({

packages/modernjs/src/cli/utils.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ describe('patchBundlerConfig', async () => {
9898
},
9999
};
100100
patchBundlerConfig<'webpack'>({
101+
bundlerType: 'webpack',
101102
bundlerConfig,
102103
isServer: true,
103104
modernjsConfig: {
@@ -117,7 +118,7 @@ describe('patchBundlerConfig', async () => {
117118
uniqueName: 'host',
118119
},
119120
watchOptions: {
120-
ignored: ['@mf-types'],
121+
ignored: ['**/@mf-types/**'],
121122
},
122123
};
123124
// @ts-ignore temp ignore
@@ -134,6 +135,7 @@ describe('patchBundlerConfig', async () => {
134135
},
135136
};
136137
patchBundlerConfig<'webpack'>({
138+
bundlerType: 'webpack',
137139
bundlerConfig,
138140
isServer: false,
139141
modernjsConfig: {
@@ -153,7 +155,7 @@ describe('patchBundlerConfig', async () => {
153155
uniqueName: 'host',
154156
},
155157
watchOptions: {
156-
ignored: ['@mf-types'],
158+
ignored: ['**/@mf-types/**'],
157159
},
158160
};
159161
// @ts-ignore temp ignore

packages/modernjs/src/cli/utils.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -225,39 +225,44 @@ export function patchBundlerConfig<T extends Bundler>(options: {
225225
bundlerConfig: BundlerConfig<T>;
226226
isServer: boolean;
227227
modernjsConfig: UserConfig<AppTools>;
228+
bundlerType: Bundler;
228229
mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions;
229230
}) {
230-
const { bundlerConfig, modernjsConfig, isServer, mfConfig } = options;
231+
const { bundlerConfig, modernjsConfig, isServer, mfConfig, bundlerType } =
232+
options;
231233
const enableSSR = Boolean(modernjsConfig.server?.ssr);
232234

233235
delete bundlerConfig.optimization?.runtimeChunk;
234236

235237
patchIgnoreWarning(bundlerConfig);
236238

237-
bundlerConfig.watchOptions = bundlerConfig.watchOptions || {};
238-
if (!Array.isArray(bundlerConfig.watchOptions.ignored)) {
239-
if (bundlerConfig.watchOptions.ignored) {
240-
bundlerConfig.watchOptions.ignored = [
241-
bundlerConfig.watchOptions.ignored as string,
242-
];
243-
} else {
244-
bundlerConfig.watchOptions.ignored = [];
239+
if (bundlerType === 'webpack') {
240+
bundlerConfig.watchOptions = bundlerConfig.watchOptions || {};
241+
if (!Array.isArray(bundlerConfig.watchOptions.ignored)) {
242+
if (bundlerConfig.watchOptions.ignored) {
243+
bundlerConfig.watchOptions.ignored = [
244+
bundlerConfig.watchOptions.ignored as string,
245+
];
246+
} else {
247+
bundlerConfig.watchOptions.ignored = [];
248+
}
245249
}
246-
}
247-
if (mfConfig.dts !== false) {
248-
if (
249-
typeof mfConfig.dts === 'object' &&
250-
typeof mfConfig.dts.consumeTypes === 'object' &&
251-
mfConfig.dts.consumeTypes.remoteTypesFolder
252-
) {
253-
bundlerConfig.watchOptions.ignored.push(
254-
mfConfig.dts.consumeTypes.remoteTypesFolder,
255-
);
250+
if (mfConfig.dts !== false) {
251+
if (
252+
typeof mfConfig.dts === 'object' &&
253+
typeof mfConfig.dts.consumeTypes === 'object' &&
254+
mfConfig.dts.consumeTypes.remoteTypesFolder
255+
) {
256+
bundlerConfig.watchOptions.ignored.push(
257+
`**/${mfConfig.dts.consumeTypes.remoteTypesFolder}/**`,
258+
);
259+
} else {
260+
bundlerConfig.watchOptions.ignored.push('**/@mf-types/**');
261+
}
256262
} else {
257-
bundlerConfig.watchOptions.ignored.push('@mf-types');
263+
bundlerConfig.watchOptions.ignored.push('**/@mf-types/**');
258264
}
259265
}
260-
261266
if (bundlerConfig.output) {
262267
if (!bundlerConfig.output?.chunkLoadingGlobal) {
263268
bundlerConfig.output.chunkLoadingGlobal = `chunk_${mfConfig.name}`;

packages/node/global.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ declare module 'webpack/lib/RuntimeModule';
1111
declare module 'webpack/lib/Template';
1212
declare module 'webpack/lib/util/compileBooleanMatcher';
1313
declare module 'webpack/lib/util/identifier';
14-
14+
declare module 'btoa';
1515
declare global {
1616
namespace NodeJS {
1717
interface Global {

packages/node/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"author": "Zack Jackson <[email protected]>",
5858
"license": "MIT",
5959
"dependencies": {
60+
"btoa": "1.2.1",
6061
"encoding": "^0.1.13",
6162
"node-fetch": "2.7.0",
6263
"@module-federation/enhanced": "workspace:*",

packages/node/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export { default as UniversalFederationPlugin } from './plugins/UniversalFederat
44
export { default as ChunkCorrelationPlugin } from './plugins/ChunkCorrelationPlugin';
55
export { default as RemotePublicPathPlugin } from './plugins/RemotePublicPathRuntimeModule';
66
export { default as EntryChunkTrackerPlugin } from './plugins/EntryChunkTrackerPlugin';
7+
export { default as UniverseEntryChunkTrackerPlugin } from './plugins/UniverseEntryChunkTrackerPlugin';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import pBtoa from 'btoa';
2+
import type { WebpackPluginInstance, Compiler } from 'webpack';
3+
4+
class UniverseEntryChunkTrackerPlugin implements WebpackPluginInstance {
5+
apply(compiler: Compiler) {
6+
const code = `
7+
if(typeof module !== 'undefined') {
8+
globalThis.entryChunkCache = globalThis.entryChunkCache || new Set();
9+
module.filename && globalThis.entryChunkCache.add(module.filename);
10+
if(module.children) {
11+
module.children.forEach(function(c) {
12+
c.filename && globalThis.entryChunkCache.add(c.filename);
13+
})
14+
}
15+
}
16+
`;
17+
const base64Code = pBtoa(code);
18+
const dataUrl = `data:text/javascript;base64,${base64Code}`;
19+
20+
compiler.hooks.afterPlugins.tap('UniverseEntryChunkTrackerPlugin', () => {
21+
new compiler.webpack.EntryPlugin(compiler.context, dataUrl, {}).apply(
22+
compiler,
23+
);
24+
});
25+
}
26+
}
27+
28+
export default UniverseEntryChunkTrackerPlugin;

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)