cool-admin-vue/build/cool/index.ts

70 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-04-02 22:50:51 +08:00
import { Plugin } from "vite";
import { parseJson } from "./utils";
2022-07-21 19:07:32 +08:00
import { createEps, createMenu, createSvg, createTag, getEps, getModules } from "./lib";
2022-04-02 22:50:51 +08:00
2022-07-21 19:07:32 +08:00
export function cool(): Plugin {
2022-04-02 22:50:51 +08:00
return {
name: "vite-cool",
2022-07-21 19:07:32 +08:00
enforce: "pre",
2022-04-02 22:50:51 +08:00
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
function done(data: any) {
res.writeHead(200, { "Content-Type": "text/html;charset=UTF-8" });
res.end(JSON.stringify(data));
}
2022-07-21 19:07:32 +08:00
if (req.url?.includes("__cool")) {
2022-04-02 22:50:51 +08:00
const body = await parseJson(req);
2022-07-21 19:07:32 +08:00
let next: any;
2022-04-02 22:50:51 +08:00
switch (req.url) {
// 快速创建菜单
case "/__cool_createMenu":
next = createMenu(body);
break;
// 获取模块列表
case "/__cool_modules":
next = getModules();
break;
// 创建描述文件
case "/__cool_eps":
next = createEps(body);
break;
}
if (next) {
next.then((data: any) => {
done({
code: 1000,
data
});
}).catch((message: string) => {
done({
code: 1001,
message
});
});
}
} else {
next();
}
});
},
2022-07-21 19:07:32 +08:00
transform(code, id) {
return createTag(code, id);
},
transformIndexHtml(html) {
return createSvg(html);
},
2022-04-02 22:50:51 +08:00
config() {
return {
define: {
__EPS__: getEps()
}
};
}
};
2022-07-21 19:07:32 +08:00
}