实现 docs/dacli/v0.11-mirror-refactor.md Step 1-3: - src/webapi/_loader.ts: 通用 web 端源码加载器 (读源→替换 import→写 tmp→dynamic import) - src/webapi/_defhttp_shim.ts: 用 JnpfClient 实现 web 端 defHttp (get/post/put/delete) - src/webapi/visualDev.ts: 30+ 函数直接 import web/.../api/onlineDev/visualDev.ts - src/webapi/dataModel.ts: 10 函数 import + lodash omit shim - src/webapi/dataSource.ts: 11 函数 import - src/commands/form/list.ts: 改造样板 (c.get → loadVisualDev.getVisualDevList) 关键技术: - 读 web 端源 → 替换 #/api/request → shim 路径 → 替换 #/ → WEB_SRC → 替换 lodash-es → Node 原生 - 用 globalThis 共享 client (避免 tsx/动态 import 多实例) - shim 自动 strip /api 前缀 (因 JnpfClient.baseUrl 已含 /api) Checkpoint PASS: - test-webapi-visualdev.ts: visualDev 11 函数 import + 实测 (getVisualDevList + getInfo 返回数据正确) - test-webapi-datamodel.ts: dataModel 10 函数 import + 实测 (getDataModelList 返回数据) - dataSource smoke test: getDataSourceSelector 返回 2 项 - form list 命令 webapi 版与原版输出完全一致 - baseline 跑通, 无新 fail Co-Authored-By: Claude <[email protected]>
103 lines
3.8 KiB
TypeScript
103 lines
3.8 KiB
TypeScript
/**
|
|
* _loader.ts — 通用 web 端源码加载器 (v0.11+ Phase 1)
|
|
*
|
|
* 设计: 与 _buildformdata.ts 同样的技巧 — 读源码 → 替换 import → 写 tmp → dynamic import
|
|
*
|
|
* 替换规则 (按顺序, 避免误伤):
|
|
* 1. `from '#/api/request'` → `from '<shim path>'` (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) { ... }`
|
|
*
|
|
* 注意:
|
|
* - 不修改 web/ 源码 (CLAUDE.md §15 禁止)
|
|
* - 单进程缓存: 同一 source 路径只 load 一次
|
|
* - shim 路径指向 _defhttp_shim.ts (位于本目录)
|
|
*/
|
|
import { readFileSync, writeFileSync, mkdirSync } from 'fs'
|
|
import { tmpdir } from 'os'
|
|
import { join, dirname } from 'path'
|
|
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')
|
|
|
|
/** lodash omit 的最小实现 (web dataModel.ts 用) */
|
|
const LODASH_OMIT_STUB = `
|
|
const omit = (obj, keys) => {
|
|
if (!obj) return obj;
|
|
const out = { ...obj };
|
|
for (const k of (Array.isArray(keys) ? keys : [keys])) delete out[k];
|
|
return out;
|
|
};`
|
|
|
|
export interface LoadOptions {
|
|
/** 自定义 shim 路径 (默认 _defhttp_shim.ts) */
|
|
shimPath?: string
|
|
/** 跳过 lodash 替换 (默认 true, visualDev.ts 不需要) */
|
|
handleLodash?: boolean
|
|
}
|
|
|
|
/** 单进程缓存: path → module */
|
|
const cache = new Map<string, Promise<any>>()
|
|
|
|
/**
|
|
* 加载 web 端 TypeScript 文件, 替换 import, dynamic import
|
|
* @param sourcePath web 端 .ts 源文件绝对路径
|
|
* @returns 加载后的 ES module (含所有 named export)
|
|
*/
|
|
export async function loadWebModule<T = any>(sourcePath: string, opts: LoadOptions = {}): Promise<T> {
|
|
if (cache.has(sourcePath)) return cache.get(sourcePath) as Promise<T>
|
|
|
|
const promise = (async () => {
|
|
const shimPath = opts.shimPath ?? SHIM_PATH
|
|
|
|
// 1. 读源
|
|
let src = readFileSync(sourcePath, 'utf-8')
|
|
|
|
// 2. 替换 #/api/request → shim (必须在 #/ 通用规则之前)
|
|
src = src.replace(/from\s+['"]#\/api\/request['"]/g, `from '${shimPath}'`)
|
|
|
|
// 3. 替换其他 #/... → WEB_SRC/... (但已替换的 #/api/request 不再被命中)
|
|
src = src.replace(/from\s+['"]#\/(.+?)['"]/g, (_, p) => `from '${WEB_SRC}/${p}'`)
|
|
|
|
// 4. 替换 @jnpf/utils → uuid (避开 Vite env)
|
|
src = src.replace(/from\s+['"]@jnpf\/utils['"]/g, `from '${JNPF_UTILS_UUID}'`)
|
|
|
|
// 5. 替换 lodash-es → Node 原生实现 (按需)
|
|
if (opts.handleLodash !== false) {
|
|
src = src.replace(
|
|
/import\s*\{\s*cloneDeep\s*\}\s*from\s+['"]lodash-es['"];?/g,
|
|
`const cloneDeep = (x) => structuredClone(x);`,
|
|
)
|
|
src = src.replace(
|
|
/import\s*\{\s*omit\s*\}\s*from\s+['"]lodash-es['"];?/g,
|
|
LODASH_OMIT_STUB,
|
|
)
|
|
}
|
|
|
|
// 6. 写 tmp
|
|
const tmpDir = `${tmpdir()}/dacli-webapi-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`
|
|
mkdirSync(tmpDir, { recursive: true })
|
|
const tmpFile = join(tmpDir, sourcePath.split('/').pop()!)
|
|
writeFileSync(tmpFile, src)
|
|
|
|
// 7. dynamic import
|
|
return await import(pathToFileURL(tmpFile).href)
|
|
})()
|
|
|
|
cache.set(sourcePath, promise)
|
|
return promise
|
|
}
|
|
|
|
/** 清空缓存 (测试用) */
|
|
export function clearCache(): void {
|
|
cache.clear()
|
|
}
|
|
|
|
/** 检查缓存状态 (调试用) */
|
|
export function getCacheKeys(): string[] {
|
|
return Array.from(cache.keys())
|
|
} |