@@ -2,15 +2,15 @@ import type { SFCScriptBlock } from '@vue/compiler-sfc';
2
2
import type { ParseResult } from 'ast-kit' ;
3
3
import type { Page } from './page' ;
4
4
import fs from 'node:fs' ;
5
+ import { createRequire } from 'node:module' ;
5
6
import path from 'node:path' ;
6
- import process from 'node:process' ;
7
7
import vm from 'node:vm' ;
8
- import generate from '@babel/generator' ;
9
8
import * as t from '@babel/types' ;
10
9
import { parse as parseSFC } from '@vue/compiler-sfc' ;
11
10
import { babelParse , isCallOf } from 'ast-kit' ;
12
11
import * as ts from 'typescript' ;
13
12
import { getConfig } from './config' ;
13
+ import { generate } from './utils/babel' ;
14
14
import { debug } from './utils/debug' ;
15
15
16
16
interface ScriptSetup extends SFCScriptBlock {
@@ -186,11 +186,7 @@ function parseScriptSetup(file: File) {
186
186
187
187
async function exec < R = any > ( file : string , exp : t . Expression , imports : t . ImportDeclaration [ ] ) : Promise < R | undefined > {
188
188
189
- const ast = t . file ( t . program ( [
190
- t . expressionStatement ( exp ) ,
191
- ] ) ) ;
192
-
193
- const code = generate ( ast ) . code ;
189
+ const code = generate ( exp ) . code ;
194
190
195
191
let script = '' ;
196
192
@@ -224,13 +220,15 @@ async function executeTypeScriptCode(code: string, filename: string): Promise<an
224
220
} ,
225
221
} ) . outputText ;
226
222
223
+ const dir = path . dirname ( filename ) ;
224
+
227
225
// 创建一个新的虚拟机上下文
228
226
const vmContext = {
229
- require,
227
+ require : createRequire ( dir ) ,
230
228
module : { } ,
231
229
exports : { } ,
232
230
__filename : filename ,
233
- __dirname : path . dirname ( filename ) ,
231
+ __dirname : dir ,
234
232
} ;
235
233
236
234
// 使用 vm 模块执行 JavaScript 代码
0 commit comments