Skip to content

Commit dc29e88

Browse files
committedFeb 4, 2018
feat: polish build output
1 parent 0c5db66 commit dc29e88

File tree

3 files changed

+83
-20
lines changed

3 files changed

+83
-20
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module.exports = function formatStats (stats, dir, api) {
2+
const fs = require('fs')
3+
const zlib = require('zlib')
4+
const chalk = require('chalk')
5+
const ui = require('cliui')({ width: 60 })
6+
7+
const json = stats.toJson({
8+
hash: false,
9+
modules: false,
10+
chunks: false
11+
})
12+
13+
let assets = json.assets
14+
? json.assets
15+
: json.children.reduce((acc, child) => acc.concat(child.assets), [])
16+
17+
const seenNames = new Map()
18+
const isJS = val => /\.js$/.test(val)
19+
const isCSS = val => /\.css$/.test(val)
20+
const isMinJS = val => /\.min\.js$/.test(val)
21+
assets = assets
22+
.filter(a => {
23+
if (seenNames.has(a.name)) {
24+
return false
25+
}
26+
seenNames.set(a.name, true)
27+
return isJS(a.name) || isCSS(a.name)
28+
})
29+
.sort((a, b) => {
30+
if (isJS(a.name) && isCSS(b.name)) return -1
31+
if (isCSS(a.name) && isJS(b.name)) return 1
32+
if (isMinJS(a.name) && !isMinJS(b.name)) return -1
33+
if (!isMinJS(a.name) && isMinJS(b.name)) return 1
34+
return b.size - a.size
35+
})
36+
37+
function formatSize (size) {
38+
return (size / 1024).toFixed(2) + ' kb'
39+
}
40+
41+
function getGzippedSize (asset) {
42+
const filepath = api.resolve(`dist/${asset.name}`)
43+
const buffer = fs.readFileSync(filepath)
44+
return formatSize(zlib.gzipSync(buffer).length)
45+
}
46+
47+
function makeRow (a, b, c) {
48+
return ` ${a}\t ${b}\t ${c}`
49+
}
50+
51+
ui.div(
52+
makeRow(
53+
chalk.cyan.bold(`File`),
54+
chalk.cyan.bold(`Size`),
55+
chalk.cyan.bold(`Gzipped`)
56+
) + `\n\n` +
57+
assets.map(a => makeRow(
58+
/js$/.test(a.name)
59+
? chalk.green(`${dir}/${a.name}`)
60+
: chalk.blue(`${dir}/${a.name}`),
61+
formatSize(a.size),
62+
getGzippedSize(a)
63+
)).join(`\n`)
64+
)
65+
66+
return `${ui.toString()}\n\n ${chalk.gray(`Images and other types of assets omitted.`)}\n`
67+
}

‎packages/@vue/cli-service/lib/commands/build/index.js

+15-20
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ module.exports = (api, options) => {
3333

3434
api.setMode(args.mode)
3535

36+
const fs = require('fs')
3637
const chalk = require('chalk')
3738
const rimraf = require('rimraf')
3839
const webpack = require('webpack')
40+
const formatStats = require('./formatStats')
3941
const {
4042
log,
4143
done,
@@ -79,31 +81,24 @@ module.exports = (api, options) => {
7981
return reject(err)
8082
}
8183

82-
if (!args.silent) {
83-
// TODO polish output
84-
process.stdout.write(stats.toString({
85-
hash: false,
86-
timings: false,
87-
colors: true,
88-
modules: false,
89-
children: api.hasPlugin('typescript') || args.target !== 'app',
90-
chunks: false,
91-
chunkModules: false
92-
}) + '\n\n')
93-
}
94-
9584
if (stats.hasErrors()) {
9685
return reject(`Build failed with errors.`)
9786
}
9887

99-
if (!args.silent && args.target === 'app') {
100-
done(`Build complete. The ${chalk.cyan(options.outputDir)} directory is ready to be deployed.\n`)
101-
if (options.baseUrl === '/') {
102-
info(`The app is built assuming that it will be deployed at the root of a domain.`)
103-
info(`If you intend to deploy it under a subpath, update the ${chalk.green('base')} option`)
104-
info(`in your project config (${chalk.cyan(`vue.config.js`)} or ${chalk.green('"vue"')} field in ${chalk.cyan(`package.json`)}).\n`)
88+
if (!args.silent) {
89+
log(formatStats(stats, options.outputDir, api))
90+
if (args.target === 'app') {
91+
done(`Build complete. The ${chalk.cyan(options.outputDir)} directory is ready to be deployed.\n`)
92+
if (
93+
options.baseUrl === '/' &&
94+
// only log the tips if this is the first build
95+
!fs.existsSync(api.resolve('node_modules/.cache'))
96+
) {
97+
info(`The app is built assuming that it will be deployed at the root of a domain.`)
98+
info(`If you intend to deploy it under a subpath, update the ${chalk.green('baseUrl')} option`)
99+
info(`in your project config (${chalk.cyan(`vue.config.js`)} or ${chalk.green('"vue"')} field in ${chalk.cyan(`package.json`)}).\n`)
100+
}
105101
}
106-
// TODO info(`You can view more deployment tips at ???`)
107102
}
108103

109104
// test-only signal

‎packages/@vue/cli-service/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"cache-loader": "^1.2.0",
3131
"case-sensitive-paths-webpack-plugin": "^2.1.1",
3232
"chalk": "^2.3.0",
33+
"cliui": "^4.0.0",
3334
"copy-webpack-plugin": "^4.3.1",
3435
"css-loader": "^0.28.9",
3536
"escape-string-regexp": "^1.0.5",

0 commit comments

Comments
 (0)