sync update from LAN repo (source master @ c21a2a4a)

This commit is contained in:
gozeros committed 2026-11-10 14:03:50 +08:00
1 parent ea9dcff9c0
commit 66a443e915
140 files changed
+13777 -1040

No files matched your search

+89 -12
View File
@@ -1,4 +1,4 @@
import { existsSync } from "node:fs";
import { existsSync, readFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
@@ -8,11 +8,63 @@ import sourceIdentifierPlugin from "vite-plugin-source-identifier";
const rootDir = fileURLToPath(new URL(".", import.meta.url));
const JSX_RUNTIME_NAMED_EXPORTS = [
"jsx",
"jsxs",
"jsxDEV",
"createElement",
"Fragment",
] as const;
const JSX_RUNTIME_DEP_FILES = new Set([
"react_jsx-runtime.js",
"react_jsx-dev-runtime.js",
]);
function hasNamedJsxExport(code: string): boolean {
return (
/\bexport\s+const\s+jsx\b/.test(code) ||
/\bexport\s+function\s+jsx\b/.test(code) ||
/\bexport\s*\{[^}]*\bjsx\b/.test(code)
);
}
// Vite prebundles CJS `react/jsx-runtime` as `export default require_jsx_runtime()`.
// App source is rewritten to a default import, but a stale Chrome 102 tab may still
// execute `import { jsx } from '.../react_jsx-runtime.js'`. Named re-exports from a
// default-only module throw SyntaxError at link time, so the page never reloads.
function withNamedJsxExports(code: string): string {
if (hasNamedJsxExport(code)) return code;
return code.replace(
/export\s+default\s+([^;]+);/,
(_match, expr: string) =>
[
`const __jsxRuntime = ${String(expr).trim()};`,
"export default __jsxRuntime;",
...JSX_RUNTIME_NAMED_EXPORTS.map(
(name) => `export const ${name} = __jsxRuntime.${name};`,
),
].join("\n"),
);
}
function staleDepsModuleSource(): string {
return [
"export function jsx(){return null}",
"export function jsxs(){return null}",
"export function jsxDEV(){return null}",
"export const Fragment = Symbol.for('react.fragment');",
"export default {jsx, jsxs, jsxDEV, Fragment};",
"try{const k='__vite_stale_chunk_reloaded__';const n=Number(sessionStorage.getItem(k)||'0');" +
"if(n<3){sessionStorage.setItem(k,String(n+1));location.reload();}}catch(e){}",
].join("\n");
}
// 依赖预构建缓存被重建后(pnpm install / 重复启动 vite / --force),
// 残留的旧浏览器标签页仍会按旧版本号(?v=<hash>)请求已删除的 chunk,
// 每次都在终端刷 "The file does not exist ... in the optimize deps directory"。
// 该插件拦截这类「文件确实不存在」的 deps 请求,返回一段触发 location.reload()
// 的模块脚本,让旧页面自愈到新版本;sessionStorage 防止真正的坏页面无限刷新。
// 残留的旧浏览器标签页仍会按旧版本号(?v=<hash>)请求已删除的 chunk。
// Chrome 在链接 `import { jsx } from ...` 时若没有本地 named export,会直接
// SyntaxError,模块体根本不会执行,页面就白屏卡死。缺失 chunk 一律补本地
// jsx 导出再刷新;真实 jsx-runtime 也补上 named export,兼容旧模块图。
const staleDepsChunkSelfReload = (): Plugin => ({
name: "stale-deps-chunk-self-reload",
configureServer(server) {
@@ -23,17 +75,28 @@ const staleDepsChunkSelfReload = (): Plugin => ({
next();
return;
}
const filePath = path.join(depsDir, decodeURIComponent(match[1]));
const fileName = path.basename(decodeURIComponent(match[1]));
if (fileName !== decodeURIComponent(match[1])) {
next();
return;
}
const filePath = path.join(depsDir, fileName);
if (existsSync(filePath)) {
if (!JSX_RUNTIME_DEP_FILES.has(fileName)) {
next();
return;
}
res.setHeader("Content-Type", "application/javascript");
res.end(withNamedJsxExports(readFileSync(filePath, "utf8")));
return;
}
if (!/^chunk-[A-Za-z0-9_-]+\.js$/.test(fileName)) {
next();
return;
}
res.setHeader("Content-Type", "application/javascript");
res.end(
"if(!sessionStorage.getItem('__vite_stale_chunk_reloaded__')){" +
"sessionStorage.setItem('__vite_stale_chunk_reloaded__','1');" +
"location.reload();}",
);
res.setHeader("Cache-Control", "no-store");
res.end(staleDepsModuleSource());
});
},
});
@@ -91,7 +154,21 @@ export default defineConfig({
// 下混用新旧缓存 chunk,报 "does not provide an export named 'refractor'"
// 并整树白屏(清 node_modules/.vite + 重启 + 硬刷新才恢复)。启动即预打包可避免。
optimizeDeps: {
include: ["react-syntax-highlighter", "refractor"],
include: [
"react",
"react/jsx-runtime",
"react/jsx-dev-runtime",
"react-dom",
"react-dom/client",
"react-syntax-highlighter",
"refractor",
],
esbuildOptions: {
target: "chrome100",
},
},
esbuild: {
target: "chrome100",
},
// Relative URLs make the same build work at the domain root and at any
// Nginx path prefix. A fixed prefix can still be supplied at release time.