保誠-保戶業務員媒合平台
HelenHuang
2022-06-09 9bdb95c9e34cef640534e5e5a1e2225a80442000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
 * 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;