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