Skip to content

perf: update vm imports to use named imports and dynamic require#5313

Merged
chenjiahan merged 1 commit intomainfrom
vm_dymamic_0526
May 26, 2025
Merged

perf: update vm imports to use named imports and dynamic require#5313
chenjiahan merged 1 commit intomainfrom
vm_dymamic_0526

Conversation

@chenjiahan
Copy link
Copy Markdown
Member

Summary

Update the node:vm imports to use named imports and dynamic require for better performance.

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Copilot AI review requested due to automatic review settings May 26, 2025 09:27
@netlify
Copy link
Copy Markdown

netlify Bot commented May 26, 2025

Deploy Preview for rsbuild failed. Why did it fail? →

Name Link
🔨 Latest commit 5517705
🔍 Latest deploy log https://app.netlify.com/projects/rsbuild/deploys/683433fe4e0adf00088b238a

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 createRequire and dynamic require('node:vm') in both ESM and CJS runners
  • Convert asModule to dynamically import Module and SyntheticModule
  • Remove static vm imports 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 when SourceTextModule is unavailable.
const vm = require('node:vm') as typeof import('node:vm');

packages/core/src/server/runner/cjs.ts:89

  • The require function is not defined in this CJS file. You need to add const 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(
Copy link

Copilot AI May 26, 2025

Choose a reason for hiding this comment

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

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 }).

Copilot uses AI. Check for mistakes.
@netlify
Copy link
Copy Markdown

netlify Bot commented May 26, 2025

Deploy Preview for rsbuild failed. Why did it fail? →

Name Link
🔨 Latest commit 5517705
🔍 Latest deploy log https://app.netlify.com/projects/rsbuild/deploys/683433fe4e0adf00088b238a

2 similar comments
@netlify
Copy link
Copy Markdown

netlify Bot commented May 26, 2025

Deploy Preview for rsbuild failed. Why did it fail? →

Name Link
🔨 Latest commit 5517705
🔍 Latest deploy log https://app.netlify.com/projects/rsbuild/deploys/683433fe4e0adf00088b238a

@netlify
Copy link
Copy Markdown

netlify Bot commented May 26, 2025

Deploy Preview for rsbuild failed. Why did it fail? →

Name Link
🔨 Latest commit 5517705
🔍 Latest deploy log https://app.netlify.com/projects/rsbuild/deploys/683433fe4e0adf00088b238a

@chenjiahan chenjiahan merged commit fa0f574 into main May 26, 2025
7 of 17 checks passed
@chenjiahan chenjiahan deleted the vm_dymamic_0526 branch May 26, 2025 10:54
@chenjiahan chenjiahan mentioned this pull request May 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants