buildPlugin.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //plugins\buildPlugin.ts
  2. import path from "path";
  3. import fs from "fs-extra";
  4. class BuildObj {
  5. //编译主进程代码
  6. buildMain() {
  7. require("esbuild").buildSync({
  8. entryPoints: ["./src/main/mainEntry.ts"],
  9. bundle: true,
  10. platform: "node",
  11. minify: true,
  12. outfile: "./dist/mainEntry.js",
  13. external: ["electron", "pg", "tedious", "mysql", "mysql2", "oracledb", "pg-query-stream", "sqlite3"],
  14. });
  15. }
  16. //为生产环境准备package.json
  17. preparePackageJson() {
  18. let pkgJsonPath = path.join(process.cwd(), "package.json");
  19. let localPkgJson = JSON.parse(fs.readFileSync(pkgJsonPath, "utf-8"));
  20. let electronConfig = localPkgJson.devDependencies.electron.replace("^", "");
  21. localPkgJson.main = "mainEntry.js";
  22. delete localPkgJson.scripts;
  23. delete localPkgJson.devDependencies;
  24. localPkgJson.devDependencies = { electron: electronConfig };
  25. let tarJsonPath = path.join(process.cwd(), "dist", "package.json");
  26. fs.writeFileSync(tarJsonPath, JSON.stringify(localPkgJson));
  27. fs.mkdirSync(path.join(process.cwd(), "dist/node_modules"));
  28. }
  29. async prepareSqlite() {
  30. //拷贝better-sqlite3
  31. let srcDir = path.join(process.cwd(), `node_modules/better-sqlite3`);
  32. let destDir = path.join(process.cwd(), `dist/node_modules/better-sqlite3`);
  33. fs.ensureDirSync(destDir);
  34. console.log(srcDir, destDir);
  35. fs.copySync(srcDir, destDir, {
  36. filter: (src) => {
  37. if (src.endsWith("better-sqlite3") || src.endsWith("build") || src.endsWith("Release") || src.endsWith("better_sqlite3.node")) return true;
  38. else if (src.includes("node_modules\\better-sqlite3\\lib")) return true;
  39. else return false;
  40. },
  41. });
  42. // 拷贝build目录
  43. srcDir = path.join(process.cwd(), `node_modules/better-sqlite3/build`);
  44. destDir = path.join(process.cwd(), `dist/build`);
  45. fs.ensureDirSync(destDir);
  46. fs.copySync(srcDir, destDir);
  47. let pkgJson = `{"name": "better-sqlite3","main": "lib/index.js"}`;
  48. let pkgJsonPath = path.join(process.cwd(), `dist/node_modules/better-sqlite3/package.json`);
  49. fs.writeFileSync(pkgJsonPath, pkgJson);
  50. //制作bindings模块
  51. let bindingPath = path.join(process.cwd(), `dist/node_modules/bindings/index.js`);
  52. fs.ensureFileSync(bindingPath);
  53. let bindingsContent = `module.exports = () => {
  54. let addonPath = require("path").join(__dirname, '../better-sqlite3/build/Release/better_sqlite3.node');
  55. return require(addonPath);
  56. };`;
  57. fs.writeFileSync(bindingPath, bindingsContent);
  58. pkgJson = `{"name": "bindings","main": "index.js"}`;
  59. pkgJsonPath = path.join(process.cwd(), `dist/node_modules/bindings/package.json`);
  60. fs.writeFileSync(pkgJsonPath, pkgJson);
  61. }
  62. prepareKnexjs() {
  63. let pkgJsonPath = path.join(process.cwd(), `dist/node_modules/knex`);
  64. fs.ensureDirSync(pkgJsonPath);
  65. require("esbuild").buildSync({
  66. entryPoints: ["./node_modules/knex/knex.js"],
  67. bundle: true,
  68. platform: "node",
  69. format: "cjs",
  70. minify: true,
  71. outfile: "./dist/node_modules/knex/index.js",
  72. external: ["oracledb", "pg-query-stream", "pg", "sqlite3", "tedious", "mysql", "mysql2", "better-sqlite3"],
  73. });
  74. let pkgJson = `{"name": "bindings","main": "index.js"}`;
  75. pkgJsonPath = path.join(process.cwd(), `dist/node_modules/knex/package.json`);
  76. fs.writeFileSync(pkgJsonPath, pkgJson);
  77. }
  78. //使用electron-builder制成安装包
  79. buildInstaller() {
  80. let options = {
  81. config: {
  82. directories: {
  83. output: path.join(process.cwd(), "release"),
  84. app: path.join(process.cwd(), "dist"),
  85. },
  86. files: ["**"],
  87. extends: null,
  88. productName: "fc-ele",
  89. appId: "com.fcele.kindring.cn",
  90. asar: true,
  91. nsis: {
  92. oneClick: true,
  93. perMachine: true,
  94. allowToChangeInstallationDirectory: false,
  95. createDesktopShortcut: true,
  96. createStartMenuShortcut: true,
  97. shortcutName: "fc-ele",
  98. },
  99. publish: [{ provider: "generic", url: "http://localhost:5500/" }],
  100. extraResources: [
  101. { from: `./db.db`, to: `./` },
  102. { from: `./logo.ico`, to: `./` },
  103. ],
  104. },
  105. project: process.cwd(),
  106. };
  107. return require("electron-builder").build(options);
  108. }
  109. }
  110. export let buildPlugin = () => {
  111. return {
  112. name: "build-plugin",
  113. closeBundle: () => {
  114. let buildObj = new BuildObj();
  115. buildObj.buildMain();
  116. buildObj.preparePackageJson();
  117. buildObj.prepareSqlite();
  118. buildObj.buildInstaller();
  119. buildObj.prepareKnexjs();
  120. },
  121. };
  122. };