Files
vibemake/scripts/build.mjs
T
2026-11-09 18:43:21 +08:00

136 lines
9.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 线上输出 dist/ + dist.zip;开发输出 dist-dev/ + dist-dev.zip,互不覆盖。
// 网站没有编译步骤,这里只挑出上线需要的文件,每个页面放进同名目录。
// 用法:node scripts/build.mjs [--env=dev]
import { cpSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync, existsSync } from 'node:fs';
import { join, resolve } from 'node:path';
import { execFileSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { compressRobot } from './meshes.mjs';
import { versionSiteAssets } from './lib/version-site-assets.mjs';
const ROOT = resolve(fileURLToPath(new URL('..', import.meta.url)));
const BUILD_ID = Date.now().toString(36);
const BUILD_ENV = process.argv.find((arg) => arg.startsWith('--env='))?.slice(6) || 'auto';
if (!['auto', 'dev'].includes(BUILD_ENV)) throw new Error(`不支持的打包环境:${BUILD_ENV}`);
const IS_DEV = BUILD_ENV === 'dev';
const OUTPUT_NAME = IS_DEV ? 'dist-dev' : 'dist';
const DIST = join(ROOT, OUTPUT_NAME);
const ARCHIVE_NAME = `${OUTPUT_NAME}.zip`;
const TARGET_NAME = IS_DEV ? 'DEV 开发环境' : '线上正式环境';
const FRONTEND = IS_DEV ? 'https://dev-vibemake.robotquan.com' : 'https://vibemake.robotquan.com';
const API_BASE = IS_DEV ? 'https://dev-vibemake-api.robotquan.com/api' : 'https://vibemake-api.robotquan.com/api';
const SKIP = /(^|\/)(\.DS_Store|Thumbs\.db)$/;
const INDEPENDENT_PATHS = ['cad', 'pcb', 'js/cad', 'css/cad.css'];
const copy = (rel) => cpSync(join(ROOT, rel), join(DIST, rel), {
recursive: true,
filter: (src) => !SKIP.test(src) && !INDEPENDENT_PATHS.some((path) => src === join(ROOT, path)),
});
// CAD and PCB are independently published and never belong to the site package.
mkdirSync(DIST, { recursive: true });
for (const name of readdirSync(DIST)) rmSync(join(DIST, name), { recursive: true, force: true });
for (const f of ['index.html', 'css', 'js', 'assets', 'data', 'vendor', 'play']) copy(f);
const apiFile = join(DIST, 'js/api.js');
const apiSource = readFileSync(apiFile, 'utf8');
if (!apiSource.includes("export const BUILD_ENV = 'auto';")) throw new Error('未找到接口环境标记,停止打包');
writeFileSync(apiFile, apiSource.replace("export const BUILD_ENV = 'auto';", `export const BUILD_ENV = '${BUILD_ENV}';`));
// 浏览器标签、收藏夹和添加到手机桌面使用独立的简化机器人图标。
for (const f of ['favicon.svg', 'favicon.ico', 'apple-touch-icon.png', 'site.webmanifest', 'icon-16.png', 'icon-32.png', 'icon-48.png', 'icon-64.png', 'icon-192.png', 'icon-512.png']) copy(f);
// 静态网站:CAD / PCB 页面由各自的独立发布包提供。
for (const page of ['explore', 'project', 'studio', 'workspace']) {
mkdirSync(join(DIST, page), { recursive: true });
cpSync(join(ROOT, `${page}.html`), join(DIST, page, 'index.html'));
}
// 只带 robots.json 里用到的模型目录(demo-* 是早期示例,不上线)
mkdirSync(join(DIST, 'robots'));
copy('robots/robots.json');
const robots = JSON.parse(readFileSync(join(ROOT, 'robots/robots.json'), 'utf8'));
const dirs = [...new Set(robots.map((r) => r.urdf.split('/').slice(0, 2).join('/')))];
for (const d of dirs) copy(d);
// 模型网格:STL → Draco 压缩 GLB(首页、工作台加载快很多)
for (const r of robots) {
const t0 = Date.now();
const { files, before, after } = await compressRobot(join(DIST, r.urdf));
const mb = (n) => (n / 1048576).toFixed(1);
console.log(` ${r.id.padEnd(16)} ${String(files).padStart(3)} 个网格 ${mb(before)} MB → ${mb(after)} MB (${Date.now() - t0} ms)`);
}
writeFileSync(join(DIST, 'build-info.json'), JSON.stringify({
environment: BUILD_ENV,
deploymentTarget: IS_DEV ? 'dev' : 'production',
buildId: BUILD_ID,
builtAt: new Date().toISOString(),
frontend: FRONTEND,
apiBase: API_BASE,
archive: ARCHIVE_NAME,
scope: 'site',
excludedPaths: INDEPENDENT_PATHS,
}, null, 2) + '\n');
writeFileSync(join(DIST, 'DEPLOY.md'), `# ${TARGET_NAME}部署
- 上传文件:${ARCHIVE_NAME}
- 目标网站:${FRONTEND}
- 构建版本:${BUILD_ID}
- 本地目录:${OUTPUT_NAME}/(两种环境分别输出,互不覆盖)
本包只更新主站,不包含 cad/、pcb/、js/cad/ 和 css/cad.css,也不需要 CAD / PCB 源码或构建产物。
手动发布时先核对面板中的服务器、网站域名和网站根目录,再将包内文件合并覆盖到现有网站根目录。保留线上 cad/、pcb/、js/cad/ 和 css/cad.css;不要先清空网站,不要使用不带排除规则的 rsync --delete。
如需先解压到新版本目录再切换网站目录,必须先将上述独立模块从当前线上目录完整保留到新目录,或使用指向独立模块目录的固定软链接;主站 ZIP 本身不能替代整个网站目录。保留上一版本和服务器配置以便回退。
发布后打开 ${FRONTEND}/build-info.json?check=${BUILD_ID},核对 buildId 为 ${BUILD_ID},deploymentTarget 为 ${IS_DEV ? 'dev' : 'production'}。若不同,检查解压位置、网站根目录及缓存配置。
${BUILD_ENV === 'dev' ? `本包仅用于 dev 环境,接口固定为 https://dev-vibemake-api.robotquan.com/api,始终读取接口数据。
推荐域名:https://dev-vibemake.robotquan.com。
1. 将 dist-dev.zip 上传到 dev 静态网站根目录。
2. 将压缩包内的文件直接解压到网站根目录;根目录应能看到 index.html、js/、css/、play/,不要再套一层 dist 目录。
3. 保留网站原有的 nginx、HTTPS、CORS 等配置,解压时覆盖同名文件。
4. 打开网站并强制刷新,检查社区详情、AI 装配和行走演示;手机端 CAD / PCB 应跳转首页。
` : ''}静态网站,1Panel 新建「静态网站」,把本目录所有文件放到网站根目录即可,不需要运行 npm 或 Node 服务。
- 页面地址:\`/\`、\`/explore/\`、\`/studio/\`、\`/cad/\`、\`/pcb/\`、\`/project/?id=<编号>\`、\`/workspace/\`、\`/workspace/?code=<编号>\`
- PCB:在 PCB 仓库执行 ./build.sh,生成 dist/vibemake-pcb.zip 和 dist/vibemake-pcb/。目录与压缩包根目录直接包含 index.html、assets/ 等文件,不带额外一层 pcb/;将包内文件直接解压到网站根目录下的 pcb/ 目录,不随主站发布。
- CAD:在 SolidCraft 仓库执行 ./build.sh,将完整发布包 dist/vibemake-cad.zip 解压到网站根目录下的 cad/ 目录;包内直接是 index.html、editor/、js/、css/、vendor/ 和当次构建的引擎,不再套一层 cad/。dist/vibemake-cad-engine.zip 只有引擎,没有 HTML,仅用于已部署 CAD 页面的引擎更新,该引擎包自带 cad/,需合并解压到网站根目录。主站 build / build:dev 不发布这些文件。
- CAD / PCB 仅电脑端开放;更新主站后继续使用线上已发布的模块版本。
- Nginx 等静态服务器需让 PCB 无扩展名深链接回退到 /pcb/index.html,例如:location ^~ /pcb/ { try_files $uri $uri/ /pcb/index.html; }。
- 接口:${BUILD_ENV === 'dev' ? '固定连接 https://dev-vibemake-api.robotquan.com/api。' : '浏览器按访问的域名直接请求后端(js/api.js 的 API_HOSTS):dev-vibemake.robotquan.com → https://dev-vibemake-api.robotquan.com/api;vibemake.robotquan.com → https://vibemake-api.robotquan.com/api。'}
- **后端需要允许前端域名跨域**(CORS:允许来源 ${BUILD_ENV === 'dev' ? 'https://dev-vibemake.robotquan.com' : 'https://dev-vibemake.robotquan.com、https://vibemake.robotquan.com'};
方法 GET/POST/PUT/PATCH/DELETE/OPTIONS;请求头 Authorization、Content-Type、Accept;暴露 X-Request-Id)。
- 阿里云 OSS(Bucket linkerbot)跨域规则也要加上这两个域名,否则封面上传会失败。
- 第三方库(three.js、urdf-loader、marked、DOMPurify)和字体都在 vendor/ 里,不依赖任何外部 CDN。
- 发布时完整覆盖压缩包中的文件,尤其是 js/、css/ 和各页面 index.html;不要只替换入口 HTML。应用入口及其依赖使用统一构建版本号,见 build-info.json 的 buildId。
- HTML 建议设置 Cache-Control: no-cache,确保浏览器能获取新入口。首次修复旧缓存后强制刷新一次;若启用了 CDN 缓存 HTML,发布后同步刷新页面缓存。
`);
await versionSiteAssets(DIST, BUILD_ID, ['index.html', 'explore/index.html', 'project/index.html', 'studio/index.html', 'workspace/index.html'], {
externalDirectories: ['js/cad'], externalUrls: { 'js/cad': '/cad/js/cad' },
});
for (const path of INDEPENDENT_PATHS) {
if (existsSync(join(DIST, path))) throw new Error(`主站包不得包含独立模块:${path}`);
}
// 检查:不能有 .mjs(静态服务器常把它当成二进制文件返回,模块脚本加载失败)
const walk = (d) => readdirSync(d, { withFileTypes: true }).flatMap((e) => (e.isDirectory() ? walk(join(d, e.name)) : [join(d, e.name)]));
const mjs = walk(DIST).filter((f) => f.endsWith('.mjs'));
if (mjs.length) throw new Error(`dist 里有 .mjs 文件,请改成 .js:${mjs.join(', ')}`);
// 打 zip(macOS / Linux 自带 zip)
const zip = join(ROOT, ARCHIVE_NAME);
rmSync(zip, { force: true });
try {
execFileSync('zip', ['-qr', zip, '.'], { cwd: DIST });
console.log(`已生成 ${zip}`);
} catch (error) {
if (error.code !== 'ENOENT') throw error;
console.log(`没有找到 zip 命令,只生成了 ${OUTPUT_NAME} 目录`);
}
console.log(`部署目标:${TARGET_NAME} · ${FRONTEND} · 版本 ${BUILD_ID}`);
console.log(`dist:${DIST}`);
console.log('CAD / PCB:独立发布,主站包不包含模块页面、引擎和专用 JS/CSS。');
console.log(`模型:${dirs.join('、')}`);
if (!existsSync(join(DIST, 'robots/so101/so101.urdf'))) throw new Error('模型文件没有复制成功');