本系列教程共十篇
覆盖文件
覆盖文件的意思就是,这个文件已存在,删除原本全部内容,替换为下方内容,不过删之前建议备份哦
覆盖scripts\build-ssg.mjs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| import { spawn } from 'node:child_process' import { createRequire } from 'node:module' import { existsSync } from 'node:fs' import { resolve } from 'node:path'
const BUILD_DONE_MARKERS = [ '[HOOK] build:after done', 'RSS Feed Files', ] const GRACE_MS = 800 const HEARTBEAT_MS = 45_000 const MAX_MS = 20 * 60 * 1000
const require = createRequire(import.meta.url) const valaxyBin = require.resolve('valaxy/bin/valaxy.mjs') const distIndex = resolve(process.cwd(), 'dist/index.html')
const child = spawn(process.execPath, [valaxyBin, 'build', '--ssg'], { stdio: ['inherit', 'pipe', 'pipe'], env: process.env, windowsHide: true, })
let finished = false let successTimer let outputBuffer = ''
function killChildTree() { if (!child.pid) return
if (process.platform === 'win32') { spawn('taskkill', ['/PID', String(child.pid), '/T', '/F'], { stdio: 'ignore', windowsHide: true, }) return }
try { child.kill('SIGKILL') } catch {} }
function finishSuccess() { if (finished) return finished = true clearTimeout(maxTimer) clearTimeout(successTimer) clearInterval(heartbeatTimer)
if (!existsSync(distIndex)) { console.error('\n构建流程已结束,但未找到 dist/index.html,请检查构建日志。\n') killChildTree() process.exit(1) return }
console.log('\n✓ 构建已完成,正在结束进程…\n') killChildTree() setTimeout(() => process.exit(0), 200) }
function finishError(code) { if (finished) return finished = true clearTimeout(maxTimer) clearTimeout(successTimer) clearInterval(heartbeatTimer) process.exit(code ?? 1) }
function scheduleSuccess() { clearTimeout(successTimer) successTimer = setTimeout(finishSuccess, GRACE_MS) }
function inspectOutput(text) { outputBuffer += text if (outputBuffer.length > 8192) outputBuffer = outputBuffer.slice(-8192)
if (BUILD_DONE_MARKERS.some(marker => outputBuffer.includes(marker))) scheduleSuccess() }
const heartbeatTimer = setInterval(() => { if (finished) return console.log('\n⏳ 仍在构建中(SSG 预渲染可能较慢,请稍候)…\n') }, HEARTBEAT_MS)
const maxTimer = setTimeout(() => { if (finished) return console.error('\nSSG 构建超时(20 分钟)。\n') killChildTree() finishError(1) }, MAX_MS)
child.stdout.on('data', (chunk) => { const text = chunk.toString() process.stdout.write(text) inspectOutput(text) })
child.stderr.on('data', (chunk) => { const text = chunk.toString() process.stderr.write(text) inspectOutput(text) })
child.on('error', (error) => { console.error(error) finishError(1) })
child.on('close', (code) => { if (finished) return if (code === 0 || existsSync(distIndex)) finishSuccess() else finishError(code) })
|
修改文件
修改package.json
替换原本"build:ssg": "valaxy build --ssg",为
1 2
| "build:ssg": "node scripts/build-ssg.mjs", "build:ssg:raw": "valaxy build --ssg",
|
替换原本"build": "npm run build:ssg",为
1
| "build": "node scripts/build-ssg.mjs",
|
好啦到此修复完成啦,综上全部的美化教程就到此结束啦,感谢观看哦~