const fs = require('fs');
const path = require('path');

const projectRoot = path.resolve(__dirname, '..');
const srcIndex = path.join(projectRoot, 'index.html');
const distDir = path.join(projectRoot, 'dist');
const distIndex = path.join(distDir, 'index.html');

if (!fs.existsSync(distDir)) {
    fs.mkdirSync(distDir, { recursive: true });
}

let html = fs.readFileSync(srcIndex, 'utf8');

// 替换开发路径为构建产物路径
html = html.replace(/['"]\/src\/main\.ts['"]/g, '"/assets/index.js"');

fs.writeFileSync(distIndex, html, 'utf8');

console.log('postbuild: index.html written to dist/');
