forked from oslabs-beta/Reactrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev.js
54 lines (50 loc) · 1.37 KB
/
dev.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
const spawn = require('cross-spawn')
const path = require('path')
const webpack = require('webpack')
const webpackConfigClient = require('./webpack.config.client')
const webpackConfigServer = require('./webpack.config.server')
//bundle the two different configurations and alter them to development mode
const compiler = webpack([
{
...webpackConfigClient,
mode: 'development',
devtool: 'source-map',
output: {
...webpackConfigClient.output,
filename: '[name].js',
},
},
{
...webpackConfigServer,
mode: 'development',
devtool: 'source-map',
},
])
let node
//state which bundle is compiling and if the server is already running, kill it
compiler.hooks.watchRun.tap('Dev', (compiler) => {
console.log(`Compiling ${compiler.name} ...`)
if (compiler.name === 'server' && node) {
node.kill()
node = undefined
}
})
//watches or detects changes in source code and automatically re-compiles and restarts server with the changes
compiler.watch({}, (err, stats) => {
if (err) {
console.error(err)
process.exit(1)
}
console.log(stats?.toString('minimal'))
const compiledSuccessfully = !stats?.hasErrors()
if (compiledSuccessfully && !node) {
console.log('Starting Node.js ...')
node = spawn(
'node',
['--inspect', path.join(__dirname, 'dist/main.js')],
{
stdio: 'inherit',
}
)
}
})