Files
vibemake/scripts/build.mjs
T
Jerry 1c837766cd feat: integrate robot assembly and simulation with resilient loading
Add backend-driven assembly and simulation experiences, MAKE branding, mobile rendering budgets and desktop-only CAD/PCB routes. Improve logo loading progress, document image recovery and URDF request handling; include dev packaging and regression coverage.
2026-10-30 18:38:39 +08:00

101 lines
6.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(静态网站,直接放到网站根目录)。
// 网站没有编译步骤,这里只挑出上线需要的文件,每个页面放进同名目录。
// 用法: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';
const ROOT = resolve(fileURLToPath(new URL('..', import.meta.url)));
const DIST = join(ROOT, 'dist');
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 SKIP = /(^|\/)(\.DS_Store|Thumbs\.db)$/;
const copy = (rel) => cpSync(join(ROOT, rel), join(DIST, rel), { recursive: true, filter: (src) => !SKIP.test(src) });
rmSync(DIST, { recursive: true, force: true });
mkdirSync(DIST);
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);
// 静态网站:每个页面放进同名目录的 index.html,地址是 /explore/、/studio/、/cad/、/pcb/、/project/?id=…、/workspace/?code=…
for (const page of ['explore', 'project', 'studio', 'cad', 'pcb', 'workspace']) {
mkdirSync(join(DIST, page));
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,
builtAt: new Date().toISOString(),
...(BUILD_ENV === 'dev' ? { frontend: 'https://dev-vibemake.robotquan.com', apiBase: 'https://dev-vibemake-api.robotquan.com/api' } : {}),
}, null, 2) + '\n');
writeFileSync(join(DIST, 'DEPLOY.md'), `# ${BUILD_ENV === 'dev' ? 'DEV 环境部署' : '部署'}
${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=<编号>\`
- 页面嵌入:/cad/ 加载 https://cadworld-studio.robotquan.com/cad,/pcb/ 加载 https://cadworld-studio.robotquan.com/pcb;仅电脑端开放,手机 / 平板或 820px 及以下窄屏访问时回到首页,且不加载编辑器。
- 接口:${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。
`);
// 样式表和导航脚本加版本号,部署新包后浏览器一定拿到新文件(不会用到旧缓存)
const BUILD_ID = Date.now().toString(36);
for (const f of ['index.html', 'explore/index.html', 'project/index.html', 'studio/index.html', 'cad/index.html', 'pcb/index.html', 'workspace/index.html']) {
const file = join(DIST, f);
writeFileSync(file, readFileSync(file, 'utf8')
.replace(/href="css\/(style|fonts)\.css"/g, `href="css/$1.css?v=${BUILD_ID}"`)
.replace(/src="(\/?js\/(?:shell|desktop-tools)\.js)"/g, `src="$1?v=${BUILD_ID}"`));
}
// 检查:不能有 .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, BUILD_ENV === 'dev' ? 'dist-dev.zip' : 'dist.zip');
rmSync(zip, { force: true });
try {
execFileSync('zip', ['-qr', zip, '.'], { cwd: DIST });
console.log(`已生成 ${zip}`);
} catch {
console.log('没有找到 zip 命令,只生成了 dist 目录');
}
console.log(`dist:${DIST}`);
console.log(`模型:${dirs.join('、')}`);
if (!existsSync(join(DIST, 'robots/so101/so101.urdf'))) throw new Error('模型文件没有复制成功');