loggerFactory.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * The LoggerFactory wraps a hard source plugin exposed on the webpack Compiler.
  3. *
  4. * The plugin handle, `'hard-source-log'` takes one object as input and should
  5. * log it to the console, disk, or somewhere. Or not if it the message should
  6. * be ignored. The object has a few arguments that generally follows this
  7. * structure. The `data` key will generally have an `id` value.
  8. *
  9. * ```js
  10. * {
  11. * from: 'core',
  12. * level: 'error',
  13. * message: 'HardSourceWebpackPlugin requires a cacheDirectory setting.',
  14. * data: {
  15. * id: 'need-cache-directory-option'
  16. * }
  17. * }
  18. * ```
  19. *
  20. * So a simple plugin handle may be
  21. *
  22. * ```js
  23. * compiler.plugin('hard-source-log', function(message) {
  24. * console[message.level].call(
  25. * console,
  26. * 'hard-source:' + message.from, message.message
  27. * );
  28. * });
  29. * ```
  30. *
  31. * @module hard-source-webpack-plugin/logger-factory
  32. * @author Michael "Z" Goddard <mzgoddard@gmail.com>
  33. */
  34. const pluginCompat = require('./util/plugin-compat');
  35. const LOGGER_SEPARATOR = ':';
  36. const DEFAULT_LOGGER_PREFIX = 'hard-source';
  37. const LOGGER_FACTORY_COMPILER_KEY = `${__dirname}/hard-source-logger-factory-compiler-key`;
  38. /**
  39. * @constructor Logger
  40. * @memberof module:hard-source-webpack-plugin/logger-factory
  41. */
  42. class Logger {
  43. constructor(compiler) {
  44. this.compiler = compiler;
  45. this._lock = null;
  46. }
  47. /**
  48. * @method lock
  49. * @memberof module:hard-source-webpack-plugin/logger-factory~Logger#
  50. */
  51. lock() {
  52. this._lock = [];
  53. }
  54. /**
  55. * @method unlock
  56. * @memberof module:hard-source-webpack-plugin/logger-factory~Logger#
  57. */
  58. unlock() {
  59. const _this = this;
  60. if (_this._lock) {
  61. const lock = _this._lock;
  62. _this._lock = null;
  63. lock.forEach(value => {
  64. _this.write(value);
  65. });
  66. }
  67. }
  68. /**
  69. * @method write
  70. * @memberof module:hard-source-webpack-plugin/logger-factory~Logger#
  71. */
  72. write(value) {
  73. if (this._lock) {
  74. return this._lock.push(value);
  75. }
  76. if (this.compiler.hooks && this.compiler.hooks.hardSourceLog.taps.length) {
  77. this.compiler.hooks.hardSourceLog.call(value);
  78. } else if (
  79. this.compiler._plugins &&
  80. this.compiler._plugins['hard-source-log'] &&
  81. this.compiler._plugins['hard-source-log'].length
  82. ) {
  83. (this.compiler.applyPlugins1 || this.compiler.applyPlugins).call(
  84. this.compiler,
  85. 'hard-source-log',
  86. value,
  87. );
  88. } else {
  89. console.error(
  90. `[${DEFAULT_LOGGER_PREFIX}${LOGGER_SEPARATOR}${value.from}]`,
  91. value.message,
  92. );
  93. }
  94. }
  95. /**
  96. * @method from
  97. * @memberof module:hard-source-webpack-plugin/logger-factory~Logger#
  98. */
  99. from(name) {
  100. return new LoggerFrom(this, name);
  101. }
  102. }
  103. /**
  104. * @constructor LoggerFrom
  105. * @memberof module:hard-source-webpack-plugin/logger-factory
  106. */
  107. class LoggerFrom {
  108. constructor(logger, from) {
  109. this._logger = logger;
  110. this._from = from;
  111. }
  112. /**
  113. * @method from
  114. * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
  115. */
  116. from(name) {
  117. return new LoggerFrom(this._logger, this._from + LOGGER_SEPARATOR + name);
  118. }
  119. /**
  120. * @method _write
  121. * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
  122. */
  123. _write(level, data, message) {
  124. this._logger.write({
  125. from: this._from,
  126. level,
  127. message,
  128. data,
  129. });
  130. }
  131. /**
  132. * @method error
  133. * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
  134. */
  135. error(data, message) {
  136. this._write('error', data, message);
  137. }
  138. /**
  139. * @method warn
  140. * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
  141. */
  142. warn(data, message) {
  143. this._write('warn', data, message);
  144. }
  145. /**
  146. * @method info
  147. * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
  148. */
  149. info(data, message) {
  150. this._write('info', data, message);
  151. }
  152. /**
  153. * @method log
  154. * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
  155. */
  156. log(data, message) {
  157. this._write('log', data, message);
  158. }
  159. /**
  160. * @method debug
  161. * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
  162. */
  163. debug(data, message) {
  164. this._write('debug', data, message);
  165. }
  166. }
  167. /**
  168. * @constructor LoggerFactory
  169. * @memberof module:hard-source-webpack-plugin/logger-factory
  170. */
  171. class LoggerFactory {
  172. constructor(compiler) {
  173. this.compiler = compiler;
  174. pluginCompat.register(compiler, 'hardSourceLog', 'sync', ['data']);
  175. }
  176. /**
  177. * @method create
  178. * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFactory#
  179. */
  180. create() {
  181. const compiler = this.compiler;
  182. if (!compiler[LOGGER_FACTORY_COMPILER_KEY]) {
  183. compiler[LOGGER_FACTORY_COMPILER_KEY] = new Logger(this.compiler);
  184. }
  185. return compiler[LOGGER_FACTORY_COMPILER_KEY];
  186. }
  187. }
  188. /**
  189. * @function getLogger
  190. * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFactory.
  191. */
  192. LoggerFactory.getLogger = compilation => {
  193. while (compilation.compiler.parentCompilation) {
  194. compilation = compilation.compiler.parentCompilation;
  195. }
  196. return new LoggerFactory(compilation.compiler).create();
  197. };
  198. module.exports = LoggerFactory;