Skip to content

Commit ed26dfb

Browse files
committed
fix: 使用 pathToFileURL 确保 Windows 下路径导入正确
修改多处文件导入逻辑,统一使用 pathToFileURL 转换路径为 URL 格式,解决 Windows 系统下路径格式问题导致的模块导入失败
1 parent 2c69c96 commit ed26dfb

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

packages/compiler/src/config/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import assert from 'node:assert';
33
import { existsSync } from 'node:fs';
44
import { extname, join } from 'node:path';
55
import process from 'node:process';
6+
import { pathToFileURL } from 'node:url';
67
import { chalk, chokidar, compatESModuleRequire, deepmerge, lodash, winPath } from '@fesjs/utils';
78
import joi from 'joi';
89
import { ServiceStage } from '../service/enums';
@@ -141,7 +142,11 @@ export default class Config {
141142
}
142143

143144
async requireConfigs(configFiles: string[]): Promise<any[]> {
144-
const models = await Promise.all(configFiles.map(f => import(f)));
145+
const models = await Promise.all(configFiles.map(f => {
146+
// 使用 pathToFileURL 确保在 Windows 下路径格式正确
147+
const fileUrl = pathToFileURL(f).href;
148+
return import(fileUrl);
149+
}));
145150
return models.map(m => compatESModuleRequire(m));
146151
}
147152

packages/compiler/src/service/utils/pluginUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Plugin } from '../../types';
22
import { basename, dirname, extname, join, relative } from 'node:path';
33
import process from 'node:process';
4+
import { pathToFileURL } from 'node:url';
45
import { chalk, compatESModuleRequire, lodash, resolve, winPath } from '@fesjs/utils';
56
import { readJSONSync } from 'fs-extra/esm';
67
import { packageUp } from 'package-up';
@@ -158,7 +159,9 @@ export async function pathToObj({ path, type, cwd }: PathToObjOptions): Promise<
158159
path: winPath(path),
159160
async apply() {
160161
try {
161-
const ret = await import(path);
162+
// 使用 pathToFileURL 确保在 Windows 下路径格式正确
163+
const fileUrl = pathToFileURL(path).href;
164+
const ret = await import(fileUrl);
162165
// use the default member for es modules
163166
return compatESModuleRequire(ret);
164167
}

packages/preset-built-in/src/plugins/features/mock.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { existsSync, readFileSync } from 'node:fs';
22
import { resolve } from 'node:path';
33
import process from 'node:process';
4+
import { pathToFileURL } from 'node:url';
45
import { chokidar, lodash } from '@fesjs/utils';
56

67
export default (api) => {
@@ -100,7 +101,7 @@ export default (api) => {
100101
// require最新的 mock.js 文件
101102
try {
102103
// register babel
103-
const _initFunction = await import(mockFile);
104+
const _initFunction = await import(pathToFileURL(mockFile).href);
104105
const initFunction = _initFunction.default || _initFunction;
105106
if (!lodash.isFunction(initFunction)) {
106107
api.logger.info('mock.js should export Function');

0 commit comments

Comments
 (0)