-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpackBuild.js
62 lines (55 loc) · 1.44 KB
/
webpackBuild.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
import ora from 'ora'
import webpack from 'webpack'
import {getPluginConfig, getUserConfig} from './config'
import createWebpackConfig from './createWebpackConfig'
import debug from './debug'
import {deepToString} from './utils'
import {logBuildResults} from './webpackUtils'
/**
* If you pass a non-falsy type, this will handle spinner display and output
* logging itself, otherwise use the stats provided in the callback.
*/
export default function webpackBuild(type, args, buildConfig, cb) {
if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'production'
}
let pluginConfig = getPluginConfig(args)
let userConfig
try {
userConfig = getUserConfig(args, {pluginConfig})
}
catch (e) {
return cb(e)
}
if (typeof buildConfig == 'function') {
buildConfig = buildConfig(args)
}
let webpackConfig
try {
webpackConfig = createWebpackConfig(buildConfig, pluginConfig, userConfig)
}
catch (e) {
return cb(e)
}
debug('webpack config: %s', deepToString(webpackConfig))
let spinner
if (type) {
spinner = ora(`Building ${type}`).start()
}
let compiler = webpack(webpackConfig)
compiler.run((err, stats) => {
if (err) {
if (spinner) {
spinner.fail()
}
return cb(err)
}
if (spinner || stats.hasErrors()) {
logBuildResults(stats, spinner)
}
if (stats.hasErrors()) {
return cb(new Error('Build failed with errors.'))
}
cb(null, stats)
})
}