perf: update vm imports to use named imports and dynamic require#5313
Merged
chenjiahan merged 1 commit intomainfrom May 26, 2025
Merged
perf: update vm imports to use named imports and dynamic require#5313chenjiahan merged 1 commit intomainfrom
chenjiahan merged 1 commit intomainfrom
Conversation
❌ Deploy Preview for rsbuild failed. Why did it fail? →
|
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR refactors how the node:vm module is loaded by switching from static imports to named/dynamic requires for potential performance improvements.
- Use
createRequireand dynamicrequire('node:vm')in both ESM and CJS runners - Convert
asModuleto dynamically importModuleandSyntheticModule - Remove static
vmimports and adjust TypeScript types
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/core/src/server/runner/esm.ts | Added createRequire and dynamic require('node:vm') for ESM runner |
| packages/core/src/server/runner/cjs.ts | Removed static vm import and added dynamic require('node:vm') |
| packages/core/src/server/runner/asModule.ts | Switched to dynamic import('node:vm'), updated SyntheticModule usage |
Comments suppressed due to low confidence (2)
packages/core/src/server/runner/esm.ts:43
- New dynamic
require('node:vm')logic in the ESM runner isn’t covered by existing tests. Add tests to verify both successful module creation and the fallback error whenSourceTextModuleis unavailable.
const vm = require('node:vm') as typeof import('node:vm');
packages/core/src/server/runner/cjs.ts:89
- The
requirefunction is not defined in this CJS file. You need to addconst require = createRequire(import.meta.url);at the top to avoid a ReferenceError.
const vm = require('node:vm') as typeof import('node:vm');
| const exports = [...new Set(['default', ...Object.keys(something)])]; | ||
|
|
||
| const m = new vm.SyntheticModule( | ||
| const m = new SyntheticModule( |
There was a problem hiding this comment.
The third argument to SyntheticModule should be an options object (e.g. { context }) instead of passing the raw context. Update to new SyntheticModule(exports, init, { context }).
❌ Deploy Preview for rsbuild failed. Why did it fail? →
|
2 similar comments
❌ Deploy Preview for rsbuild failed. Why did it fail? →
|
❌ Deploy Preview for rsbuild failed. Why did it fail? →
|
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
Update the
node:vmimports to use named imports and dynamic require for better performance.Checklist