Skip to content

Commit fa0f574

Browse files
authored
perf: update vm imports to use named imports and dynamic require (#5313)
1 parent 4490d91 commit fa0f574

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

packages/core/src/server/runner/asModule.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
import vm from 'node:vm';
1+
import type { ModuleLinker, SourceTextModule } from 'node:vm';
22

33
export const asModule = async (
44
something: Record<string, any>,
55
context: Record<string, any>,
66
unlinked?: boolean,
7-
): Promise<vm.SourceTextModule> => {
8-
if (something instanceof vm.Module) {
7+
): Promise<SourceTextModule> => {
8+
const { Module, SyntheticModule } = await import('node:vm');
9+
10+
if (something instanceof Module) {
911
return something;
1012
}
1113

1214
const exports = [...new Set(['default', ...Object.keys(something)])];
1315

14-
const m = new vm.SyntheticModule(
16+
const m = new SyntheticModule(
1517
exports,
1618
() => {
1719
for (const name of exports) {
@@ -22,8 +24,11 @@ export const asModule = async (
2224
context,
2325
},
2426
);
27+
2528
if (unlinked) return m;
26-
await m.link((() => {}) as () => vm.Module);
29+
30+
await m.link((() => {}) as unknown as ModuleLinker);
31+
2732
// @ts-expect-error copy from webpack
2833
if (m.instantiate) m.instantiate();
2934
await m.evaluate();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createRequire } from 'node:module';
22
import path from 'node:path';
3-
import vm from 'node:vm';
43
import { BasicRunner } from './basic';
54
import type {
65
BasicGlobalContext,
@@ -87,6 +86,7 @@ export class CommonJsRunner extends BasicRunner {
8786

8887
protected createCjsRequirer(): RunnerRequirer {
8988
const requireCache = Object.create(null);
89+
const vm = require('node:vm') as typeof import('node:vm');
9090

9191
return (currentDirectory, modulePath, context = {}) => {
9292
const file = context.file || this.getFile(modulePath, currentDirectory);

packages/core/src/server/runner/esm.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import { createRequire } from 'node:module';
12
import path from 'node:path';
23
import { fileURLToPath, pathToFileURL } from 'node:url';
3-
import vm, { type SourceTextModule } from 'node:vm';
4-
import { asModule } from './asModule';
5-
4+
import type { SourceTextModule } from 'node:vm';
65
import { color } from '../../helpers';
6+
import { asModule } from './asModule';
77
import { CommonJsRunner } from './cjs';
88
import { EsmMode, type RunnerRequirer } from './type';
99

10+
const require = createRequire(import.meta.url);
11+
1012
export class EsmRunner extends CommonJsRunner {
1113
protected createRunner(): void {
1214
super.createRunner();
@@ -38,6 +40,8 @@ export class EsmRunner extends CommonJsRunner {
3840
protected createEsmRequirer(): RunnerRequirer {
3941
const esmCache = new Map<string, SourceTextModule>();
4042
const esmIdentifier = this._options.name;
43+
const vm = require('node:vm') as typeof import('node:vm');
44+
4145
return (currentDirectory, modulePath, context = {}) => {
4246
if (!vm.SourceTextModule) {
4347
throw new Error(

0 commit comments

Comments
 (0)