forked from ai/size-limit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·153 lines (142 loc) · 3.96 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env node
'use strict'
const ciJobNumber = require('ci-job-number')
const readPkg = require('read-pkg-up')
const globby = require('globby')
const yargs = require('yargs')
const chalk = require('chalk')
const bytes = require('bytes')
const path = require('path')
const legacyApi = require('./legacy-api')
const getSize = require('.')
const argv = yargs
.usage('$0')
.option('why', {
alias: 'w',
describe: 'Show package content',
type: 'boolean'
})
.option('babili', {
type: 'boolean'
})
.version()
.help()
.alias('help', 'h')
.alias('version', 'v')
.epilog('Size Limit will read sizeLimit section from package.json.\n' +
'Configurtion example:\n' +
'\n' +
' "sizeLimit": [\n' +
' {\n' +
' "path": "index.js",\n' +
' "limit": "9 KB",\n' +
' "babili": true\n' +
' }\n' +
' ]')
.locale('en')
.argv
function ownError (msg) {
const error = new Error(msg)
error.sizeLimit = true
return error
}
function formatBytes (size) {
const format = bytes
.format(size, { unitSeparator: ' ' })
.replace('k', 'K')
return chalk.bold(format)
}
if (ciJobNumber() !== 1) {
process.stdout.write(
chalk.yellow('Size Limits run only on first CI job, to save CI resources'))
process.stdout.write('\n')
process.exit(0)
}
let getOptions
if (argv['_'].length === 0) {
getOptions = readPkg().then(result => {
if (!result.pkg) {
throw ownError(
'Can not find package.json. ' +
'Be sure that your run Size Limit inside project dir.'
)
} else if (!result.pkg.sizeLimit) {
throw ownError(
'Can not find sizeLimit section in package.json. ' +
'Add it according Size Limit docs.'
)
}
return Promise.all(result.pkg.sizeLimit.map(line => {
const cwd = path.dirname(result.path)
return globby(line.path, { cwd }).then(files => {
if (files.length === 0) {
files = line.path
if (typeof files === 'string') files = [files]
}
return {
bundle: result.pkg.name,
babili: line.babili,
limit: line.limit,
full: files.map(i => path.join(cwd, i)),
files
}
})
}))
})
} else {
getOptions = legacyApi(argv)
}
getOptions.then(files => {
return Promise.all(files.map(file => {
const opts = {
minifier: file.babili ? 'babili' : 'uglifyjs',
bundle: file.bundle
}
if (argv.why) {
opts.analyzer = process.env['NODE_ENV'] === 'test' ? 'static' : 'server'
}
return getSize(file.full, opts).then(size => ({
limit: bytes.parse(file.limit),
files: file.files,
size
}))
}))
}).then(files => {
process.stdout.write('\n')
let bad = false
for (const file of files) {
if (files.length > 1) {
process.stdout.write(chalk.gray(` ${ file.files }\n`))
}
if (file.limit && file.size <= file.limit) {
process.stdout.write(
` Package size: ${ chalk.green(formatBytes(file.size)) }\n` +
` Size limit: ${ formatBytes(file.limit) }\n`)
} else if (file.limit) {
process.stdout.write(
` ${ chalk.red('Package has exceeded the size limit') }\n` +
` Package size: ${ chalk.red(formatBytes(file.size)) }\n` +
` Size limit: ${ formatBytes(file.limit) }\n`)
bad = true
} else {
process.stdout.write(
` Package size: ${ formatBytes(file.size) }\n`)
}
}
process.stdout.write(
chalk.gray(' With all dependencies, minified and gzipped\n\n'))
if (bad) process.exit(3)
}).catch(e => {
let msg
if (e.sizeLimit) {
msg = e.message
} else if (e.message.indexOf('Module not found:') !== -1) {
const first = e.message.match(/Module not found:[^\n]*/)[0]
const filtered = first.replace('Module not found: Error: ', '')
msg = filtered
} else {
msg = e.stack
}
process.stderr.write(chalk.red(`${ msg }\n`))
process.exit(1)
})