Skip to content

Commit 762b9ef

Browse files
2heal1claude
andauthored
fix(rsbuild-plugin): replace direct JSON import with builder define to fix Node.js >= 22 (#4564)
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 6860918 commit 762b9ef

File tree

19 files changed

+63
-41
lines changed

19 files changed

+63
-41
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@module-federation/rsbuild-plugin": patch
3+
---
4+
5+
fix(rsbuild-plugin): replace direct package.json import with builder define injection to avoid Node.js >= 22 compatibility error

.eslintrc.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@
2424
"no-extra-semi": "error"
2525
}
2626
},
27+
{
28+
"files": [
29+
"**/src/**/*.ts",
30+
"**/src/**/*.tsx",
31+
"**/src/**/*.js",
32+
"**/src/**/*.jsx"
33+
],
34+
"excludedFiles": ["**/*.config.*", "**/*.spec.*", "**/*.test.*"],
35+
"rules": {
36+
"no-restricted-imports": [
37+
"error",
38+
{
39+
"patterns": [
40+
{
41+
"group": ["**/*.json"],
42+
"message": "Direct JSON imports may fail in Node.js >= 22. Inject values via builder define (source.define in rslib/rsbuild) instead."
43+
}
44+
]
45+
}
46+
]
47+
}
48+
},
2749
{
2850
"files": ["*.spec.ts", "*.spec.tsx", "*.spec.js", "*.spec.jsx"],
2951
"env": { "jest": true },

packages/esbuild/.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"!**/*",
55
"**/vite.config.*.timestamp*",
66
"**/vitest.config.*.timestamp*",
7-
"rslib.config.ts"
7+
"rslib.config.ts",
8+
"dist/**"
89
],
910
"overrides": [
1011
{

packages/esbuild/src/adapters/lib/commonjs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export function commonjs({
140140
setup({ onLoad, esbuild, initialOptions }) {
141141
let esbuild_shim: typeof import('esbuild') | undefined;
142142
const require_esbuild = () =>
143+
// eslint-disable-next-line @typescript-eslint/no-require-imports
143144
esbuild || (esbuild_shim ||= require('esbuild'));
144145
const read = promises.readFile;
145146
const lexer = new Lexer();

packages/esbuild/src/adapters/lib/manifest.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
import fs from 'fs';
22
import path from 'path';
33
import { resolve } from './collect-exports.js';
4-
import {
5-
BuildOptions,
6-
PluginBuild,
7-
Plugin,
8-
OnResolveArgs,
9-
OnLoadArgs,
10-
BuildResult,
11-
BuildContext,
12-
} from 'esbuild';
13-
//@ts-expect-error
14-
import { version as pluginVersion } from '@module-federation/esbuild/package.json';
15-
4+
import { BuildResult } from 'esbuild';
165
interface OutputFile {
176
entryPoint?: string;
187
imports?: { path: string }[];
@@ -91,6 +80,7 @@ export const writeRemoteManifest = async (config: any, result: BuildResult) => {
9180
try {
9281
const packageJsonPath =
9382
(await resolve(process.cwd(), '/package.json')) || '';
83+
// eslint-disable-next-line @typescript-eslint/no-require-imports
9484
packageJson = require(packageJsonPath);
9585
} catch (e) {
9686
packageJson = { name: config.name };
@@ -261,7 +251,7 @@ export const writeRemoteManifest = async (config: any, result: BuildResult) => {
261251
},
262252
types,
263253
globalName: config.name,
264-
pluginVersion,
254+
pluginVersion: __VERSION__,
265255
publicPath,
266256
},
267257
shared,

packages/esbuild/src/adapters/lib/plugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const cjsToEsmPlugin: Plugin = {
5353
async (args: OnLoadArgs) => {
5454
let esbuild_shim: typeof import('esbuild') | undefined;
5555
const require_esbuild = () =>
56+
// eslint-disable-next-line @typescript-eslint/no-require-imports
5657
build.esbuild || (esbuild_shim ||= require('esbuild'));
5758

5859
const packageBuilder = await require_esbuild().build({

packages/modernjs-v3/src/cli/configPlugin.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ import type {
2020
} from '@modern-js/app-tools';
2121
import type { BundlerChainConfig } from '../interfaces/bundler';
2222

23-
declare global {
24-
namespace NodeJS {
25-
interface ProcessEnv {
26-
IS_ESM_BUILD?: string;
27-
}
28-
}
29-
}
30-
3123
const defaultPath = path.resolve(process.cwd(), 'module-federation.config.ts');
3224

3325
export type ConfigType = Rspack.Configuration;
@@ -56,7 +48,9 @@ const resolvePackageFile = (
5648
return require.resolve(
5749
path.join(
5850
packageRoot,
59-
process.env.IS_ESM_BUILD === 'true' ? esmRelativePath : cjsRelativePath,
51+
process.env['IS_ESM_BUILD'] === 'true'
52+
? esmRelativePath
53+
: cjsRelativePath,
6054
),
6155
);
6256
};
@@ -103,6 +97,7 @@ export const getMFConfig = async (
10397
return config;
10498
}
10599
const mfConfigPath = configPath ? configPath : defaultPath;
100+
// eslint-disable-next-line @typescript-eslint/no-require-imports
106101
const { createJiti } = require('jiti');
107102
const jit = createJiti(__filename, {
108103
interopDefault: true,
@@ -327,7 +322,6 @@ export function patchBundlerConfig(options: {
327322

328323
const splitChunkConfig = chain.optimization.splitChunks.entries();
329324
if (!isServer) {
330-
// @ts-ignore type not the same
331325
autoDeleteSplitChunkCacheGroups(mfConfig, splitChunkConfig);
332326
}
333327

packages/modernjs-v3/src/cli/server/data-fetch-server-plugin.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const dataFetchServePlugin = (): ServerPlugin => ({
99
const { middlewares } = api.getServerContext();
1010
middlewares.push({
1111
name: 'module-federation-serve-manifest',
12-
// @ts-ignore type error
1312
handler: dataFetchMiddleWare,
1413
});
1514
});

packages/modernjs-v3/src/cli/ssrPlugin.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ export const moduleFederationSSRPlugin = (
301301
});
302302
}
303303
});
304-
// @ts-ignore
305304
api.config(() => {
306305
return {
307306
builderPlugins: [mfSSRRsbuildPlugin(pluginOptions)],

packages/modernjs/src/cli/configPlugin.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ import type {
2222
} from '@modern-js/app-tools';
2323
import type { BundlerChainConfig } from '../interfaces/bundler';
2424

25-
declare global {
26-
namespace NodeJS {
27-
interface ProcessEnv {
28-
IS_ESM_BUILD?: string;
29-
}
30-
}
31-
}
32-
3325
const defaultPath = path.resolve(process.cwd(), 'module-federation.config.ts');
3426

3527
export type ConfigType<T> = T extends 'webpack'
@@ -62,7 +54,9 @@ const resolvePackageFile = (
6254
return require.resolve(
6355
path.join(
6456
packageRoot,
65-
process.env.IS_ESM_BUILD === 'true' ? esmRelativePath : cjsRelativePath,
57+
process.env['IS_ESM_BUILD'] === 'true'
58+
? esmRelativePath
59+
: cjsRelativePath,
6660
),
6761
);
6862
};
@@ -109,6 +103,7 @@ export const getMFConfig = async (
109103
return config;
110104
}
111105
const mfConfigPath = configPath ? configPath : defaultPath;
106+
// eslint-disable-next-line @typescript-eslint/no-require-imports
112107
const { createJiti } = require('jiti');
113108
const jit = createJiti(__filename, {
114109
interopDefault: true,
@@ -333,7 +328,7 @@ export function patchBundlerConfig(options: {
333328

334329
const splitChunkConfig = chain.optimization.splitChunks.entries();
335330
if (!isServer) {
336-
// @ts-ignore type not the same
331+
// @ts-expect-error type not the same
337332
autoDeleteSplitChunkCacheGroups(mfConfig, splitChunkConfig);
338333
}
339334

0 commit comments

Comments
 (0)