build.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const webpack = require("webpack");
  2. const { resolve } = require("path");
  3. const ora = require("ora");
  4. const { spawn } = require("child_process");
  5. const rm = require("rimraf");
  6. const chalk = require("chalk");
  7. const webpackConfig = require("./webpack.config.main.js");
  8. const electron = require("electron");
  9. const spinner = ora("building electron...");
  10. spinner.start();
  11. let electronProcess = null;
  12. const isDev = process.env.NODE_ENV !== "production";
  13. const startMain = () => {
  14. return new Promise((resolve) => {
  15. rm("./dist/main.js", (removeErr) => {
  16. if (removeErr) {
  17. throw removeErr;
  18. }
  19. webpackConfig.mode = isDev ? "development" : "production";
  20. const compiler = webpack(webpackConfig);
  21. compiler.watch({}, (err, stats) => {
  22. if (err) throw err;
  23. spinner.stop();
  24. process.stdout.write(
  25. stats.toString({
  26. colors: true,
  27. modules: false,
  28. children: false,
  29. chunks: false,
  30. chunkModules: false,
  31. }) + "\n\n"
  32. );
  33. if (stats.hasErrors()) {
  34. console.log(chalk.red("Build failed with errors.\n"));
  35. process.exit(1);
  36. }
  37. resolve();
  38. console.log(
  39. chalk.cyan(
  40. `Build complete in ${stats.endTime - stats.startTime}ms.\n`
  41. )
  42. );
  43. if (process.env.IS_BUILDER === "builder") process.exit();
  44. });
  45. });
  46. });
  47. };
  48. const startElectron = () => {
  49. if (process.env.IS_BUILDER === "builder") return;
  50. var args = ["--inspect=5858", resolve(__dirname, "../dist/main.js")];
  51. electronProcess = spawn(electron, args);
  52. if (isDev) {
  53. electronProcess.stdout.on("data", (data) => {
  54. console.log(chalk.blue(data.toString()));
  55. });
  56. electronProcess.stderr.on("data", (data) => {
  57. console.log(chalk.blue(data.toString()));
  58. });
  59. }
  60. electronProcess.on("close", () => {
  61. process.exit();
  62. });
  63. };
  64. async function init() {
  65. try {
  66. await startMain();
  67. await startElectron();
  68. } catch (error) {
  69. console.error(error);
  70. }
  71. }
  72. init();