dev-runner.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. "use strict";
  2. const chalk = require("chalk");
  3. const electron = require("electron");
  4. const path = require("path");
  5. const { say } = require("cfonts");
  6. const { spawn } = require("child_process");
  7. const webpack = require("webpack");
  8. const WebpackDevServer = require("webpack-dev-server");
  9. const webpackHotMiddleware = require("webpack-hot-middleware");
  10. const mainConfig = require("./webpack.main.config");
  11. const rendererConfig = require("./webpack.renderer.config");
  12. let electronProcess = null;
  13. let manualRestart = false;
  14. let hotMiddleware;
  15. function logStats(proc, data) {
  16. let log = "";
  17. log += chalk.yellow.bold(
  18. `┏ ${proc} Process ${new Array(19 - proc.length + 1).join("-")}`
  19. );
  20. log += "\n\n";
  21. if (typeof data === "object") {
  22. data
  23. .toString({
  24. colors: true,
  25. chunks: false
  26. })
  27. .split(/\r?\n/)
  28. .forEach(line => {
  29. log += " " + line + "\n";
  30. });
  31. } else {
  32. log += ` ${data}\n`;
  33. }
  34. log += "\n" + chalk.yellow.bold(`┗ ${new Array(28 + 1).join("-")}`) + "\n";
  35. }
  36. function startRenderer() {
  37. return new Promise((resolve, reject) => {
  38. rendererConfig.entry.renderer = [path.join(__dirname, "dev-client")].concat(
  39. rendererConfig.entry.renderer
  40. );
  41. const compiler = webpack(rendererConfig);
  42. hotMiddleware = webpackHotMiddleware(compiler, {
  43. log: false,
  44. heartbeat: 2500
  45. });
  46. compiler.plugin("compilation", compilation => {
  47. compilation.plugin("html-webpack-plugin-after-emit", (data, cb) => {
  48. hotMiddleware.publish({ action: "reload" });
  49. cb();
  50. });
  51. });
  52. compiler.plugin("done", stats => {
  53. logStats("Renderer", stats);
  54. });
  55. const server = new WebpackDevServer(compiler, {
  56. contentBase: path.join(__dirname, "../"),
  57. hot: true,
  58. quiet: true,
  59. before(app, ctx) {
  60. app.use(hotMiddleware);
  61. ctx.middleware.waitUntilValid(() => {
  62. resolve();
  63. });
  64. }
  65. });
  66. server.listen(9080);
  67. });
  68. }
  69. function startMain() {
  70. return new Promise((resolve, reject) => {
  71. mainConfig.entry.main = [
  72. path.join(__dirname, "../src/main/index.dev.js")
  73. ].concat(mainConfig.entry.main);
  74. const compiler = webpack(mainConfig);
  75. compiler.plugin("watch-run", (compilation, done) => {
  76. logStats("Main", chalk.white.bold("compiling..."));
  77. hotMiddleware.publish({ action: "compiling" });
  78. done();
  79. });
  80. compiler.watch({}, (err, stats) => {
  81. if (err) {
  82. return;
  83. }
  84. logStats("Main", stats);
  85. if (electronProcess && electronProcess.kill) {
  86. manualRestart = true;
  87. process.kill(electronProcess.pid);
  88. electronProcess = null;
  89. startElectron();
  90. setTimeout(() => {
  91. manualRestart = false;
  92. }, 5000);
  93. }
  94. resolve();
  95. });
  96. });
  97. }
  98. function startElectron() {
  99. electronProcess = spawn(electron, [
  100. "--inspect=5858",
  101. path.join(__dirname, "../dist/electron/main.js")
  102. ]);
  103. electronProcess.stdout.on("data", data => {
  104. electronLog(data, "blue");
  105. });
  106. electronProcess.stderr.on("data", data => {
  107. electronLog(data, "red");
  108. });
  109. electronProcess.on("close", () => {
  110. if (!manualRestart) process.exit();
  111. });
  112. }
  113. function electronLog(data, color) {
  114. let log = "";
  115. data = data.toString().split(/\r?\n/);
  116. data.forEach(line => {
  117. log += ` ${line}\n`;
  118. });
  119. if (/[0-9A-z]+/.test(log)) {}
  120. }
  121. function greeting() {
  122. const cols = process.stdout.columns;
  123. let text = "";
  124. if (cols > 104) text = "electron-vue";
  125. else if (cols > 76) text = "electron-|vue";
  126. else text = false;
  127. if (text) {
  128. say(text, {
  129. colors: ["yellow"],
  130. font: "simple3d",
  131. space: false
  132. });
  133. }
  134. }
  135. function init() {
  136. greeting();
  137. Promise.all([startRenderer(), startMain()])
  138. .then(() => {
  139. startElectron();
  140. })
  141. .catch(err => {
  142. });
  143. }
  144. init();