1 | #! /usr/bin/env node
|
2 | const webpack = require('webpack')
|
3 | const chokidar = require('chokidar')
|
4 | const nodemon = require('nodemon')
|
5 | const paths = require('../config/paths')
|
6 | const once = require('ramda').once
|
7 | const defaultConfig = require('../config/webpack.config')
|
8 | const path = require('path')
|
9 | const fs = require('fs')
|
10 |
|
11 | const options = {
|
12 | env: 'development'
|
13 | }
|
14 |
|
15 | const configPath = path.resolve('backpack.config.js')
|
16 | let userConfig = {}
|
17 |
|
18 | if (fs.existsSync(configPath)) {
|
19 | const userConfigModule = require(configPath)
|
20 | userConfig = userConfigModule.default || userConfigModule
|
21 | }
|
22 |
|
23 | const serverConfig = userConfig.webpack
|
24 | ? userConfig.webpack(defaultConfig(options), options)
|
25 | : defaultConfig(options)
|
26 |
|
27 | process.on('SIGINT', process.exit)
|
28 |
|
29 | let serverCompiler
|
30 |
|
31 | const startServer = () => {
|
32 | const serverPaths = Object
|
33 | .keys(serverCompiler.options.entry)
|
34 | .map(entry => path.join(serverCompiler.options.output.path, `${entry}.js`))
|
35 | nodemon({ script: serverPaths[0], watch: serverPaths, nodeArgs: process.argv.slice(2) })
|
36 | .on('quit', process.exit)
|
37 | }
|
38 |
|
39 | const compileServer = () => serverCompiler.run(() => undefined)
|
40 |
|
41 | const startServerOnce = once(() => startServer())
|
42 |
|
43 | const watcher = chokidar.watch([paths.serverSrcPath])
|
44 |
|
45 | watcher.on('ready', () => {
|
46 | watcher
|
47 | .on('add', compileServer)
|
48 | .on('addDir', compileServer)
|
49 | .on('change', compileServer)
|
50 | .on('unlink', compileServer)
|
51 | .on('unlinkDir', compileServer)
|
52 | })
|
53 |
|
54 | serverCompiler = webpack(serverConfig, (err, stats) => {
|
55 | if (err) return
|
56 | startServerOnce()
|
57 | })
|
58 |
|
59 |
|