-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpackUtils.js
170 lines (151 loc) · 4.62 KB
/
webpackUtils.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import path from 'path'
import chalk from 'chalk'
import figures from 'figures'
import filesize from 'filesize'
import {sync as gzipSize} from 'gzip-size'
const FRIENDLY_SYNTAX_ERROR_LABEL = 'Syntax error:'
let s = n => n === 1 ? '' : 's'
/**
* Create a banner comment for a UMD build file from package.json config.
*/
export function createBanner(pkg) {
let banner = `${pkg.name} v${pkg.version}`
if (pkg.homepage) {
banner += ` - ${pkg.homepage}`
}
if (pkg.license) {
banner += `\n${pkg.license} Licensed`
}
return banner
}
/**
* Create Webpack externals config from a module → global variable mapping.
*/
export function createExternals(externals = {}) {
return Object.keys(externals).reduce((webpackExternals, packageName) => {
let globalName = externals[packageName]
webpackExternals[packageName] = {
root: globalName,
commonjs2: packageName,
commonjs: packageName,
amd: packageName,
}
return webpackExternals
}, {})
}
function formatMessage(message) {
return message
// Make some common errors shorter:
.replace(
// Babel syntax error
'Module build failed: SyntaxError:',
FRIENDLY_SYNTAX_ERROR_LABEL
)
.replace(
// Webpack file not found error
/Module not found: Error: Cannot resolve 'file' or 'directory'/,
'Module not found:'
)
// Webpack loader names obscure CSS filenames
.replace(/^.*css-loader.*!/gm, '')
}
function isLikelyASyntaxError(message) {
return message.includes(FRIENDLY_SYNTAX_ERROR_LABEL)
}
function formatMessages(messages, type) {
return messages.map(
message => `${type} in ${formatMessage(message)}`
)
}
function getFileDetails(stats) {
let outputPath = stats.compilation.outputOptions.path
return Object.keys(stats.compilation.assets)
.filter(assetName => /\.(css|js)$/.test(assetName))
.map(assetName => {
let size = gzipSize(stats.compilation.assets[assetName].source())
return {
dir: path.dirname(path.join(path.relative(process.cwd(), outputPath), assetName)),
name: path.basename(assetName),
size,
sizeLabel: filesize(size),
}
})
}
export function logBuildResults(stats, spinner) {
if (stats.hasErrors()) {
if (spinner) {
spinner.fail()
console.log()
}
logErrorsAndWarnings(stats)
}
else if (stats.hasWarnings()) {
if (spinner) {
spinner.stopAndPersist({symbol: chalk.yellow(figures.warning)})
console.log()
}
logErrorsAndWarnings(stats)
console.log()
logGzippedFileSizes(stats)
}
else {
if (spinner) {
spinner.succeed()
console.log()
}
logGzippedFileSizes(stats)
}
}
export function logErrorsAndWarnings(stats) {
// Show fewer error details
let json = stats.toJson({}, true)
let formattedErrors = formatMessages(json.errors, chalk.bgRed.white(' ERROR '))
let formattedWarnings = formatMessages(json.warnings, chalk.bgYellow.black(' WARNING '))
if (stats.hasErrors()) {
let errors = formattedErrors.length
console.log(chalk.red(`Failed to compile with ${errors} error${s(errors)}.`))
if (formattedErrors.some(isLikelyASyntaxError)) {
// If there are any syntax errors, show just them.
// This prevents a confusing ESLint parsing error preceding a much more
// useful Babel syntax error.
formattedErrors = formattedErrors.filter(isLikelyASyntaxError)
}
formattedErrors.forEach(message => {
console.log()
console.log(message)
})
return
}
if (stats.hasWarnings()) {
let warnings = formattedWarnings.length
console.log(chalk.yellow(`Compiled with ${warnings} warning${s(warnings)}.`))
formattedWarnings.forEach(message => {
console.log()
console.log(message)
})
}
}
/**
* Take any number of Webpack Stats objects and log the gzipped size of the JS
* and CSS assets they contain, largest-first.
*/
export function logGzippedFileSizes(...stats) {
let files = stats.reduce((files, stats) => (files.concat(getFileDetails(stats))), [])
.filter(({name}) => !/^runtime\.[a-z\d]+\.js$/.test(name))
let longest = files.reduce((max, {dir, name}) => {
let length = (dir + name).length
return length > max ? length : max
}, 0)
let pad = (dir, name) => Array(longest - (dir + name).length + 1).join(' ')
console.log(`File size${s(files.length)} after gzip:`)
console.log()
files
.sort((a, b) => b.size - a.size)
.forEach(({dir, name, sizeLabel}) => {
console.log(
` ${chalk.dim(`${dir}${path.sep}`)}${chalk.cyan(name)}` +
` ${pad(dir, name)}${chalk.green(sizeLabel)}`
)
})
console.log()
}