二. Issues
1. Vitepress热更新崩溃
当频繁更新文件,或者文档内容非常大(例如大量的 Markdown 文件、复杂的代码块或嵌入的内容),VitePress 在构建或热更新(HMR, Hot Module Replacement)时可能会消耗大量内存,此时Vitepress容易发生崩溃,崩溃信息如下所示:
text
FATAL ERROR: Reached heap limit Allocation failed - JavaScript heap out of memory
1: 00007FF6E3E0C1FF node::SetCppgcReference+13695
2: 00007FF6E3D84E16 DSA_meth_get_flags+75686
3: 00007FF6E3D86C51 DSA_meth_get_flags+83425
4: 00007FF6E48455D1 v8::Isolate::ReportExternalAllocationLimitReached+65
5: 00007FF6E482EFC8 v8::Function::Experimental_IsNopFunction+1192
6: 00007FF6E467FAB0 v8::StackTrace::GetFrameCount+426608
7: 00007FF6E467CD95 v8::StackTrace::GetFrameCount+415061
8: 00007FF6E4692194 v8::StackTrace::GetFrameCount+502100
9: 00007FF6E4692A58 v8::StackTrace::GetFrameCount+504344
10: 00007FF6E46A2BB9 v8::StackTrace::GetFrameCount+570233
11: 00007FF6E437042B v8::CodeEvent::GetFunctionName+112299
12: 00007FF6848A667A
error Command failed with exit code 134.
这里我并没有尝试解决该问题,而是采用了一个规避的方式,即在发生崩溃的时候让程序自动重启(省得我自己去执行yarn docs:dev)
1. 安装nodemon
shell
yarn add --dev nodemon
2. 编写自启动脚本
在根目录下编写脚本start-vitepress.js:
js
import { exec } from 'child_process';
function startVitePress() {
console.log('VitePress start...');
const process = exec('yarn docs:dev', (error, stdout, stderr) => {
if (error) {
console.error(error);
console.error(stderr);
startVitePress();
} else {
console.log(stdout);
}
});
process.on('close', (code) => {
console.log('VitePress process quit, code:', code);
if (code !== 0) {
console.log('VitePress quit unexpectedly, restart vitepress...');
startVitePress();
}
});
process.stdout.on('data', (data) => {
console.log(data.toString());
});
process.stderr.on('data', (data) => {
console.error(data.toString());
});
}
startVitePress();
3. 配置nodemon
在根目录下常见modemon.json:
json
{
"exec": "node start-vitepress.js",
"ext": "js"
}
4. 启动nodemon
在package.json中增加nodemon启动的描述:
json
"scripts": {
"docs:dev": "vitepress dev docs",
"docs:dev:watch": "nodemon --config nodemon.json",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs"
},
启动程序:
shell
yarn docs:dev:watch