Files
CloudSSH/scripts/build-html.js
T
TanXin e5fcddce7f release: v1.4.0 — 升级 Theme V2 与在线主题编辑体验 (#52)
升级 Theme V2 界面风格系统、GitHub Pages 在线主题编辑器与登录账号主题同步,并补齐主题数据安全校验、首屏性能优化和相关回归测试。

完整变更请参阅 CHANGELOG.md。
2026-07-30 15:11:09 +08:00

76 lines
3.2 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const { execFileSync } = require('child_process');
const rootDir = path.resolve(__dirname, '..');
const frontendDir = path.join(rootDir, 'frontend');
const distDir = path.join(frontendDir, 'dist');
const workerDir = path.join(rootDir, 'src', 'worker');
const htmlTsFile = path.join(workerDir, 'html.ts');
console.log('Building frontend...');
try {
execFileSync(process.execPath, ['scripts/sync-theme-editor.js'], { cwd: rootDir, stdio: 'inherit' });
// Dependencies are installed explicitly by developers/CI. Keeping installs out
// of the build makes production artifacts deterministic and offline-buildable.
execFileSync('pnpm', ['run', 'build'], { cwd: frontendDir, stdio: 'inherit' });
console.log('Inlining assets...');
// 2. Read dist/index.html
let html = fs.readFileSync(path.join(distDir, 'index.html'), 'utf8');
// 3. Find assets in dist/assets
const assetsDir = path.join(distDir, 'assets');
const files = fs.readdirSync(assetsDir).sort();
const jsFiles = files.filter((file) => file.endsWith('.js'));
const cssFiles = files.filter((file) => file.endsWith('.css'));
if (jsFiles.length !== 1 || cssFiles.length !== 1) {
throw new Error(
`Expected exactly one JS and one CSS bundle for Worker inlining, got ${jsFiles.length} JS and ${cssFiles.length} CSS.`
);
}
const jsContent = fs.readFileSync(path.join(assetsDir, jsFiles[0]), 'utf8');
const cssContent = fs.readFileSync(path.join(assetsDir, cssFiles[0]), 'utf8');
// 4. Inline favicon.svg as base64 data URL
const faviconPath = path.join(frontendDir, 'public', 'favicon.svg');
if (fs.existsSync(faviconPath)) {
const faviconContent = fs.readFileSync(faviconPath, 'utf8');
const faviconBase64 = Buffer.from(faviconContent).toString('base64');
html = html.replace(
/href=["']\/favicon\.svg["']/i,
`href="data:image/svg+xml;base64,${faviconBase64}"`
);
}
// 5. Inline CSS (replace <link rel="stylesheet" ...>)
html = html.replace(/<link[^>]*rel=["']stylesheet["'][^>]*href=["']\/assets\/[^"']+["'][^>]*>/i, () => `<style>${cssContent}</style>`);
html = html.replace(/<link[^>]*rel=["']stylesheet["'][^>]*href=["']\/src\/[^"']+["'][^>]*>/i, () => `<style>${cssContent}</style>`);
// 6. Inline JS (replace <script type="module" src="/src/main.ts"></script> or similar)
html = html.replace(/<script[^>]*type=["']module["'][^>]*src=["']\/assets\/[^"']+["'][^>]*><\/script>/i, () => `<script type="module">${jsContent}</script>`);
html = html.replace(/<script[^>]*type=["']module["'][^>]*src=["']\/src\/[^"']+["'][^>]*><\/script>/i, () => `<script type="module">${jsContent}</script>`);
// 7. Write to src/worker/html.ts
const escapedHtml = html
.replace(/\\/g, '\\\\')
.replace(/`/g, '\\`')
.replace(/\${/g, '\\${');
const htmlTsContent = `// Automatically generated by scripts/build-html.js. Do not edit directly.
export const HTML = \`${escapedHtml}\`;
`;
if (!fs.existsSync(workerDir)) {
fs.mkdirSync(workerDir, { recursive: true });
}
fs.writeFileSync(htmlTsFile, htmlTsContent, 'utf8');
console.log('Frontend built and inlined successfully to src/worker/html.ts!');
} catch (error) {
console.error('Failed to build and inline frontend:', error);
process.exit(1);
}