-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
129 lines (108 loc) · 3.32 KB
/
build.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
#!/usr/bin/env node
'use strict'
const path = require('path')
const fs = require('fs')
const Getopt = require('node-getopt')
const ProgressPlugin = require('webpack/lib/ProgressPlugin')
const webpack = require('webpack')
const WebpackDevServer = require('webpack-dev-server')
const devProxyConfig = require('./devProxyConfig')
let getopt = new Getopt([
[ 'c', 'conf=', 'One config file. Can contain array of configs. Required.' ],
[ 'p', 'port=', 'The port to use. Defaults to 8080.' ],
[ 'h', 'host=', 'The host to use. Defaults to localhost.' ],
[ 'v', 'verbose' ]
])
.bindHelp()
.parseSystem()
if (!getopt.options.hasOwnProperty('conf')) {
getopt.showHelp()
process.exit(1)
}
let verbose = getopt.options.verbose
const baseDir = process.cwd()
const configPath = path.join(baseDir, getopt.options.conf)
// get the webpack.js file from the folder
try {
fs.accessSync(configPath)
} catch (e) {
if (e.code === 'ENOENT') {
throw new Error('Wrong filepath to webpack.js.')
}
}
let buildConfs = require(configPath)
buildConfs = buildConfs instanceof Array ? buildConfs : [buildConfs]
let i = 0
for (let buildConf of buildConfs) {
i++
let progress = {
start: function (id) {
let info = {
perc: 0,
msg: ''
}
this.interval = setInterval(() => console.log(`[${id}]: ${info.perc.toFixed(1)}% ${info.msg}`), 1000)
return new ProgressPlugin(function (perc, msg) {
info.perc = perc * 100
info.msg = msg
})
},
stop: function () {
clearInterval(this.interval)
}
}
if (buildConf.mode === 'development') {
// take out the devServer config
let serverConf = buildConf.devServer
delete buildConf.devServer
// configure dev proxy
serverConf.proxy = devProxyConfig(buildConf)
// take port out of arguments
const port = serverConf.port = parseInt(getopt.options.port) || 8080
// set proper public path
const host = serverConf.host = getopt.options.host || 'localhost'
buildConf.output.publicPath = `http://${host}:${port}/`
if (verbose) {
buildConf.devServer.stats = 'verbose'
console.log('build conf:')
console.log(JSON.stringify(buildConf, null, 2))
console.log('starting compile')
}
// start server
WebpackDevServer.addDevServerEntrypoints(buildConf, serverConf)
const compiler = webpack(buildConf)
const server = new WebpackDevServer(compiler, serverConf)
server.listen(port)
console.log(`Starting server on http://${host}:${port}/`)
} else if (buildConf.mode === 'production') {
// ---------------------------- prod ----------------------------
if (verbose) {
console.log('build conf:')
console.log(JSON.stringify(buildConf, null, 2))
console.log('starting compile')
}
// compile
let compiler = webpack(buildConf)
compiler.apply(progress.start(i))
compiler.run((err, stats) => {
progress.stop()
if (err) {
console.error(err.stack || err)
if (err.details) {
console.error(err.details)
}
return
}
const info = stats.toJson()
if (stats.hasErrors()) {
console.error(info.errors)
}
if (stats.hasWarnings()) {
console.warn(info.warnings)
}
})
} else {
console.error('build config without mode')
process.exit(1)
}
}