cool-admin-vue/build/cool/demo.ts
神仙都没用 897571c7ad 发布 7.1.0
2024-01-25 17:30:25 +08:00

47 lines
960 B
TypeScript

import type { Plugin } from "vite";
import { glob } from "glob";
import path from "path";
import { readFileSync } from "fs";
export function demo(enable?: boolean): Plugin {
const virtualModuleIds = ["virtual:demo"];
return {
name: "vite-cool-demo",
enforce: "pre",
resolveId(id) {
if (virtualModuleIds.includes(id)) {
return "\0" + id;
}
},
async load(id) {
if (id === "\0virtual:demo") {
const demo = {};
if (enable) {
const files = await glob("./src/modules/demo/views/crud/components/**", {
stat: true,
withFileTypes: true
});
for (const file of files) {
if (file.isFile()) {
const p = path.join(file.path, file.name);
demo[
p
.replace(/\\/g, "/")
.split("src/modules/demo/views/crud/components/")[1]
] = readFileSync(p, "utf-8");
}
}
}
return `
export const demo = ${JSON.stringify(demo)};
`;
}
}
};
}