Skip to content

fix(rsbuild-plugin): replace direct JSON import with builder define to fix Node.js >= 22#4564

Merged
2heal1 merged 2 commits intomainfrom
fix/import-attr
Mar 18, 2026
Merged

fix(rsbuild-plugin): replace direct JSON import with builder define to fix Node.js >= 22#4564
2heal1 merged 2 commits intomainfrom
fix/import-attr

Conversation

@2heal1
Copy link
Member

@2heal1 2heal1 commented Mar 18, 2026

Description

Summary

Importing JSON files directly (e.g. import pkg from '../../package.json') throws an error in Node.js >= 22 without an explicit with { type: 'json' }
assertion. This PR eliminates the import entirely by injecting the package name as a compile-time string literal via source.define in rslib.config.ts, so no
JSON import exists in the output at all.

Additionally, an ESLint rule is added to prevent the same issue from being reintroduced across any src/ file in the monorepo, and a pre-existing bug where
runtime-tools lint was scanning dist/ artifacts is also fixed.

Changes

  • packages/rsbuild-plugin/src/cli/index.ts — remove import pkgJson from '../../package.json', replace pkgJson.name usages with
    RSBUILD_PLUGIN_PACKAGE_NAME
  • packages/rsbuild-plugin/rslib.config.ts — read package.json via fs.readFileSync at config-eval time and inject the name through source.define; rslib
    inlines it as a string literal at build time
  • packages/rsbuild-plugin/src/env.d.ts (new) — ambient declaration for RSBUILD_PLUGIN_PACKAGE_NAME so TypeScript is satisfied
  • .eslintrc.json — add no-restricted-imports rule banning **/*.json imports inside any /src/ file, with an exemption for config/spec/test files
  • packages/runtime-tools/.eslintrc.json — append dist/** to ignorePatterns to restore the root .eslintignore exclusion that was being overridden by "!**/*"

Related Issue

Types of changes

  • Docs change / refactoring / dependency upgrade
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • I have updated the documentation.

…o fix Node.js >= 22

- Remove `import pkgJson from '../../package.json'` which throws on Node.js >= 22
  without `assert { type: 'json' }` / `with { type: 'json' }`
- Inject package name via `source.define` in rslib.config.ts so the value is
  inlined as a string literal at build time
- Add `src/env.d.ts` to declare the `__RSBUILD_PLUGIN_PACKAGE_NAME__` global
- Add `no-restricted-imports` ESLint rule in root config to ban `*.json` imports
  inside `**/src/**` files going forward
- Fix `packages/runtime-tools/.eslintrc.json` ignorePatterns overriding root
  `.eslintignore` causing `dist/` to be linted

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@netlify
Copy link

netlify bot commented Mar 18, 2026

Deploy Preview for module-federation-docs ready!

Name Link
🔨 Latest commit 1334efa
🔍 Latest deploy log https://app.netlify.com/projects/module-federation-docs/deploys/69ba5635aa2d0c0008f0ccab
😎 Deploy Preview https://deploy-preview-4564--module-federation-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@changeset-bot
Copy link

changeset-bot bot commented Mar 18, 2026

🦋 Changeset detected

Latest commit: 1334efa

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 45 packages
Name Type
@module-federation/rsbuild-plugin Patch
@module-federation/modern-js-v3 Patch
@module-federation/modern-js Patch
@module-federation/rspress-plugin Patch
remote5 Patch
remote6 Patch
shared-tree-shaking-with-server-host Patch
shared-tree-shaking-with-server-provider Patch
website-new Patch
@module-federation/runtime Patch
@module-federation/enhanced Patch
@module-federation/rspack Patch
@module-federation/webpack-bundler-runtime Patch
@module-federation/sdk Patch
@module-federation/runtime-tools Patch
@module-federation/managers Patch
@module-federation/manifest Patch
@module-federation/dts-plugin Patch
@module-federation/third-party-dts-extractor Patch
@module-federation/devtools Patch
@module-federation/bridge-react Patch
@module-federation/bridge-vue3 Patch
@module-federation/bridge-shared Patch
@module-federation/bridge-react-webpack-plugin Patch
@module-federation/retry-plugin Patch
@module-federation/data-prefetch Patch
@module-federation/error-codes Patch
@module-federation/inject-external-runtime-core-plugin Patch
@module-federation/runtime-core Patch
create-module-federation Patch
@module-federation/cli Patch
@module-federation/treeshake-server Patch
@module-federation/treeshake-frontend Patch
@module-federation/metro Patch
@module-federation/metro-plugin-rnef Patch
@module-federation/metro-plugin-rnc-cli Patch
@module-federation/esbuild Patch
@module-federation/nextjs-mf Patch
@module-federation/node Patch
@module-federation/storybook-addon Patch
shared-tree-shaking-no-server-host Patch
shared-tree-shaking-no-server-provider Patch
@module-federation/utilities Patch
node-dynamic-remote-new-version Patch
node-dynamic-remote Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 83ea016794

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +57 to +59
process.env['IS_ESM_BUILD'] === 'true'
? esmRelativePath
: cjsRelativePath,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Keep IS_ESM_BUILD dot-access for define substitution

