1 | const path = require('path')
|
2 | const chalk = require('chalk')
|
3 | const textTable = require('text-table')
|
4 | const gzipSize = require('gzip-size')
|
5 | const formatWebpackMessages = require('@poi/dev-utils/formatWebpackMessages')
|
6 | const prettyBytes = require('@poi/dev-utils/prettyBytes')
|
7 | const logger = require('@poi/logger')
|
8 |
|
9 | class PrintStatusPlugin {
|
10 | constructor(opts = {}) {
|
11 | this.opts = opts
|
12 | }
|
13 |
|
14 | apply(compiler) {
|
15 | compiler.hooks.invalid.tap('show-rebuild-reason', file => {
|
16 | const d = new Date()
|
17 | logger.log(
|
18 | chalk.dim(
|
19 | `[${d.getHours()}:${d.getMinutes()}:${d.getSeconds()}] Rebuilding due to changes in ${file}`
|
20 | )
|
21 | )
|
22 | })
|
23 |
|
24 | compiler.hooks.done.tapPromise('print-status', async stats => {
|
25 | if (this.opts.clearConsole !== false && process.env.NODE_ENV !== 'test') {
|
26 | require('@poi/dev-utils/clearConsole')()
|
27 | }
|
28 | if (stats.hasErrors() || stats.hasWarnings()) {
|
29 | if (stats.hasErrors()) {
|
30 | process.exitCode = 1
|
31 | }
|
32 |
|
33 |
|
34 | const messages = formatWebpackMessages(stats.toJson())
|
35 | if (messages) {
|
36 | const { errors, warnings } = messages
|
37 | for (const error of errors) {
|
38 | console.error(error)
|
39 | }
|
40 | for (const warning of warnings) {
|
41 | console.error(warning)
|
42 | }
|
43 | }
|
44 |
|
45 | logger.debug(() =>
|
46 | stats.toString({
|
47 | colors: true
|
48 | })
|
49 | )
|
50 | } else {
|
51 | if (this.opts.printSucessMessage) {
|
52 | logger.done(
|
53 | `Build completed in ${stats.endTime - stats.startTime} ms`
|
54 | )
|
55 | }
|
56 |
|
57 | if (
|
58 | (this.opts.printFileStats || logger.options.debug) &&
|
59 | !process.env.CI &&
|
60 | process.stdout.isTTY
|
61 | ) {
|
62 | logger.log()
|
63 | const assets = await Promise.all(
|
64 | stats.toJson().assets.map(async asset => {
|
65 | asset.gzipped = await gzipSize(
|
66 | stats.compilation.assets[asset.name].source()
|
67 | )
|
68 | return asset
|
69 | })
|
70 | )
|
71 | const data = assets.map(asset => {
|
72 | const filename = path.relative(
|
73 | process.cwd(),
|
74 | path.join(compiler.options.output.path, asset.name)
|
75 | )
|
76 | return [
|
77 | path.join(
|
78 | path.dirname(filename),
|
79 | chalk.bold(path.basename(filename))
|
80 | ),
|
81 | chalk.green(prettyBytes(asset.size)),
|
82 | chalk.green(prettyBytes(asset.gzipped))
|
83 | ]
|
84 | })
|
85 | data.unshift([
|
86 | chalk.bold('file'),
|
87 | chalk.bold('size'),
|
88 | chalk.bold('gzipped')
|
89 | ])
|
90 | data.push([
|
91 | '(total)',
|
92 | chalk.green(
|
93 | prettyBytes(
|
94 | assets.reduce((result, asset) => result + asset.size, 0)
|
95 | )
|
96 | ),
|
97 | chalk.green(
|
98 | prettyBytes(
|
99 | assets.reduce((result, asset) => result + asset.gzipped, 0)
|
100 | )
|
101 | )
|
102 | ])
|
103 | logger.log(
|
104 | textTable(data, {
|
105 | stringLength: require('string-width')
|
106 | })
|
107 | )
|
108 | }
|
109 | }
|
110 | })
|
111 | }
|
112 | }
|
113 |
|
114 | module.exports = PrintStatusPlugin
|