devPlugin.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { ViteDevServer } from "vite";
  2. export let devPlugin = () => {
  3. return {
  4. name: "dev-plugin",
  5. configureServer(server: ViteDevServer) {
  6. require("esbuild").buildSync({
  7. entryPoints: ["./src/main/mainEntry.ts"],
  8. bundle: true,
  9. platform: "node",
  10. outfile: "./dist/mainEntry.js",
  11. external: ["electron", "pg", "tedious", "mysql", "mysql2", "oracledb", "pg-query-stream", "sqlite3"],
  12. });
  13. // require("esbuild").buildSync({
  14. // entryPoints: ["./src/main/preload.ts"],
  15. // bundle: true,
  16. // platform: "node",
  17. // outfile: "./dist/preload.js",
  18. // external: ["electron", "pg", "tedious", "mysql", "mysql2", "oracledb", "pg-query-stream", "sqlite3"],
  19. // });
  20. if (!server.httpServer) throw new Error("server.httpServer is null check devPlugin.ts ");
  21. server.httpServer?.once("listening", () => {
  22. let { spawn } = require("child_process");
  23. let addressInfo = server.httpServer?.address() as any;
  24. // console.log(server);
  25. // console.log(addressInfo);
  26. let httpAddress = `http://${addressInfo.address}:${addressInfo.port}`;
  27. let electronProcess = spawn(require("electron").toString(), ["./dist/mainEntry.js", httpAddress], {
  28. cwd: process.cwd(),
  29. stdio: "inherit",
  30. });
  31. electronProcess.on("close", () => {
  32. server.close();
  33. process.exit();
  34. });
  35. });
  36. },
  37. };
  38. };
  39. type ResultObject = {
  40. [key: string]: () => { find: RegExp; code: string };
  41. };
  42. export let getReplacer = () => {
  43. let externalModels = ["os", "fs", "path", "events", "child_process", "crypto", "http", "buffer", "url", "better-sqlite3", "knex"];
  44. let result:ResultObject = {};
  45. for (let item of externalModels) {
  46. result[item] = () => ({
  47. find: new RegExp(`^${item}$`),
  48. code: `const ${item} = require('${item}');export { ${item} as default }`,
  49. });
  50. }
  51. result["electron"] = () => {
  52. let electronModules = ["clipboard", "ipcRenderer", "nativeImage", "shell", "webFrame"].join(",");
  53. return {
  54. find: new RegExp(`^electron$`),
  55. code: `const {${electronModules}} = require('electron');export {${electronModules}}`,
  56. };
  57. };
  58. return result;
  59. };