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