Skip to content

Commit 45aaf01

Browse files
committed
Merge branch 'master' of github.com:uni-ku/define-page
2 parents 20e9d38 + 1ad6259 commit 45aaf01

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/context.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ export const ctx = new Context();
251251
function listFiles(dir: string, options: fg.Options = {}) {
252252
const { cwd, ignore = [], deep = 3, ...others } = options;
253253
// fast-glob also use '/' for windows
254-
const source = FILE_EXTENSIONS.map(ext => fg.convertPathToPattern(`${dir}/**/*.${ext}`));
254+
const source = FILE_EXTENSIONS.map(ext => `${fg.convertPathToPattern(`${dir}`)}/**/*.${ext}`);
255255
const files = fg.sync(source, {
256256
cwd: cwd ? fg.convertPathToPattern(cwd) : undefined,
257257
ignore: ignore.map(fg.convertPathToPattern),

src/file.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import type { SFCScriptBlock } from '@vue/compiler-sfc';
22
import type { ParseResult } from 'ast-kit';
33
import type { Page } from './page';
44
import fs from 'node:fs';
5+
import { createRequire } from 'node:module';
56
import path from 'node:path';
6-
import process from 'node:process';
77
import vm from 'node:vm';
8-
import generate from '@babel/generator';
98
import * as t from '@babel/types';
109
import { parse as parseSFC } from '@vue/compiler-sfc';
1110
import { babelParse, isCallOf } from 'ast-kit';
1211
import * as ts from 'typescript';
1312
import { getConfig } from './config';
13+
import { generate } from './utils/babel';
1414
import { debug } from './utils/debug';
1515

1616
interface ScriptSetup extends SFCScriptBlock {
@@ -186,11 +186,7 @@ function parseScriptSetup(file: File) {
186186

187187
async function exec<R = any>(file: string, exp: t.Expression, imports: t.ImportDeclaration[]): Promise<R | undefined> {
188188

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;
194190

195191
let script = '';
196192

@@ -224,13 +220,15 @@ async function executeTypeScriptCode(code: string, filename: string): Promise<an
224220
},
225221
}).outputText;
226222

223+
const dir = path.dirname(filename);
224+
227225
// 创建一个新的虚拟机上下文
228226
const vmContext = {
229-
require,
227+
require: createRequire(dir),
230228
module: {},
231229
exports: {},
232230
__filename: filename,
233-
__dirname: path.dirname(filename),
231+
__dirname: dir,
234232
};
235233

236234
// 使用 vm 模块执行 JavaScript 代码

src/utils/babel/index.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import babelGenerator from '@babel/generator';
2+
import * as babelTypes from '@babel/types';
3+
4+
function getDefaultExport<T = any>(expr: T): T {
5+
return (expr as any).default === undefined ? expr : (expr as any).default;
6+
}
7+
8+
export const generate = getDefaultExport(babelGenerator);
9+
10+
export const t = getDefaultExport(babelTypes);

0 commit comments

Comments
 (0)