diff --git a/packages/leafer-x-webfont/demo/index.html b/packages/leafer-x-webfont/demo/index.html index 013da51..498a905 100644 --- a/packages/leafer-x-webfont/demo/index.html +++ b/packages/leafer-x-webfont/demo/index.html @@ -29,7 +29,9 @@

LeaferJS 中文字体插件 —— 画布里的 Text 用什么字体,就只加载那几个字的子集(6 字 ≈ 6KB,而非 16MB)。 服务端:webfont.shenzilong.cn - / GitHub + / 插件源码 + / 本页源码 + / npm

diff --git a/scripts/leafer-demo-plugin.ts b/scripts/leafer-demo-plugin.ts new file mode 100644 index 0000000..7d26b26 --- /dev/null +++ b/scripts/leafer-demo-plugin.ts @@ -0,0 +1,27 @@ +/** + * 构建时把 packages/leafer-x-webfont/demo/index.html 复制进主站 dist。 + * + * 该 demo 是零构建的纯静态单文件(依赖全走 jsDelivr CDN),不参与 vite-ssg 管线; + * 挂到主站 /leafer-demo/ 路径下,后端 staticFileMiddleware 已支持目录 → index.html 解析。 + * 若 demo 未来变成多文件,需改为 copy 整个目录。 + */ +import type { Plugin } from "vite"; +import { copyFileSync, mkdirSync } from "node:fs"; +import { dirname, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; + +export function leaferDemoPlugin(): Plugin { + const rootDir = dirname(dirname(fileURLToPath(import.meta.url))); + const source = resolve(rootDir, "packages/leafer-x-webfont/demo/index.html"); + const target = resolve(rootDir, "dist/leafer-demo/index.html"); + + return { + name: "copy-leafer-demo", + apply: "build", + closeBundle() { + mkdirSync(dirname(target), { recursive: true }); + copyFileSync(source, target); + console.log(`[leafer-demo] copied → dist/leafer-demo/index.html`); + }, + }; +} diff --git a/vite.config.ts b/vite.config.ts index e3f4327..473cead 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -4,6 +4,7 @@ import vue from "@vitejs/plugin-vue"; import { pilot } from "vite-plugin-pilot"; import commonjs from "vite-plugin-commonjs"; import { sitemapPlugin } from "./scripts/sitemap-plugin"; +import { leaferDemoPlugin } from "./scripts/leafer-demo-plugin"; /** * 引入 vite-ssg 的类型声明 —— * vite-ssg 内部通过 declaration merging 给 vite 的 UserConfig 扩展了 ssgOptions 字段, @@ -22,6 +23,7 @@ const sitemapRoutes = [ { path: "/offline-subset", changefreq: "weekly" as const, priority: 0.8 }, { path: "/docs", changefreq: "weekly" as const, priority: 0.8 }, { path: "/demo", changefreq: "monthly" as const, priority: 0.6 }, + { path: "/leafer-demo/", changefreq: "monthly" as const, priority: 0.6 }, ]; export default defineConfig({ @@ -132,6 +134,8 @@ export default { isInited: () => false, init: () => Promise.resolve(), encode: ( }, }, sitemapPlugin(sitemapRoutes), + /** 把 leafer 插件 demo 复制进 dist/leafer-demo/(纯静态单文件,不走 vite-ssg 管线) */ + leaferDemoPlugin(), /** * 构建时替换 index.html 中的时间戳占位符 *