ChalkLoggerPlugin.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. const chalk = require('chalk');
  2. const pluginCompat = require('./util/plugin-compat');
  3. const LOGGER_SEPARATOR = ':';
  4. const DEFAULT_LOGGER_PREFIX = 'hardsource';
  5. const messages = {
  6. 'serialization--error-freezing-module': {
  7. short: value =>
  8. `Could not freeze ${value.data.moduleReadable}: ${
  9. value.data.errorMessage
  10. }`,
  11. },
  12. 'serialzation--cache-incomplete': {
  13. short: value =>
  14. `Last compilation did not finish saving. Building new cache.`,
  15. },
  16. 'confighash--directory-no-confighash': {
  17. short: value => `Config hash skipped in cache directory.`,
  18. },
  19. 'confighash--new': {
  20. short: value =>
  21. `Writing new cache ${value.data.configHash.substring(0, 8)}...`,
  22. },
  23. 'confighash--reused': {
  24. short: value =>
  25. `Reading from cache ${value.data.configHash.substring(0, 8)}...`,
  26. },
  27. 'caches--delete-old': {
  28. short: value =>
  29. `Deleted ${value.data.deletedSizeMB} MB. Using ${
  30. value.data.sizeMB
  31. } MB of disk space.`,
  32. },
  33. 'caches--keep': {
  34. short: value => `Using ${value.data.sizeMB} MB of disk space.`,
  35. },
  36. 'environment--inputs': {
  37. short: value =>
  38. `Tracking node dependencies with: ${value.data.inputs.join(', ')}.`,
  39. },
  40. 'environment--config-changed': {
  41. short: value => 'Configuration changed. Building new cache.',
  42. },
  43. 'environment--changed': {
  44. short: value => `Node dependencies changed. Building new cache.`,
  45. },
  46. 'environment--hardsource-changed': {
  47. short: value => `hard-source version changed. Building new cache.`,
  48. },
  49. 'childcompiler--no-cache': {
  50. once: value =>
  51. `A child compiler has its cache disabled. Skipping child in hard-source.`,
  52. },
  53. 'childcompiler--unnamed-cache': {
  54. once: value =>
  55. `A child compiler has unnamed cache. Skipping child in hard-source.`,
  56. },
  57. unrecognized: {
  58. short: value => value.message,
  59. },
  60. };
  61. const logLevels = ['error', 'warn', 'info', 'log', 'debug'];
  62. const levelId = level => logLevels.indexOf(level.toLowerCase());
  63. const compareLevel = (a, b) => levelId(a) - levelId(b);
  64. class ChalkLoggerPlugin {
  65. constructor(options = {}) {
  66. this.options = options;
  67. this.once = {};
  68. // mode: 'test' or 'none'
  69. this.options.mode =
  70. this.options.mode || (process.env.NODE_ENV === 'test' ? 'test' : 'none');
  71. // level: 'error', 'warn', 'info', 'log', 'debug'
  72. this.options.level =
  73. this.options.level || (this.options.mode === 'test' ? 'warn' : 'debug');
  74. }
  75. apply(compiler) {
  76. const compilerHooks = pluginCompat.hooks(compiler);
  77. compilerHooks.hardSourceLog.tap('HardSource - ChalkLoggerPlugin', value => {
  78. if (compareLevel(this.options.level, value.level) < 0) {
  79. return;
  80. }
  81. let headerColor = chalk.white;
  82. let color = chalk.white;
  83. if (value.level === 'error') {
  84. headerColor = chalk.red;
  85. } else if (value.level === 'warn') {
  86. headerColor = chalk.yellow;
  87. } else if (value.level === 'info') {
  88. headerColor = chalk.white;
  89. } else {
  90. headerColor = color = chalk.gray;
  91. }
  92. const header = headerColor(
  93. `[${DEFAULT_LOGGER_PREFIX}${LOGGER_SEPARATOR}${compiler.__hardSource_shortConfigHash ||
  94. value.from}]`,
  95. );
  96. // Always use warn or error so that output goes to stderr.
  97. const consoleFn = value.level === 'error' ? console.error : console.warn;
  98. let handle = messages[value.data.id];
  99. if (!handle) {
  100. handle = messages.unrecognized;
  101. }
  102. if (handle) {
  103. if (handle.once) {
  104. if (!this.once[value.data.id]) {
  105. this.once[value.data.id] = true;
  106. consoleFn.call(console, header, color(handle.once(value)));
  107. }
  108. } else if (handle.short) {
  109. consoleFn.call(console, header, color(handle.short(value)));
  110. }
  111. } else {
  112. consoleFn.call(console, header, color(value.message));
  113. }
  114. });
  115. }
  116. }
  117. module.exports = ChalkLoggerPlugin;