修复 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:
dsh
2026-09-15 12:22:44 +08:00
parent 30c958c2f0
commit 9c0851fd12
5 changed files with 49 additions and 2 deletions
+15 -1
View File
@@ -17,8 +17,22 @@ const {
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
? `<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")
+1 -1
View File
@@ -1,5 +1,5 @@
{
"updatedAt": "2026-09-15T04:07:15.600Z",
"updatedAt": "2026-09-15T04:22:32.764Z",
"source": "weread",
"books": [
{