forked from estrattonbailey/unibundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·149 lines (122 loc) · 3.92 KB
/
index.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
#! /usr/bin/env node
'use strict'
const pkg = require('./package.json')
const fs = require('fs-extra')
const path = require('path')
const chalk = require('chalk')
const webpack = require('webpack')
const devServer = require('webpack-dev-server')
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages')
const gzip = require('gzip-size')
const loading = require('loading-indicator')
const { _: args } = require('minimist')(process.argv.slice(2))
const userConfig = require('./lib/userConfig.js')
const clientConfig = require('./config/client.js')(args[0])
const serverConfig = require('./config/server.js')(args[0])
const clientCompiler = webpack(clientConfig)
const serverCompiler = webpack(serverConfig)
let loader = null
let clientComplete = false
let serverComplete = false
/**
* Empty build directory first so we don't have conflicting files
*/
fs.emptyDir(path.resolve(process.cwd(), userConfig.buildDir), () => {
if (args[0]) {
console.log(chalk.green(`unibundle`), chalk.gray('production'))
loader = loading.start()
clientCompiler.run((err, stats) => done('client', err, stats, true))
serverCompiler.run((err, stats) => done('server', err, stats, true))
} else {
console.log(chalk.green(`unibundle`), chalk.gray('development'))
clientCompiler.hooks.done.tap({ name: 'unibundle stats' }, stats => {
done('client', null, stats)
})
userConfig.css && fs.remove(path.resolve(userConfig.publicDir, userConfig.css.output.filename))
fs.remove(path.resolve(userConfig.publicDir, userConfig.client.output.filename))
const server = new devServer(clientCompiler, {
// hot: true, // breaks, never gets update
publicPath: '/',
headers: {
'Access-Control-Allow-Origin': '*',
},
noInfo: true,
clientLogLevel: 'none',
quiet: true
})
server.listen(8080, e => {
console.log(
chalk.green(`unibundle`),
chalk.gray('client bundle served at'),
`localhost:8080/${userConfig.client.output.filename}`
)
})
;['SIGINT', 'SIGTERM'].forEach(function(sig) {
process.on(sig, function() {
server.close()
process.exit()
})
})
serverCompiler.watch(
{ quiet: true },
(err, stats) => done('server', err, stats)
)
}
function done (which, err, stats, production) {
if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}
return;
}
const msgs = stats.toJson({}, true)
if (msgs.errors.length) {
console.log(chalk.red('\t', 'errors'))
console.log()
msgs.errors.map(e => {
console.log('\t', e.message || e)
console.log()
})
}
if (msgs.warnings.length) {
console.log(chalk.yellow('\t', 'warnings'))
console.log()
msgs.warnings.map(e => {
console.log('\t', e.message || e)
console.log()
})
}
loading.stop(loader)
if (msgs.errors.length || msgs.warnings.length) return
let size = (msgs.assets[0].size / 1024).toFixed(2) + ' kb'
if (production && which === 'client') {
const file = fs.readFileSync(
path.resolve(userConfig.publicDir, userConfig.client.output.filename)
).toString('utf8')
size = (gzip.sync(file) / 1024).toFixed(2) + ' kb gzipped'
} else if (production) {
size += ' minified'
}
console.log(
chalk.green(`unibundle`),
chalk.gray(which),
size
)
if (which === 'client') {
clientComplete = true
} else if (which === 'server') {
serverComplete = true
}
if (production && clientComplete && serverComplete) copy()
}
function copy () {
fs.copy(userConfig.publicDir, path.join(userConfig.buildDir, userConfig.publicDir), () => {
console.log(
chalk.green(`unibundle`),
chalk.gray('copy'),
`copied compiled files to /${userConfig.buildDir}`
)
})
}
})