mirror of
https://github.com/basketikun/infinite-canvas.git
synced 2026-08-05 17:04:27 +08:00
feat(plugin): add local plugin discovery and management with cache busting
This commit is contained in:
+31
-3
@@ -1,8 +1,8 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { readdirSync, readFileSync } from "node:fs";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import react from "@vitejs/plugin-react";
|
||||
import { defineConfig } from "vite";
|
||||
import { defineConfig, type Plugin } from "vite";
|
||||
|
||||
import { parseChangelog } from "./src/lib/release";
|
||||
|
||||
@@ -10,9 +10,37 @@ const webDir = dirname(fileURLToPath(import.meta.url));
|
||||
const localVersion = readFileSync(resolve(webDir, "../VERSION"), "utf8").trim() || "dev";
|
||||
const localChangelog = readFileSync(resolve(webDir, "../CHANGELOG.md"), "utf8");
|
||||
|
||||
// 暴露 /plugins/index.json:列出 public/plugins 下的本地插件文件,
|
||||
// 供前端自动发现并加入插件列表(默认关闭)。dev 下实时读目录,构建时产出静态清单。
|
||||
function localPluginsManifest(): Plugin {
|
||||
const pluginsDir = resolve(webDir, "public/plugins");
|
||||
const listLocalPlugins = () => {
|
||||
try {
|
||||
return readdirSync(pluginsDir)
|
||||
.filter((file) => file.endsWith(".js"))
|
||||
.sort()
|
||||
.map((file) => `/plugins/${file}`);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
return {
|
||||
name: "local-plugins-manifest",
|
||||
configureServer(server) {
|
||||
server.middlewares.use("/plugins/index.json", (_req, res) => {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
res.end(JSON.stringify(listLocalPlugins()));
|
||||
});
|
||||
},
|
||||
generateBundle() {
|
||||
this.emitFile({ type: "asset", fileName: "plugins/index.json", source: JSON.stringify(listLocalPlugins()) });
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export default defineConfig({
|
||||
base: process.env.VITE_BASE || "/",
|
||||
plugins: [react()],
|
||||
plugins: [react(), localPluginsManifest()],
|
||||
resolve: {
|
||||
alias: {
|
||||
"@": resolve(webDir, "src"),
|
||||
|
||||
Reference in New Issue
Block a user