run-compiler.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const fs = require('fs');
  2. const path = require('path');
  3. const webpack = require('webpack');
  4. const configPath = process.argv[2];
  5. const outPath = path.resolve(process.argv[3]);
  6. const config = require(configPath);
  7. config.output = {
  8. path: path.dirname(outPath),
  9. filename: path.basename(outPath)
  10. };
  11. const { wrapInitWithProfilingTimeout, wrapInitWithTryCatch, ...webpackConfig } = config;
  12. webpack(webpackConfig, (err, stats) => {
  13. if (err) {
  14. console.error(err);
  15. process.exit(1);
  16. } else if (stats.hasErrors()) {
  17. console.error(stats.toString('normal'));
  18. process.exit(1);
  19. } else {
  20. let contents = fs.readFileSync(outPath, 'utf8');
  21. if (wrapInitWithTryCatch) {
  22. contents = `try {
  23. ${contents}
  24. } catch (err) {
  25. console.error('Electron ${webpackConfig.output.filename} script failed to run');
  26. console.error(err);
  27. }`;
  28. }
  29. if (wrapInitWithProfilingTimeout) {
  30. contents = `function ___electron_webpack_init__() {
  31. ${contents}
  32. };
  33. if ((globalThis.process || binding.process).argv.includes("--profile-electron-init")) {
  34. setTimeout(___electron_webpack_init__, 0);
  35. } else {
  36. ___electron_webpack_init__();
  37. }`;
  38. }
  39. fs.writeFileSync(outPath, contents);
  40. process.exit(0);
  41. }
  42. });