compiler.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.runCompiler = runCompiler;
  6. var _webpack = require("next/dist/compiled/webpack/webpack");
  7. function generateStats(result, stat) {
  8. const { errors , warnings } = stat.toJson({
  9. preset: "errors-warnings",
  10. moduleTrace: true
  11. });
  12. if (errors && errors.length > 0) {
  13. result.errors.push(...errors);
  14. }
  15. if (warnings && warnings.length > 0) {
  16. result.warnings.push(...warnings);
  17. }
  18. return result;
  19. }
  20. // Webpack 5 requires the compiler to be closed (to save caches)
  21. // Webpack 4 does not have this close method so in order to be backwards compatible we check if it exists
  22. function closeCompiler(compiler) {
  23. return new Promise((resolve, reject)=>{
  24. // @ts-ignore Close only exists on the compiler in webpack 5
  25. return compiler.close((err)=>err ? reject(err) : resolve());
  26. });
  27. }
  28. function runCompiler(config, { runWebpackSpan }) {
  29. return new Promise((resolve, reject)=>{
  30. const compiler = (0, _webpack).webpack(config);
  31. compiler.run((err, stats)=>{
  32. const webpackCloseSpan = runWebpackSpan.traceChild("webpack-close", {
  33. name: config.name
  34. });
  35. webpackCloseSpan.traceAsyncFn(()=>closeCompiler(compiler)).then(()=>{
  36. if (err) {
  37. var _stack;
  38. const reason = (_stack = err.stack) != null ? _stack : err.toString();
  39. if (reason) {
  40. return resolve({
  41. errors: [
  42. {
  43. message: reason,
  44. details: err.details
  45. }
  46. ],
  47. warnings: [],
  48. stats
  49. });
  50. }
  51. return reject(err);
  52. } else if (!stats) throw new Error("No Stats from webpack");
  53. const result = webpackCloseSpan.traceChild("webpack-generate-error-stats").traceFn(()=>generateStats({
  54. errors: [],
  55. warnings: [],
  56. stats
  57. }, stats));
  58. return resolve(result);
  59. });
  60. });
  61. });
  62. }
  63. //# sourceMappingURL=compiler.js.map