printBuildError.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. 'use strict';
  8. const chalk = require('chalk');
  9. module.exports = function printBuildError(err) {
  10. const message = err != null && err.message;
  11. const stack = err != null && err.stack;
  12. // Add more helpful message for Terser error
  13. if (
  14. stack &&
  15. typeof message === 'string' &&
  16. message.indexOf('from Terser') !== -1
  17. ) {
  18. try {
  19. const matched = /(.+)\[(.+):(.+),(.+)\]\[.+\]/.exec(stack);
  20. if (!matched) {
  21. throw new Error('Using errors for control flow is bad.');
  22. }
  23. const problemPath = matched[2];
  24. const line = matched[3];
  25. const column = matched[4];
  26. console.log(
  27. 'Failed to minify the code from this file: \n\n',
  28. chalk.yellow(
  29. `\t${problemPath}:${line}${column !== '0' ? ':' + column : ''}`
  30. ),
  31. '\n'
  32. );
  33. } catch (ignored) {
  34. console.log('Failed to minify the bundle.', err);
  35. }
  36. console.log('Read more here: https://cra.link/failed-to-minify');
  37. } else {
  38. console.log((message || err) + '\n');
  39. }
  40. console.log();
  41. };