修复 pnpm dev 启动刷红:Sketch.astro 脚本误识别 + .pnpm-store 撑爆 inotify
两个独立问题叠在一起,用户看到的只是第一个(扫描失败会中止启动,
第二个来不及暴露)。
1) Sketch.astro 的 frontmatter 里出现字面量的 script 开标签
Astro 编译器靠扫描源码文本收集内联脚本,分不清真标签和字符串/注释。
原代码用模板字符串拼 iframe 里的 resize 脚本,那个字面量开标签被当成
组件脚本抽出来交给 Vite,里面的 ${JSON.stringify(...)} 直接报
`Expected "}" but found "{"`,dev 依赖扫描失败、整个 /src 刷红。
-> 改成 "\x3Cscript>" / "\x3C/script>" 拼接。
注意闭标签原本写作 <\/script> 所以没被误判,只有开标签要处理。
修完第一版又把解释写进注释、注释里带了字面量开标签,于是换了条错误
(`Unexpected "。"`)继续报;这次注释也改成不带字面量的写法。
2) .pnpm-store 被 Vite 的 watcher 一起监听
项目根目录下的 .pnpm-store 是 pnpm 内容寻址仓库(54269 个文件 / 672MB,
已被 .gitignore 忽略),Vite 默认 watch 项目根,直接撑爆 inotify
(max_user_watches = 65536),启动后无限刷
`ENOSPC: System limit for number of file watchers reached`。
-> astro.config.mjs 加 vite.server.watch.ignored: ["**/.pnpm-store/**"]。
顺带:package.json 加 "type": "module",消掉 Node 对
src/plugins/*.js 的 MODULE_TYPELESS_PACKAGE_JSON 警告(两个文件本来就是 ESM)。
验证
- 删掉 node_modules/.vite 强制重新做依赖扫描后启动 pnpm dev:
ERROR 0 / Failed to scan 0 / ENOSPC 0,ready in 496ms
/、/bookshelf/、/bookshelf/books/ 均 200
- pnpm build 退出码 0,MODULE_TYPELESS 警告 0
- 仅剩 [WARN] glob-loader 提示 src/content/studies 为空,属预期
- 操作指南补了这条 FAQ,说明两个原因和"注释里也不能写"这个坑
This commit is contained in:
@@ -137,4 +137,18 @@ export default defineConfig({
|
|||||||
],
|
],
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
vite: {
|
||||||
|
server: {
|
||||||
|
watch: {
|
||||||
|
/*
|
||||||
|
* 项目根目录下的 .pnpm-store 是 pnpm 的内容寻址仓库:5.4 万个文件、672 MB,
|
||||||
|
* 而且已经被 .gitignore 忽略。Vite 默认会把项目根整个 watch,加上它以后
|
||||||
|
* inotify 直接到顶(max_user_watches = 65536),dev server 一启动就刷屏:
|
||||||
|
* ENOSPC: System limit for number of file watchers reached
|
||||||
|
* 排除掉即可。node_modules / .git 是 Vite 默认就排除的,不用管。
|
||||||
|
*/
|
||||||
|
ignored: ["**/.pnpm-store/**"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -294,6 +294,24 @@ sudo sysctl fs.inotify.max_user_watches=524288
|
|||||||
|
|
||||||
如果连空态提示都看不到,检查 `src/data/reading.json` 是否存在(这个文件要提交进 git)。
|
如果连空态提示都看不到,检查 `src/data/reading.json` 是否存在(这个文件要提交进 git)。
|
||||||
|
|
||||||
|
**`pnpm dev` 启动时满屏刷红**
|
||||||
|
|
||||||
|
历史上踩过两个独立的原因,都已修好,这里记下来免得再犯:
|
||||||
|
|
||||||
|
1. **源码里出现字面量的 script 开标签**(`Sketch.astro` 的 frontmatter 里曾经有)。
|
||||||
|
Astro 编译器靠**扫描源码文本**收集内联脚本,它分不清「这是真的标签」还是
|
||||||
|
「在字符串/注释里」。一旦扫到,就会把后面的内容当成组件脚本交给 Vite 解析,
|
||||||
|
报 `Expected "}" but found "{"` 或 `Unexpected "。"`,整个 `/src` 刷红。
|
||||||
|
→ 用 `"\x3Cscript>"` 拼出来。**注释里也不能写**,第一版就是栽在注释上。
|
||||||
|
|
||||||
|
2. **`.pnpm-store` 被 Vite 的 watcher 一起监听**。它是 pnpm 的内容寻址仓库,
|
||||||
|
5.4 万个文件 / 672MB,直接把 inotify 撑爆(`max_user_watches = 65536`),
|
||||||
|
报 `ENOSPC: System limit for number of file watchers reached`。
|
||||||
|
→ 已在 `astro.config.mjs` 里排除:`vite.server.watch.ignored: ["**/.pnpm-store/**"]`。
|
||||||
|
|
||||||
|
看到 `[WARN] [glob-loader] No files found ... src/content/studies` 是**正常的** ——
|
||||||
|
`studies` 集合目前是空的,专题页会显示空态。
|
||||||
|
|
||||||
**想回退到之前的状态**
|
**想回退到之前的状态**
|
||||||
```bash
|
```bash
|
||||||
git log --oneline # 看提交
|
git log --oneline # 看提交
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
"name": "tianxuetao-blog",
|
"name": "tianxuetao-blog",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "astro dev",
|
"dev": "astro dev",
|
||||||
"start": "astro dev",
|
"start": "astro dev",
|
||||||
|
|||||||
@@ -17,8 +17,22 @@ const {
|
|||||||
|
|
||||||
const sketchId = `sketch-${Math.random().toString(36).slice(2, 10)}`
|
const sketchId = `sketch-${Math.random().toString(36).slice(2, 10)}`
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 下面两个常量用来拼出 script 开/闭标签,尖括号必须写成 \x3C。
|
||||||
|
*
|
||||||
|
* 原因:Astro 编译器靠**扫描源码文本**里的 script 标签来收集内联脚本,
|
||||||
|
* 它不认识"这只是在字符串或注释里"。一旦源码中出现字面量的开标签,
|
||||||
|
* 它就会把后面的内容当成真的组件脚本抽出来交给 Vite,dev 启动时依赖
|
||||||
|
* 扫描直接报错、整个 /src 刷红。所以:
|
||||||
|
* - 模板字符串里不能写字面量开标签
|
||||||
|
* - 上面这段注释里也不能写(第一版就是栽在这里)
|
||||||
|
* 字符串的值不变,仍然是正常的 script 开闭标签。
|
||||||
|
*/
|
||||||
|
const SCRIPT_OPEN = "\x3Cscript>"
|
||||||
|
const SCRIPT_CLOSE = "\x3C/script>"
|
||||||
|
|
||||||
const resizeScript = autoSize
|
const resizeScript = autoSize
|
||||||
? `<script>(function(){function post(){var h=Math.max(document.documentElement.scrollHeight,document.body?document.body.scrollHeight:0);parent.postMessage({__sketch:true,id:${JSON.stringify(sketchId)},height:h},'*');}var ro=new ResizeObserver(post);ro.observe(document.documentElement);if(document.body)ro.observe(document.body);window.addEventListener('load',post);setTimeout(post,50);setTimeout(post,300);})();<\/script>`
|
? `${SCRIPT_OPEN}(function(){function post(){var h=Math.max(document.documentElement.scrollHeight,document.body?document.body.scrollHeight:0);parent.postMessage({__sketch:true,id:${JSON.stringify(sketchId)},height:h},'*');}var ro=new ResizeObserver(post);ro.observe(document.documentElement);if(document.body)ro.observe(document.body);window.addEventListener('load',post);setTimeout(post,50);setTimeout(post,300);})();${SCRIPT_CLOSE}`
|
||||||
: ""
|
: ""
|
||||||
|
|
||||||
const hasDoctype = html.trim().toLowerCase().startsWith("<!doctype")
|
const hasDoctype = html.trim().toLowerCase().startsWith("<!doctype")
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"updatedAt": "2026-09-15T04:07:15.600Z",
|
"updatedAt": "2026-09-15T04:22:32.764Z",
|
||||||
"source": "weread",
|
"source": "weread",
|
||||||
"books": [
|
"books": [
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user