Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
monorepo
/
venus
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
1
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit ec59299f
authored
Jan 16, 2026
by
Lin
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat: 优化项目构建流程/依赖预更新
1 parent
dfb3df3b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
165 additions
and
107 deletions
packages/common-ui/src/components/button/index.ts
scripts/build/buildToRoot.mjs
packages/common-ui/src/components/button/index.ts
View file @
ec59299
export
{
default
,
default
as
VButton
}
from
"./index.vue"
;
\ No newline at end of file
export
{
default
,
default
as
VButton
}
from
"./index.vue"
;
scripts/build/buildToRoot.mjs
View file @
ec59299
...
...
@@ -2,156 +2,221 @@
* 自定义构建脚本
* 功能:
* 1. 清理根目录的 dist 文件夹
* 2. 构建指定的项目
* 3. 将构建产物复制到根目录的 dist 文件夹
* 2. 安装指定项目的依赖(智能检测)
* 3. 构建指定的项目
* 4. 将构建产物复制到根目录的 dist 文件夹
*/
import { execSync } from "node:child_process";
import { existsSync, mkdirSync, rmSync } from "node:fs";
import { copyFile, readdir } from "node:fs/promises";
import { existsSync, mkdirSync, rmSync
, statSync
} from "node:fs";
import { copyFile, readdir
, readFile
} from "node:fs/promises";
import { join } from "node:path";
//
获取项目根目录
//
常量定义
const rootDir = process.cwd();
const rootDistDir = join(rootDir, "dist");
const PACKAGE_SEARCH_PATHS = [
["apps", "web", "venus-agentic"],
["apps", "web"],
["apps", "frontend-micro"],
["apps", "frontend", "vue"],
["apps"],
["packages"],
["docs"],
];
// ==================== 构建配置开关 ====================
/**
* 是否在构建前安装依赖
* - true: 启用依赖安装(默认,智能检测)
* - false: 跳过依赖安装步骤
*/
const ENABLE_INSTALL_DEPS = true;
/**
* 是否强制安装依赖(仅在 ENABLE_INSTALL_DEPS 为 true 时生效)
* - true: 强制重新安装依赖,跳过智能检测
* - false: 智能检测依赖是否需要安装(默认)
*/
const FORCE_INSTALL_DEPS = false;
// ====================================================
// 公共执行配置
const execConfig = {
stdio: "inherit",
cwd: rootDir,
shell: true,
env: {
...process.env,
NODE_OPTIONS: "--max-old-space-size=8192",
},
};
/**
* 执行命令的统一封装
*/
function execCommand(command, errorMsg) {
try {
execSync(command, execConfig);
} catch (error) {
console.error(errorMsg);
console.error(`错误详情: ${error?.message || String(error)}`);
process.exit(1);
}
}
/**
* 清理根目录的 dist 文件夹
*/
function cleanRootDist() {
console.log("
🧹
清理根目录的 dist 文件夹...");
console.log("清理根目录的 dist 文件夹...");
if (existsSync(rootDistDir)) {
rmSync(rootDistDir, { recursive: true, force: true });
console.log("
✅
根目录 dist 文件夹已清理");
console.log("根目录 dist 文件夹已清理");
} else {
console.log("
ℹ️
根目录 dist 文件夹不存在,跳过清理");
console.log("根目录 dist 文件夹不存在,跳过清理");
}
}
/**
* 构建指定的项目
* @param {string} packageName - 包名或过滤器
* 查找包的路径
*/
function buildPackage(packageName) {
console.log(`\n📦 开始构建项目: ${packageName}`);
async function findPackagePath(packageName) {
const cleanName = packageName.replace("@venus/", "");
const nameVariants = [packageName, `@venus/${cleanName}`, cleanName];
for (const pathSegments of PACKAGE_SEARCH_PATHS) {
const packagePath = join(rootDir, ...pathSegments, cleanName);
if (!existsSync(packagePath)) continue;
const packageJsonPath = join(packagePath, "package.json");
if (!existsSync(packageJsonPath)) continue;
try {
const content = await readFile(packageJsonPath, "utf8");
const packageJson = JSON.parse(content);
if (nameVariants.includes(packageJson.name)) {
return packagePath;
}
} catch {
// 忽略解析错误,继续查找
}
}
return null;
}
/**
* 检查是否需要安装依赖
*/
async function shouldInstallDeps(packageName) {
const packagePath = await findPackagePath(packageName);
if (!packagePath) return true;
const nodeModulesPath = join(packagePath, "node_modules");
if (!existsSync(nodeModulesPath)) return true;
const packageJsonPath = join(packagePath, "package.json");
const pnpmLockPath = join(rootDir, "pnpm-lock.yaml");
if (!existsSync(packageJsonPath) || !existsSync(pnpmLockPath)) return true;
try {
execSync(`pnpm --filter=${packageName} run build`, {
stdio: "inherit",
cwd: rootDir,
env: {
...process.env,
NODE_OPTIONS: "--max-old-space-size=8192",
},
});
console.log(`✅ 项目构建完成: ${packageName}`);
const [packageJsonTime, lockTime, nodeModulesTime] = [
statSync(packageJsonPath).mtime,
statSync(pnpmLockPath).mtime,
statSync(nodeModulesPath).mtime,
];
return packageJsonTime > nodeModulesTime || lockTime > nodeModulesTime;
} catch {
console.error(`❌ 项目构建失败: ${packageName}`);
process.exit(1);
return true;
}
}
/**
* 安装指定项目的依赖
*/
async function installDeps(packageName, force = false) {
if (!force) {
console.log(`\n📥 检查依赖状态: ${packageName}`);
if (!(await shouldInstallDeps(packageName))) {
console.log("✅ 依赖已是最新,跳过安装");
return;
}
}
console.log(`\n📦 开始安装依赖: ${packageName}`);
execCommand(
`pnpm install --filter="${packageName}" --prefer-offline`,
`❌ 依赖安装失败: ${packageName}`,
);
console.log(`✅ 依赖安装完成: ${packageName}`);
}
/**
* 构建指定的项目
*/
function buildPackage(packageName) {
console.log(`\n📦 开始构建项目: ${packageName}`);
execCommand(
`pnpm --filter="${packageName}" run build`,
`❌ 项目构建失败: ${packageName}`,
);
console.log(`✅ 项目构建完成: ${packageName}`);
}
/**
* 递归复制目录
* @param {string} src - 源目录
* @param {string} dest - 目标目录
*/
async function copyDirectory(src, dest) {
// 确保目标目录存在
if (!existsSync(dest)) {
mkdirSync(dest, { recursive: true });
}
const entries = await readdir(src, { withFileTypes: true });
for (const entry of entries) {
for (const entry of await readdir(src, { withFileTypes: true })) {
const srcPath = join(src, entry.name);
const destPath = join(dest, entry.name);
await (entry.isDirectory() ? copyDirectory(srcPath, destPath) : copyFile(srcPath, destPath));
}
}
/**
* 查找包的路径
* @param {string} packageName - 包名
* @returns {string|null} 包的路径
*/
async function findPackagePath(packageName) {
// 移除 @venus/ 前缀(如果有)
const cleanName = packageName.replace("@venus/", "");
// 可能的路径
const possiblePaths = [
join(rootDir, "apps", "frontend-micro", cleanName),
join(rootDir, "apps", "frontend", "vue", cleanName),
join(rootDir, "apps", cleanName),
join(rootDir, "packages", cleanName),
join(rootDir, "docs"),
];
// 尝试查找包
for (const path of possiblePaths) {
if (existsSync(path)) {
const packageJsonPath = join(path, "package.json");
if (existsSync(packageJsonPath)) {
try {
const packageJson = JSON.parse(
await import("node:fs").then((fs) => fs.promises.readFile(packageJsonPath, "utf8")),
);
// 检查包名是否匹配
if (
packageJson.name === packageName ||
packageJson.name === `@venus/${cleanName}` ||
packageJson.name === cleanName
) {
return path;
}
} catch {
// 忽略 JSON 解析错误
}
}
}
}
return null;
}
/**
* 复制构建产物到根目录
* @param {string} packageName - 包名
*/
async function copyBuildToRoot(packageName) {
console.log(`\n📂 查找构建产物...`);
const packagePath = await findPackagePath(packageName);
if (!packagePath) {
console.error(`❌ 无法找到包: ${packageName}`);
process.exit(1);
}
console.log(`📍 包路径: ${packagePath}`);
const sourceDistDir = join(packagePath, "dist");
if (!existsSync(sourceDistDir)) {
console.error(`❌ 构建产物不存在: ${sourceDistDir}`);
process.exit(1);
}
console.log(`\n
📋
复制构建产物到根目录...`);
console.log(`\n复制构建产物到根目录...`);
console.log(` 源目录: ${sourceDistDir}`);
console.log(` 目标目录: ${rootDistDir}`);
try {
await copyDirectory(sourceDistDir, rootDistDir);
console.log("✅ 构建产物已复制到根目录的 dist 文件夹");
// 显示复制的文件统计
const files = await readdir(rootDistDir, { recursive: true });
console.log(`\n📊 共复制 ${files.length} 个文件`);
} catch (error) {
console.error(`❌ 复制构建产物失败: ${error.message}`);
console.error(`❌ 复制构建产物失败: ${error?.message || String(error)}`);
process.exit(1);
}
}
/**
* 验证根目录
*/
function validateRootDir() {
const workspaceFile = join(rootDir, "pnpm-workspace.yaml");
const rootPackageJson = join(rootDir, "package.json");
if (!existsSync(workspaceFile) && !existsSync(rootPackageJson)) {
console.error("❌ 错误:请在项目根目录执行此命令");
console.error(`当前目录: ${rootDir}`);
process.exit(1);
}
}
...
...
@@ -161,35 +226,32 @@ async function copyBuildToRoot(packageName) {
*/
async function main() {
const packageName = process.argv[2];
if (!packageName) {
console.error("❌ 请指定要构建的项目名");
console.log("\n使用方法:");
console.log(" pnpm build:filter <package-name>");
console.log("\n示例:");
console.log(" pnpm build:filter @venus/venus-base");
console.log(" pnpm build:filter venus-micro-app");
process.exit(1);
}
console.log("🚀 开始自定义构建流程");
console.log(`📦 目标项目: ${packageName}\n`);
validateRootDir();
console.log("开始自定义构建流程");
// 1. 清理根目录 dist
cleanRootDist();
// 2. 构建指定项目
buildPackage(packageName);
if (ENABLE_INSTALL_DEPS) {
await installDeps(packageName, FORCE_INSTALL_DEPS);
} else {
console.log("跳过依赖安装步骤\n");
}
// 3. 复制构建产物到根目录
buildPackage(packageName);
await copyBuildToRoot(packageName);
console.log("\n
✨
构建流程完成!");
console.log("\n
✅
构建流程完成!");
console.log(`📁 构建产物位置: ${rootDistDir}`);
}
// 执行主函数
main().catch((error) => {
console.error(`
❌
构建过程中出现错误: ${error.message}`);
console.error(`构建过程中出现错误: ${error.message}`);
process.exit(1);
});
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment