123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- /**
- * The LoggerFactory wraps a hard source plugin exposed on the webpack Compiler.
- *
- * The plugin handle, `'hard-source-log'` takes one object as input and should
- * log it to the console, disk, or somewhere. Or not if it the message should
- * be ignored. The object has a few arguments that generally follows this
- * structure. The `data` key will generally have an `id` value.
- *
- * ```js
- * {
- * from: 'core',
- * level: 'error',
- * message: 'HardSourceWebpackPlugin requires a cacheDirectory setting.',
- * data: {
- * id: 'need-cache-directory-option'
- * }
- * }
- * ```
- *
- * So a simple plugin handle may be
- *
- * ```js
- * compiler.plugin('hard-source-log', function(message) {
- * console[message.level].call(
- * console,
- * 'hard-source:' + message.from, message.message
- * );
- * });
- * ```
- *
- * @module hard-source-webpack-plugin/logger-factory
- * @author Michael "Z" Goddard <mzgoddard@gmail.com>
- */
- const pluginCompat = require('./util/plugin-compat');
- const LOGGER_SEPARATOR = ':';
- const DEFAULT_LOGGER_PREFIX = 'hard-source';
- const LOGGER_FACTORY_COMPILER_KEY = `${__dirname}/hard-source-logger-factory-compiler-key`;
- /**
- * @constructor Logger
- * @memberof module:hard-source-webpack-plugin/logger-factory
- */
- class Logger {
- constructor(compiler) {
- this.compiler = compiler;
- this._lock = null;
- }
- /**
- * @method lock
- * @memberof module:hard-source-webpack-plugin/logger-factory~Logger#
- */
- lock() {
- this._lock = [];
- }
- /**
- * @method unlock
- * @memberof module:hard-source-webpack-plugin/logger-factory~Logger#
- */
- unlock() {
- const _this = this;
- if (_this._lock) {
- const lock = _this._lock;
- _this._lock = null;
- lock.forEach(value => {
- _this.write(value);
- });
- }
- }
- /**
- * @method write
- * @memberof module:hard-source-webpack-plugin/logger-factory~Logger#
- */
- write(value) {
- if (this._lock) {
- return this._lock.push(value);
- }
- if (this.compiler.hooks && this.compiler.hooks.hardSourceLog.taps.length) {
- this.compiler.hooks.hardSourceLog.call(value);
- } else if (
- this.compiler._plugins &&
- this.compiler._plugins['hard-source-log'] &&
- this.compiler._plugins['hard-source-log'].length
- ) {
- (this.compiler.applyPlugins1 || this.compiler.applyPlugins).call(
- this.compiler,
- 'hard-source-log',
- value,
- );
- } else {
- console.error(
- `[${DEFAULT_LOGGER_PREFIX}${LOGGER_SEPARATOR}${value.from}]`,
- value.message,
- );
- }
- }
- /**
- * @method from
- * @memberof module:hard-source-webpack-plugin/logger-factory~Logger#
- */
- from(name) {
- return new LoggerFrom(this, name);
- }
- }
- /**
- * @constructor LoggerFrom
- * @memberof module:hard-source-webpack-plugin/logger-factory
- */
- class LoggerFrom {
- constructor(logger, from) {
- this._logger = logger;
- this._from = from;
- }
- /**
- * @method from
- * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
- */
- from(name) {
- return new LoggerFrom(this._logger, this._from + LOGGER_SEPARATOR + name);
- }
- /**
- * @method _write
- * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
- */
- _write(level, data, message) {
- this._logger.write({
- from: this._from,
- level,
- message,
- data,
- });
- }
- /**
- * @method error
- * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
- */
- error(data, message) {
- this._write('error', data, message);
- }
- /**
- * @method warn
- * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
- */
- warn(data, message) {
- this._write('warn', data, message);
- }
- /**
- * @method info
- * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
- */
- info(data, message) {
- this._write('info', data, message);
- }
- /**
- * @method log
- * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
- */
- log(data, message) {
- this._write('log', data, message);
- }
- /**
- * @method debug
- * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFrom#
- */
- debug(data, message) {
- this._write('debug', data, message);
- }
- }
- /**
- * @constructor LoggerFactory
- * @memberof module:hard-source-webpack-plugin/logger-factory
- */
- class LoggerFactory {
- constructor(compiler) {
- this.compiler = compiler;
- pluginCompat.register(compiler, 'hardSourceLog', 'sync', ['data']);
- }
- /**
- * @method create
- * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFactory#
- */
- create() {
- const compiler = this.compiler;
- if (!compiler[LOGGER_FACTORY_COMPILER_KEY]) {
- compiler[LOGGER_FACTORY_COMPILER_KEY] = new Logger(this.compiler);
- }
- return compiler[LOGGER_FACTORY_COMPILER_KEY];
- }
- }
- /**
- * @function getLogger
- * @memberof module:hard-source-webpack-plugin/logger-factory~LoggerFactory.
- */
- LoggerFactory.getLogger = compilation => {
- while (compilation.compiler.parentCompilation) {
- compilation = compilation.compiler.parentCompilation;
- }
- return new LoggerFactory(compilation.compiler).create();
- };
- module.exports = LoggerFactory;
|