diff --git a/src/webapi/_loader.ts b/src/webapi/_loader.ts index e8306a4..4b20000 100644 --- a/src/webapi/_loader.ts +++ b/src/webapi/_loader.ts @@ -1,19 +1,21 @@ /** - * _loader.ts — 通用 web 端源码加载器 (v0.11+ Phase 1) + * _loader.ts — 通用 web 端源码加载器 (v0.11+ Phase 1, v3.0+ @vben/request shim) * * 设计: 与 _buildformdata.ts 同样的技巧 — 读源码 → 替换 import → 写 tmp → dynamic import * * 替换规则 (按顺序, 避免误伤): - * 1. `from '#/api/request'` → `from ''` (defHttp shim, 优先于 #/) - * 2. `from '#/...'` → `from '${WEB_SRC}/...'` (其他 #/ alias) - * 3. `from '@jnpf/utils'` → `from '${JNPF_UTILS_UUID}'` (uuid only, 避开 Vite env) - * 4. `import { cloneDeep } from 'lodash-es';?` → `const cloneDeep = (x) => structuredClone(x);` - * 5. `import { omit } from 'lodash-es';?` → `function omit(obj, keys) { ... }` + * 1. `from '#/api/request'` → `from ''` (defHttp shim, 优先于 #/) + * 2. `from '#/...'` → `from '${WEB_SRC}/...'` (其他 #/ alias) + * 3. `from '@jnpf/utils'` → `from '${JNPF_UTILS_UUID}'` (uuid only, 避开 Vite env) + * 4. `from '@vben/request'` → `from ''` (v3.0+ enum shim) + * 5. `import { cloneDeep } from 'lodash-es';?` → `const cloneDeep = (x) => structuredClone(x);` + * 6. `import { omit } from 'lodash-es';?` → `function omit(obj, keys) { ... }` * * 注意: * - 不修改 web/ 源码 (CLAUDE.md §15 禁止) * - 单进程缓存: 同一 source 路径只 load 一次 - * - shim 路径指向 _defhttp_shim.ts (位于本目录) + * - shim 路径指向 _defhttp_shim.ts / _vben_request_shim.ts (位于本目录) + * - v3.0+ 新增 @vben/request shim (支持 web/api/core/auth.ts 的 ContentTypeEnum import) */ import { readFileSync, writeFileSync, mkdirSync } from 'fs' import { tmpdir } from 'os' @@ -22,7 +24,9 @@ import { pathToFileURL } from 'url' const WEB_SRC = '/home/ubuntu/ccWorkDir/web/apps/jnpf-web-apps-main/src' const JNPF_UTILS_UUID = '/home/ubuntu/ccWorkDir/web/packages/jnpf/utils/src/helpers/uuid.ts' -const SHIM_PATH = join(dirname(new URL(import.meta.url).pathname), '_defhttp_shim.ts') +const SHIM_DIR = dirname(new URL(import.meta.url).pathname) +const SHIM_PATH = join(SHIM_DIR, '_defhttp_shim.ts') +const VBEN_REQUEST_SHIM_PATH = join(SHIM_DIR, '_vben_request_shim.ts') /** lodash omit 的最小实现 (web dataModel.ts 用) */ const LODASH_OMIT_STUB = ` @@ -36,6 +40,8 @@ const omit = (obj, keys) => { export interface LoadOptions { /** 自定义 shim 路径 (默认 _defhttp_shim.ts) */ shimPath?: string + /** 自定义 @vben/request shim 路径 (默认 _vben_request_shim.ts) */ + vbenRequestShimPath?: string /** 跳过 lodash 替换 (默认 true, visualDev.ts 不需要) */ handleLodash?: boolean } @@ -53,6 +59,7 @@ export async function loadWebModule(sourcePath: string, opts: LoadOptio const promise = (async () => { const shimPath = opts.shimPath ?? SHIM_PATH + const vbenShimPath = opts.vbenRequestShimPath ?? VBEN_REQUEST_SHIM_PATH // 1. 读源 let src = readFileSync(sourcePath, 'utf-8') @@ -66,6 +73,9 @@ export async function loadWebModule(sourcePath: string, opts: LoadOptio // 4. 替换 @jnpf/utils → uuid (避开 Vite env) src = src.replace(/from\s+['"]@jnpf\/utils['"]/g, `from '${JNPF_UTILS_UUID}'`) + // 5. v3.0+ 替换 @vben/request → vben request shim (ContentTypeEnum/RequestEnum/ResultEnum) + src = src.replace(/from\s+['"]@vben\/request['"]/g, `from '${vbenShimPath}'`) + // 5. 替换 lodash-es → Node 原生实现 (按需) if (opts.handleLodash !== false) { src = src.replace(