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 |
|
10 | const customConfig = require(path.resolve('backpack.config.js')) || {}
|
11 | const options = {
|
12 | env: 'development',
|
13 | paths: Object.assign({}, paths, customConfig.paths || {})
|
14 | }
|
15 |
|
16 | let serverConfig
|
17 | if (customConfig.webpack) {
|
18 | serverConfig = customConfig.webpack(defaultConfig(options), { env: 'development' })
|
19 | } else {
|
20 | console.log('no custom config file found')
|
21 | serverConfig = defaultConfig(options)
|
22 | }
|
23 |
|
24 | process.on('SIGINT', process.exit)
|
25 |
|
26 | let serverCompiler
|
27 |
|
28 | const startServer = () => {
|
29 | const serverPaths = Object
|
30 | .keys(serverCompiler.options.entry)
|
31 | .map(entry => path.join(serverCompiler.options.output.path, `${entry}.js`))
|
32 | const mainPath = path.join(serverCompiler.options.output.path, 'main.js')
|
33 | nodemon({ script: mainPath, watch: serverPaths, flags: [] })
|
34 | .on('quit', process.exit)
|
35 | }
|
36 |
|
37 | const compileServer = () => serverCompiler.run(() => undefined)
|
38 |
|
39 | const startServerOnce = once(() => startServer())
|
40 |
|
41 | const watcher = chokidar.watch([options.paths.serverSrcPath])
|
42 |
|
43 | watcher.on('ready', () => {
|
44 | watcher
|
45 | .on('add', compileServer)
|
46 | .on('addDir', compileServer)
|
47 | .on('change', compileServer)
|
48 | .on('unlink', compileServer)
|
49 | .on('unlinkDir', compileServer)
|
50 | })
|
51 |
|
52 | serverCompiler = webpack(serverConfig, (err, stats) => {
|
53 | if (err) return
|
54 | startServerOnce()
|
55 | })
|
56 |
|
57 |
|