perf: require rspack to improve startup performance#5673
Merged
chenjiahan merged 1 commit intomainfrom Jul 25, 2025
Merged
Conversation
✅ Deploy Preview for rsbuild ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR improves startup performance by replacing direct ES module imports of @rspack/core with a CommonJS require approach. The change addresses a ~30ms startup delay caused by Node.js's cjs-module-lexer when importing from the CJS-only Rspack bundle.
Key changes:
- Created a new
rspack.tsmodule that usescreateRequireto load Rspack via CommonJS - Updated all files to import
rspackfrom the new module instead of directly from@rspack/core - Modified plugin files to receive
rspackfrom the bundler chain context instead of importing it
Reviewed Changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/core/src/rspack.ts | New module providing CommonJS-based Rspack import for performance |
| packages/core/src/server/devMiddlewares.ts | Updated to use centralized rspack import |
| packages/core/src/provider/rspackConfig.ts | Updated to use centralized rspack import |
| packages/core/src/provider/createCompiler.ts | Updated to use centralized rspack import |
| packages/core/src/provider/build.ts | Updated to use centralized rspack import |
| packages/core/src/plugins/sri.ts | Updated to receive rspack from chain context |
| packages/core/src/plugins/rspackProfile.ts | Updated to use centralized rspack import |
| packages/core/src/plugins/progress.ts | Updated to receive rspack from chain context |
| packages/core/src/plugins/output.ts | Updated to receive rspack from chain context |
| packages/core/src/plugins/moduleFederation.ts | Updated to use centralized rspack import |
| packages/core/src/plugins/minimize.ts | Updated to receive rspack from chain context |
| packages/core/src/pluginHelper.ts | Updated to use centralized rspack import |
| packages/core/src/index.ts | Updated to use centralized rspack import |
Comments suppressed due to low confidence (1)
packages/core/src/rspack.ts:10
- The variable name
rspackon line 10 shadows the exportedrspackon line 13, which could be confusing. Consider renaming the internal variable torspackModuleorrspackCorefor clarity.
const rspack =
Merged
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Currently, Rspack only provides a CJS bundle, so we use require to load it for better startup performance. If we use
import { rspack } from '@rspack/core', Node.js will usecjs-module-lexerto parse it, which slows down startup by 25-30ms.We can remove this module once
@rspack/coreprovides an ESM bundle in the future.Before
After
Related Links
Checklist