In resolvePackageFile(), switching to process.env['IS_ESM_BUILD'] breaks the build-time replacement configured in packages/modernjs/rslib.config.ts:30-31 and :44-45, which only defines the exact expression process.env.IS_ESM_BUILD. In the published ESM bundle this condition will stay runtime-dependent, and for normal consumers where that env var is unset it will always pick the CJS paths, so the ESM package can resolve dist/cjs/... plugins instead of the intended .mjs files.

Useful? React with 👍 / 👎.

Comment on lines +51 to +53
process.env['IS_ESM_BUILD'] === 'true'
? esmRelativePath
: cjsRelativePath,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve dot-access for IS_ESM_BUILD in v3 build

This has the same regression as the non-v3 package: packages/modernjs-v3/rslib.config.ts:30-31 and :44-45 inject process.env.IS_ESM_BUILD, but the new bracket access is a different expression and will not be replaced at build time. As a result, the generated ESM artifact now depends on a runtime env var that consumers do not set, so resolvePackageFile() can incorrectly load the CJS runtime plugin paths.

Useful? React with 👍 / 👎.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 18, 2026

Open in StackBlitz

@module-federation/devtools

pnpm add https://pkg.pr.new/@module-federation/devtools@4564

@module-federation/cli

pnpm add https://pkg.pr.new/@module-federation/cli@4564

create-module-federation

pnpm add https://pkg.pr.new/create-module-federation@4564

@module-federation/data-prefetch

pnpm add https://pkg.pr.new/@module-federation/data-prefetch@4564

@module-federation/dts-plugin

pnpm add https://pkg.pr.new/@module-federation/dts-plugin@4564

@module-federation/enhanced

pnpm add https://pkg.pr.new/@module-federation/enhanced@4564

@module-federation/error-codes

pnpm add https://pkg.pr.new/@module-federation/error-codes@4564

@module-federation/esbuild

pnpm add https://pkg.pr.new/@module-federation/esbuild@4564

@module-federation/managers

pnpm add https://pkg.pr.new/@module-federation/managers@4564

@module-federation/manifest

pnpm add https://pkg.pr.new/@module-federation/manifest@4564

@module-federation/metro

pnpm add https://pkg.pr.new/@module-federation/metro@4564

@module-federation/metro-plugin-rnc-cli

pnpm add https://pkg.pr.new/@module-federation/metro-plugin-rnc-cli@4564

@module-federation/metro-plugin-rnef

pnpm add https://pkg.pr.new/@module-federation/metro-plugin-rnef@4564

@module-federation/modern-js

pnpm add https://pkg.pr.new/@module-federation/modern-js@4564

@module-federation/modern-js-v3

pnpm add https://pkg.pr.new/@module-federation/modern-js-v3@4564

@module-federation/native-federation-tests

pnpm add https://pkg.pr.new/@module-federation/native-federation-tests@4564

@module-federation/native-federation-typescript

pnpm add https://pkg.pr.new/@module-federation/native-federation-typescript@4564

@module-federation/nextjs-mf

pnpm add https://pkg.pr.new/@module-federation/nextjs-mf@4564

@module-federation/node

pnpm add https://pkg.pr.new/@module-federation/node@4564

@module-federation/retry-plugin

pnpm add https://pkg.pr.new/@module-federation/retry-plugin@4564

@module-federation/rsbuild-plugin

pnpm add https://pkg.pr.new/@module-federation/rsbuild-plugin@4564

@module-federation/rspack

pnpm add https://pkg.pr.new/@module-federation/rspack@4564

@module-federation/rspress-plugin

pnpm add https://pkg.pr.new/@module-federation/rspress-plugin@4564

@module-federation/runtime

pnpm add https://pkg.pr.new/@module-federation/runtime@4564

@module-federation/runtime-core

pnpm add https://pkg.pr.new/@module-federation/runtime-core@4564

@module-federation/runtime-tools

pnpm add https://pkg.pr.new/@module-federation/runtime-tools@4564

@module-federation/sdk

pnpm add https://pkg.pr.new/@module-federation/sdk@4564

@module-federation/storybook-addon

pnpm add https://pkg.pr.new/@module-federation/storybook-addon@4564

@module-federation/third-party-dts-extractor

pnpm add https://pkg.pr.new/@module-federation/third-party-dts-extractor@4564

@module-federation/treeshake-frontend

pnpm add https://pkg.pr.new/@module-federation/treeshake-frontend@4564

@module-federation/treeshake-server

pnpm add https://pkg.pr.new/@module-federation/treeshake-server@4564

@module-federation/typescript

pnpm add https://pkg.pr.new/@module-federation/typescript@4564

@module-federation/utilities

pnpm add https://pkg.pr.new/@module-federation/utilities@4564

@module-federation/webpack-bundler-runtime

