From ed26dfb39b753f7d28ab1e3d6bd43fb90c972427 Mon Sep 17 00:00:00 2001 From: wanchun <445436867@qq.com> Date: Sun, 7 Sep 2025 16:48:28 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BD=BF=E7=94=A8=20pathToFileURL=20?= =?UTF-8?q?=E7=A1=AE=E4=BF=9D=20Windows=20=E4=B8=8B=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E6=AD=A3=E7=A1=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改多处文件导入逻辑,统一使用 pathToFileURL 转换路径为 URL 格式,解决 Windows 系统下路径格式问题导致的模块导入失败 --- packages/compiler/src/config/index.ts | 7 ++++++- packages/compiler/src/service/utils/pluginUtils.ts | 5 ++++- packages/preset-built-in/src/plugins/features/mock.ts | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/compiler/src/config/index.ts b/packages/compiler/src/config/index.ts index 89175eb8..b8817d24 100644 --- a/packages/compiler/src/config/index.ts +++ b/packages/compiler/src/config/index.ts @@ -3,6 +3,7 @@ import assert from 'node:assert'; import { existsSync } from 'node:fs'; import { extname, join } from 'node:path'; import process from 'node:process'; +import { pathToFileURL } from 'node:url'; import { chalk, chokidar, compatESModuleRequire, deepmerge, lodash, winPath } from '@fesjs/utils'; import joi from 'joi'; import { ServiceStage } from '../service/enums'; @@ -141,7 +142,11 @@ export default class Config { } async requireConfigs(configFiles: string[]): Promise { - const models = await Promise.all(configFiles.map(f => import(f))); + const models = await Promise.all(configFiles.map(f => { + // 使用 pathToFileURL 确保在 Windows 下路径格式正确 + const fileUrl = pathToFileURL(f).href; + return import(fileUrl); + })); return models.map(m => compatESModuleRequire(m)); } diff --git a/packages/compiler/src/service/utils/pluginUtils.ts b/packages/compiler/src/service/utils/pluginUtils.ts index 63efc63a..c507b204 100644 --- a/packages/compiler/src/service/utils/pluginUtils.ts +++ b/packages/compiler/src/service/utils/pluginUtils.ts @@ -1,6 +1,7 @@ import type { Plugin } from '../../types'; import { basename, dirname, extname, join, relative } from 'node:path'; import process from 'node:process'; +import { pathToFileURL } from 'node:url'; import { chalk, compatESModuleRequire, lodash, resolve, winPath } from '@fesjs/utils'; import { readJSONSync } from 'fs-extra/esm'; import { packageUp } from 'package-up'; @@ -158,7 +159,9 @@ export async function pathToObj({ path, type, cwd }: PathToObjOptions): Promise< path: winPath(path), async apply() { try { - const ret = await import(path); + // 使用 pathToFileURL 确保在 Windows 下路径格式正确 + const fileUrl = pathToFileURL(path).href; + const ret = await import(fileUrl); // use the default member for es modules return compatESModuleRequire(ret); } diff --git a/packages/preset-built-in/src/plugins/features/mock.ts b/packages/preset-built-in/src/plugins/features/mock.ts index 3c1cca10..c80c3ac9 100644 --- a/packages/preset-built-in/src/plugins/features/mock.ts +++ b/packages/preset-built-in/src/plugins/features/mock.ts @@ -1,6 +1,7 @@ import { existsSync, readFileSync } from 'node:fs'; import { resolve } from 'node:path'; import process from 'node:process'; +import { pathToFileURL } from 'node:url'; import { chokidar, lodash } from '@fesjs/utils'; export default (api) => { @@ -100,7 +101,7 @@ export default (api) => { // require最新的 mock.js 文件 try { // register babel - const _initFunction = await import(mockFile); + const _initFunction = await import(pathToFileURL(mockFile).href); const initFunction = _initFunction.default || _initFunction; if (!lodash.isFunction(initFunction)) { api.logger.info('mock.js should export Function');