From b4b37c8b0929064b3b72e8054ad958138dee5922 Mon Sep 17 00:00:00 2001 From: dadigua <0laopo0@gmail.com> Date: Sat, 16 Aug 2025 21:02:31 +0800 Subject: [PATCH] =?UTF-8?q?feat(build):=20=E4=BC=98=E5=8C=96=20TypeScript?= =?UTF-8?q?=20=E8=B7=AF=E5=BE=84=E5=88=AB=E5=90=8D=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=92=8C=E6=9E=84=E5=BB=BA=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 升级版本号至 2.0.0-alpha.57 - 增强 build.mjs 脚本,自动替换 @dadigua/hyperchat-shared 路径别名 - 优化 tsconfig.json 中的路径配置,移除冗余的通配符映射 - 清理项目中的临时数据文件 (collectedMessages.json, messages.json) - 确保编译后的文件能够正确解析共享包引用 解决了 TypeScript 编译后路径别名无法正确解析的问题,提高构建输出的可移植性。 --- package-lock.json | 4 +- package.json | 2 +- packages/core/build.mjs | 115 ++++++++++++++++++++++++++- packages/core/collectedMessages.json | 58 -------------- packages/core/messages.json | 58 -------------- packages/core/package.json | 2 +- packages/core/tsconfig.json | 5 +- packages/electron/package.json | 2 +- packages/shared/package.json | 2 +- packages/web/package.json | 2 +- packages/web/tsconfig.json | 3 - 11 files changed, 121 insertions(+), 132 deletions(-) delete mode 100644 packages/core/collectedMessages.json delete mode 100644 packages/core/messages.json diff --git a/package-lock.json b/package-lock.json index c83edb32..31747e92 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dadigua/hyperchat", - "version": "2.0.0-alpha.56", + "version": "2.0.0-alpha.57", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dadigua/hyperchat", - "version": "2.0.0-alpha.56", + "version": "2.0.0-alpha.57", "dependencies": { "@ai-sdk/anthropic": "^1.2.12", "@ai-sdk/google": "^1.2.19", diff --git a/package.json b/package.json index 6a3b7dbb..530782f5 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "name": "Dadigua", "email": "0laopo0@gmail.com" }, - "version": "2.0.0-alpha.56", + "version": "2.0.0-alpha.57", "private": true, "scripts": { "dev": "node scripts/build.mjs dev", diff --git a/packages/core/build.mjs b/packages/core/build.mjs index c2aed8b0..f89d6cfd 100644 --- a/packages/core/build.mjs +++ b/packages/core/build.mjs @@ -1,7 +1,7 @@ #!/usr/bin/env node import { execSync } from 'child_process'; -import { existsSync, mkdirSync, rmSync, copyFileSync } from 'fs'; -import { join, dirname } from 'path'; +import { existsSync, mkdirSync, rmSync, copyFileSync, readFileSync, writeFileSync, readdirSync } from 'fs'; +import { join, dirname, relative, resolve } from 'path'; import { fileURLToPath } from 'url'; const __dirname = dirname(fileURLToPath(import.meta.url)); @@ -51,4 +51,115 @@ if (existsSync(sharedI18nPath)) { console.log(`已复制: ${sharedI18nPath} -> ${distI18nPath}`); } else { console.warn(`警告: 找不到i18n.json文件: ${sharedI18nPath}`); +} + +// 替换tsconfig.paths中的别名引用为实际路径 +console.log('替换路径别名...'); +replacePathAliases(); + +/** + * 替换tsconfig中定义的paths别名引用 + */ +function replacePathAliases() { + const sharedPackageName = '@dadigua/hyperchat-shared'; + + // 检查shared包的输出文件类型 + const sharedIndexPath = join(distDir, 'shared', 'src', 'index.mjs'); + const sharedIndexJsPath = join(distDir, 'shared', 'src', 'index.js'); + const sharedIndexDtsPath = join(distDir, 'shared', 'src', 'index.d.ts'); + + let sharedPackagePath; + let sharedDtsPath; + if (existsSync(sharedIndexPath)) { + sharedPackagePath = 'index.mjs'; + sharedDtsPath = 'index.d.ts'; + } else if (existsSync(sharedIndexJsPath)) { + sharedPackagePath = 'index.js'; + sharedDtsPath = 'index.d.ts'; + } else { + console.warn('警告: 找不到shared包的index文件'); + return; + } + + // 查找所有.mjs和.d.ts文件 + const mjsFiles = findFiles(distDir, '.mjs'); + const dtsFiles = findFiles(distDir, '.d.ts'); + const dmtsFiles = findFiles(distDir, '.d.mts'); + + const allFiles = [...mjsFiles, ...dtsFiles, ...dmtsFiles]; + + console.log(`找到 ${allFiles.length} 个文件需要处理 (.mjs: ${mjsFiles.length}, .d.ts: ${dtsFiles.length}, .d.mts: ${dmtsFiles.length})`); + + allFiles.forEach(filePath => { + try { + let content = readFileSync(filePath, 'utf8'); + + // 检查是否包含需要替换的别名 + if (content.includes(sharedPackageName)) { + // 计算相对路径 + const fileDir = dirname(filePath); + const isTypeDeclaration = filePath.endsWith('.d.ts') || filePath.endsWith('.d.mts'); + + let targetPath; + if (isTypeDeclaration) { + targetPath = join(distDir, 'shared', 'src', sharedDtsPath); + } else { + targetPath = join(distDir, 'shared', 'src', sharedPackagePath); + } + + const relativePath = relative(fileDir, targetPath); + + // 确保路径格式正确(使用./开头) + const normalizedRelativePath = relativePath.startsWith('.') ? relativePath : './' + relativePath; + + // 替换所有引用(支持import type和import语句) + const originalContent = content; + + // 替换import type引用 + content = content.replace( + new RegExp(`from\s+['"]${sharedPackageName}['"]`, 'g'), + `from '${normalizedRelativePath}'` + ); + + // 替换import引用(确保不重复替换) + content = content.replace( + new RegExp(`['"]${sharedPackageName}['"]`, 'g'), + `'${normalizedRelativePath}'` + ); + + if (content !== originalContent) { + writeFileSync(filePath, content, 'utf8'); + console.log(`已更新: ${filePath} -> ${normalizedRelativePath}`); + } + } + } catch (error) { + console.warn(`处理文件 ${filePath} 时出错: ${error.message}`); + } + }); +} + +/** + * 查找指定目录下的所有指定扩展名的文件 + */ +function findFiles(dir, extension) { + const files = []; + + function traverse(currentDir) { + if (!existsSync(currentDir)) return; + + const items = readdirSync(currentDir, { withFileTypes: true }); + + items.forEach(item => { + const fullPath = join(currentDir, item.name); + + if (item.isDirectory()) { + traverse(fullPath); + } else if (item.name.endsWith(extension)) { + files.push(fullPath); + } + }); + } + + traverse(dir); + return files; } \ No newline at end of file diff --git a/packages/core/collectedMessages.json b/packages/core/collectedMessages.json deleted file mode 100644 index 61e3f6fb..00000000 --- a/packages/core/collectedMessages.json +++ /dev/null @@ -1,58 +0,0 @@ -[ - { - "type": "user", - "messages": [ - { - "role": "user", - "content": "当前时间", - "content_status": "success", - "content_date": 1753175279775, - "content_attached": true - } - ], - "index": 0 - }, - { - "type": "assistant_group", - "messages": [ - { - "role": "assistant", - "content": "我来为您查询当前时间。", - "reasoning_content": "", - "content_tool_calls": [ - { - "index": 0, - "id": "hyper_system_run_shell_command:0", - "type": "function", - "function": { - "name": "hyper_system_run_shell_command", - "args": { - "command": "date", - "working_directory": "/home/laop/projects/HyperChat/packages/core", - "timeout": 30000, - "capture_output": true - } - }, - "originalName": "run_shell_command", - "displayName": "hyper_system > run_shell_command" - } - ], - "content_status": "dataLoadComplete", - "content_date": 1753175279775, - "content_attached": true - }, - { - "role": "tool", - "content": "当前时间是:**2025年7月22日 星期二 17:08:09(中国标准时间)**", - "tool_call_name": "hyper_system_run_shell_command", - "content_status": "dataLoadComplete", - "tool_call_id": "hyper_system_run_shell_command:0", - "content_date": 1753175289448, - "content_attached": true, - "reasoning_content": "", - "content_tool_calls": [] - } - ], - "index": 2 - } -] \ No newline at end of file diff --git a/packages/core/messages.json b/packages/core/messages.json deleted file mode 100644 index 81e60467..00000000 --- a/packages/core/messages.json +++ /dev/null @@ -1,58 +0,0 @@ -[ - { - "type": "user", - "messages": [ - { - "role": "user", - "content": "当前时间", - "content_status": "success", - "content_date": 1753173408743, - "content_attached": true - } - ], - "index": 0 - }, - { - "type": "assistant_group", - "messages": [ - { - "role": "assistant", - "content": "我来为您查询当前时间。", - "reasoning_content": "", - "content_tool_calls": [ - { - "index": 0, - "id": "hyper_system_run_shell_command:0", - "type": "function", - "function": { - "name": "hyper_system_run_shell_command", - "args": { - "command": "date", - "working_directory": "/home/laop/projects/HyperChat/packages/core", - "timeout": 30000, - "capture_output": true - } - }, - "originalName": "run_shell_command", - "displayName": "hyper_system > run_shell_command" - } - ], - "content_status": "dataLoadComplete", - "content_date": 1753173408743, - "content_attached": true - }, - { - "role": "tool", - "content": "当前时间是:**2025年7月22日 星期二 16:36:52(中国标准时间)**", - "tool_call_name": "hyper_system_run_shell_command", - "content_status": "dataLoadComplete", - "tool_call_id": "hyper_system_run_shell_command:0", - "content_date": 1753173412172, - "content_attached": true, - "reasoning_content": "", - "content_tool_calls": [] - } - ], - "index": 2 - } -] \ No newline at end of file diff --git a/packages/core/package.json b/packages/core/package.json index 88da7432..62d921ee 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@dadigua/hyperchat", - "version": "2.0.0-alpha.56", + "version": "2.0.0-alpha.57", "description": "HyperChat Core - Node.js backend and CLI tool with AI chat, MCP support", "author": "Dadigua <0laopo0@gmail.com>", "license": "MIT", diff --git a/packages/core/tsconfig.json b/packages/core/tsconfig.json index 951936dd..90328a89 100644 --- a/packages/core/tsconfig.json +++ b/packages/core/tsconfig.json @@ -10,9 +10,6 @@ "./types" ], "paths": { - "@dadigua/hyperchat-shared/*": [ - "../shared/src/*" - ], "@dadigua/hyperchat-shared": [ "../shared/src/index.ts" ] @@ -24,7 +21,7 @@ "include": [ "src", "types", - "**/*.json", + "package.json", "../shared/src", "../shared/src/**/*.json", ], diff --git a/packages/electron/package.json b/packages/electron/package.json index da0e868a..85ed42e0 100644 --- a/packages/electron/package.json +++ b/packages/electron/package.json @@ -112,7 +112,7 @@ ] } }, - "version": "2.0.0-alpha.56", + "version": "2.0.0-alpha.57", "description": "HyperChat Electron main process and related functionality", "main": "dist/electron/src/main.mjs", "type": "module", diff --git a/packages/shared/package.json b/packages/shared/package.json index 9e0258aa..aa116397 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -1,6 +1,6 @@ { "name": "@dadigua/hyperchat-shared", - "version": "2.0.0-alpha.56", + "version": "2.0.0-alpha.57", "description": "HyperChat Shared - Common types, utilities and schemas", "type": "module", "main": "dist/index.js", diff --git a/packages/web/package.json b/packages/web/package.json index fd2450cd..9c52f2c3 100644 --- a/packages/web/package.json +++ b/packages/web/package.json @@ -1,6 +1,6 @@ { "name": "@dadigua/hyperchat-web", - "version": "2.0.0-alpha.56", + "version": "2.0.0-alpha.57", "private": true, "dependencies": { "@ant-design/cssinjs": "^1.24.0", diff --git a/packages/web/tsconfig.json b/packages/web/tsconfig.json index ce08c349..bfde0d97 100644 --- a/packages/web/tsconfig.json +++ b/packages/web/tsconfig.json @@ -11,9 +11,6 @@ "./types" ], "paths": { - "@dadigua/hyperchat-shared/*": [ - "../shared/src/*" - ], "@dadigua/hyperchat-shared": [ "../shared/src/index.ts" ],