135 lines
9.5 KiB
JavaScript
135 lines
9.5 KiB
JavaScript
// 线上输出 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 { readCadEngine, copyCadEngine } from './cad-engine.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 CAD_ENGINE = existsSync(join(ROOT, 'cad/engine.json')) ? readCadEngine(join(ROOT, 'cad')) : null;
|
||
const PCB_DIST = resolve(process.env.PCB_DIST || join(ROOT, '../ziro-designer-main/designer/dist'));
|
||
if (!existsSync(join(PCB_DIST, 'index.html')))
|
||
throw new Error(`PCB build not found at ${PCB_DIST}. Build ziro-designer-main/designer first, or set PCB_DIST.`);
|
||
const SKIP = /(^|\/)(\.DS_Store|Thumbs\.db)$/;
|
||
const copy = (rel) => cpSync(join(ROOT, rel), join(DIST, rel), { recursive: true, filter: (src) => !SKIP.test(src) });
|
||
|
||
// Rebuild the site package and include the CAD engine installed in the project.
|
||
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);
|
||
// 静态网站:每个页面放进同名目录的 index.html,地址是 /explore/、/studio/、/cad/、/pcb/、/project/?id=…、/workspace/?code=…
|
||
for (const page of ['explore', 'project', 'studio', 'cad', 'workspace']) {
|
||
mkdirSync(join(DIST, page), { recursive: true });
|
||
cpSync(join(ROOT, `${page}.html`), join(DIST, page, 'index.html'));
|
||
}
|
||
cpSync(PCB_DIST, join(DIST, 'pcb'), { recursive: true });
|
||
|
||
cpSync(join(ROOT, 'cad/editor'), join(DIST, 'cad/editor'), { recursive: true });
|
||
cpSync(join(ROOT, 'cad/manifest.webmanifest'), join(DIST, 'cad/manifest.webmanifest'));
|
||
cpSync(join(ROOT, 'cad/worker.js'), join(DIST, 'cad/worker.js'));
|
||
if (CAD_ENGINE) copyCadEngine(CAD_ENGINE, DIST);
|
||
|
||
// 只带 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,
|
||
}, null, 2) + '\n');
|
||
|
||
writeFileSync(join(DIST, 'DEPLOY.md'), `# ${TARGET_NAME}部署
|
||
|
||
- 上传文件:${ARCHIVE_NAME}
|
||
- 目标网站:${FRONTEND}
|
||
- 构建版本:${BUILD_ID}
|
||
- 本地目录:${OUTPUT_NAME}/(两种环境分别输出,互不覆盖)
|
||
|
||
手动发布时先核对面板中的服务器、网站域名和网站根目录。建议先解压到本网站的新版本目录,确认完整后再切换网站目录,避免访问到解压过程中新旧混合的文件;保留上一版本以便回退,不覆盖服务器配置。
|
||
发布后打开 ${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/,由独立 PCB 仓库构建;项目列表和工程深链接由 PCB 单页应用处理。站点包将 PCB_DIST 指定的独立构建复制到 dist/pcb,无需合并两份源码。
|
||
- CAD:/cad/ 为同源项目列表,/cad/editor/?project=<UUID> 直接加载 Rust/WASM;CAD / PCB 仅电脑端开放。
|
||
- CAD engine: ${CAD_ENGINE ? `included from cad/ (build ${CAD_ENGINE.manifest.buildId}).` : 'not installed in this build; extract the SolidCraft Web package into the website root before using the CAD editor.'}
|
||
- 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', 'cad/index.html', 'cad/editor/index.html', 'pcb/index.html', 'workspace/index.html']);
|
||
|
||
// 检查:不能有 .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(`PCB:${join(DIST, 'pcb')} ← ${PCB_DIST}`);
|
||
console.log(CAD_ENGINE ? `CAD: ${join(DIST, 'cad')} (build ${CAD_ENGINE.manifest.buildId})` : 'CAD: no local engine installed; deploy the SolidCraft Web package separately.');
|
||
console.log(`模型:${dirs.join('、')}`);
|
||
if (!existsSync(join(DIST, 'robots/so101/so101.urdf'))) throw new Error('模型文件没有复制成功');
|