feat: add prebuild script for processing manifest and index files

This commit is contained in:
Astrian Zheng 2025-05-28 13:55:06 +10:00
parent c34574666e
commit 6ed684e567
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA
2 changed files with 66 additions and 3 deletions

View File

@ -5,12 +5,13 @@
"type": "module",
"scripts": {
"dev": "vite",
"build": "vue-tsc -b && vite build && cp -r public/* dist/",
"build": "npm run prebuild && vue-tsc -b && vite build && cp -r public/* dist/",
"build:watch": "vite build --watch",
"preview": "vite preview",
"lint": "biome format --write .",
"quality-check": "biome ci",
"qc": "npm run quality-check"
"qc": "npm run quality-check",
"prebuild": "node scripts/prebuild.js"
},
"dependencies": {
"@tailwindcss/vite": "^4.1.7",

62
scripts/prebuild.js Normal file
View File

@ -0,0 +1,62 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// 处理 manifest.json
function processManifest() {
const manifestPath = path.join(__dirname, '../public/manifest.json');
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8'));
// 移除本地调试相关的配置
if (manifest.host_permissions) {
manifest.host_permissions = manifest.host_permissions.filter(
permission => !permission.includes('localhost')
);
}
if (manifest.content_security_policy && manifest.content_security_policy.extension_pages) {
// 移除 CSP 中的本地开发相关配置
manifest.content_security_policy.extension_pages = manifest.content_security_policy.extension_pages
.replace(/script-src 'self' http:\/\/localhost:5173;\s*/g, '')
.replace(/style-src 'self' 'unsafe-inline';\s*/g, '')
.replace(/\s*http:\/\/localhost:5173\s*/g, ' ')
.replace(/\s*ws:\/\/localhost:5173\s*/g, ' ')
.replace(/;\s+/g, '; ') // 标准化分号后的空格
.replace(/\s+/g, ' ') // 合并多个空格为一个
.trim();
}
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
console.log('✅ Manifest.json processed');
}
// 处理 index.html
function processIndexHtml() {
const indexPath = path.join(__dirname, '../index.html');
let content = fs.readFileSync(indexPath, 'utf8');
// 替换脚本地址
content = content.replace(
/src="[^"]*\/src\/main\.ts"/g,
'src="./src/main.ts"'
);
// 移除 crossorigin 属性
content = content.replace(/\s+crossorigin/g, '');
fs.writeFileSync(indexPath, content);
console.log('✅ Index.html processed');
}
// 执行处理
try {
processManifest();
processIndexHtml();
console.log('🎉 Build preparation completed!');
} catch (error) {
console.error('❌ Error during build preparation:', error);
process.exit(1);
}