1 | 'use strict';
|
2 | const chalk = require('chalk');
|
3 |
|
4 | class Logger {
|
5 | constructor(option = {}, builder) {
|
6 | this.option = option;
|
7 | this.builder = builder;
|
8 | this.name = this.builder.constructor.name;
|
9 | }
|
10 |
|
11 | setOption(option) {
|
12 | this.option = option;
|
13 | }
|
14 |
|
15 | info(info, tag = '') {
|
16 | console.log(chalk.blue(`[${this.name}] ${tag}:`), typeof info === 'object' ? info : chalk.green(info));
|
17 | }
|
18 |
|
19 | print(tag, info) {
|
20 | if (this.option.enable === undefined || this.option.enable) {
|
21 | this.info(info, tag);
|
22 | }
|
23 | }
|
24 |
|
25 | cost() {
|
26 | if (this.builder.config.cost) {
|
27 | this.info(`------ init cost: ${this.builder.t2 - this.builder.t1}, init create cost: ${this.builder.t3 - this.builder.t2}, create cost: ${this.builder.t4 - this.builder.t3} -------, create total cost: ${this.builder.t4 - this.builder.t1} -------\r\n`, 'cost');
|
28 | }
|
29 | }
|
30 |
|
31 | debug(webpackConfig) {
|
32 | this.env();
|
33 | this.config();
|
34 | this.keyword();
|
35 | this.options();
|
36 | this.all(webpackConfig);
|
37 | }
|
38 |
|
39 | line(tag, info) {
|
40 | this.lineStart(tag);
|
41 | this.print(tag, info);
|
42 | this.lineEnd(tag);
|
43 | }
|
44 |
|
45 | lineStart(tag) {
|
46 | this.print(tag, '-------------------start-------------------------');
|
47 | }
|
48 |
|
49 | lineEnd(tag) {
|
50 | this.print(tag, '-------------------end-------------------------');
|
51 | }
|
52 |
|
53 | env() {
|
54 | if (this.option.env) {
|
55 | this.info(`------ NODE_ENV: ${process.env.NODE_ENV}, BUILD_ENV: ${process.env.BUILD_ENV}, UPLOAD_CDN: ${process.env.UPLOAD_CDN} -------`, 'env');
|
56 | }
|
57 | }
|
58 |
|
59 | all(info) {
|
60 | if (this.option.all) {
|
61 | this.lineStart('all');
|
62 | if (info) {
|
63 | this.print('all', info);
|
64 | } else {
|
65 | this.config();
|
66 | this.keyword();
|
67 | this.loader();
|
68 | this.plugin();
|
69 | }
|
70 | this.lineEnd('all');
|
71 | }
|
72 | }
|
73 |
|
74 | config() {
|
75 | if (this.option.config) {
|
76 | this.print('config', this.builder.config);
|
77 | }
|
78 | }
|
79 |
|
80 | keyword() {
|
81 | if (this.option.keyword) {
|
82 | this.print('env', this.builder.env);
|
83 | this.print('extractCss', this.builder.extractCss);
|
84 | this.print('buildPath', this.builder.buildPath);
|
85 | this.print('publicPath', this.builder.publicPath);
|
86 | this.print('filename', this.builder.filename);
|
87 | this.print('imageName', this.builder.imageName);
|
88 | this.print('cssName', this.builder.cssName);
|
89 | }
|
90 | }
|
91 |
|
92 | options(options) {
|
93 | if (this.option.options) {
|
94 | this.print('options', options || this.builder.options);
|
95 | }
|
96 | }
|
97 |
|
98 | loader(loaders) {
|
99 | if (this.option.loader) {
|
100 | this.line('loaders', loaders);
|
101 | }
|
102 | }
|
103 |
|
104 | plugin(plugins) {
|
105 | if (this.option.plugin) {
|
106 | this.line('plugins', plugins);
|
107 | }
|
108 | }
|
109 | }
|
110 |
|
111 | module.exports = Logger;
|