pnpm add https://pkg.pr.new/@module-federation/webpack-bundler-runtime@4564

@module-federation/bridge-react

pnpm add https://pkg.pr.new/@module-federation/bridge-react@4564

@module-federation/bridge-react-webpack-plugin

pnpm add https://pkg.pr.new/@module-federation/bridge-react-webpack-plugin@4564

@module-federation/bridge-shared

pnpm add https://pkg.pr.new/@module-federation/bridge-shared@4564

@module-federation/bridge-vue3

pnpm add https://pkg.pr.new/@module-federation/bridge-vue3@4564

@module-federation/inject-external-runtime-core-plugin

pnpm add https://pkg.pr.new/@module-federation/inject-external-runtime-core-plugin@4564

commit: 1334efa

@github-actions
Copy link
Contributor

Bundle Size Report

15 package(s) changed, 25 unchanged.

Package dist + ESM entry

Package Total dist (raw) Delta ESM gzip Delta
@module-federation/cli 26.7 kB no change 786 B no change
@module-federation/core 39.1 kB no change 173 B no change
@module-federation/devtools 473.2 kB no change 3.9 kB no change
@module-federation/enhanced 810.5 kB no change 672 B no change
@module-federation/esbuild 268.2 kB +65 B (+0.0%) 116 B no change
@module-federation/managers 69.9 kB no change 334 B no change
@module-federation/manifest 138.4 kB no change 182 B no change
@module-federation/metro-plugin-rnc-cli 0 B no change 314 B no change
@module-federation/modern-js 185.8 kB -115 B (-0.1%) 1.2 kB no change
@module-federation/modern-js-v3 176.9 kB -115 B (-0.1%) 872 B no change
@module-federation/node 189.2 kB no change 217 B no change
@module-federation/rsbuild-plugin 120.1 kB -309 B (-0.3%) 91 B no change
@module-federation/storybook-addon 77.0 kB no change 100 B no change
@module-federation/typescript 36.9 kB +70 B (+0.2%) 0 B no change
@module-federation/utilities 110.6 kB no change 328 B no change

Bundle targets

Package Web bundle (gzip) Delta Node bundle (gzip) Delta
@module-federation/cli 2.3 kB -33 B (-1.4%) 2.3 kB -33 B (-1.4%)
@module-federation/core 1.1 kB -31 B (-2.8%) 1.0 kB -31 B (-2.9%)
@module-federation/devtools 20.8 kB -26 B (-0.1%) 20.8 kB -26 B (-0.1%)
@module-federation/enhanced 2.6 kB -45 B (-1.6%) 2.6 kB -45 B (-1.6%)
@module-federation/esbuild 50 B no change 50 B no change
@module-federation/managers 2.4 kB -26 B (-1.0%) 2.4 kB -26 B (-1.0%)
@module-federation/manifest 6.2 kB -39 B (-0.6%) 6.2 kB -39 B (-0.6%)
@module-federation/metro-plugin-rnc-cli 411 B -26 B (-5.9%) 411 B -26 B (-5.9%)
@module-federation/modern-js 4.7 kB no change 4.7 kB no change
@module-federation/modern-js-v3 4.5 kB no change 4.5 kB no change
@module-federation/node 9.2 kB -29 B (-0.3%) 9.2 kB -29 B (-0.3%)
@module-federation/rsbuild-plugin 4.5 kB -16 B (-0.3%) 4.5 kB -16 B (-0.3%)
@module-federation/storybook-addon 1.9 kB -24 B (-1.2%) 1.7 kB -24 B (-1.3%)
@module-federation/typescript n/a n/a n/a n/a
@module-federation/utilities 2.6 kB -32 B (-1.2%) 2.6 kB -32 B (-1.2%)

Consumer scenarios

Scenario Web output (gzip) Delta Node output (gzip) Delta Gap (node-web) Delta
Enhanced remoteEntry 19.5 kB -17 B (-0.1%) 20.5 kB -14 B (-0.1%) +1.0 kB +3 B

Total dist (raw): 6.31 MB (-404 B (-0.0%))
Total ESM gzip: 73.4 kB (no change)
Total web bundle (gzip): 177.8 kB (-327 B (-0.2%))
Total node bundle (gzip): 178.3 kB (-327 B (-0.2%))
Tracked ./bundler entry gzip: 556 B (no change)
Tracked ./bundler web bundle (gzip): 4.5 kB (no change)
Tracked ./bundler node bundle (gzip): 4.5 kB (no change)

Bundle sizes are generated with rslib (Rspack). Package-root metrics preserve the historical report. Tracked subpath exports such as ./bundler are measured separately so ENV_TARGET-driven tree-shaking is visible. Bare imports are externalized to keep package-level sizes consistent, and assets are emitted as resources.

@2heal1 2heal1 merged commit 762b9ef into main Mar 18, 2026
28 checks passed
@2heal1 2heal1 deleted the fix/import-attr branch March 18, 2026 09:04
@2heal1 2heal1 mentioned this pull request Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant