1 | #! /usr/bin/env node
|
2 | const webpack = require('webpack')
|
3 | const nodemon = require('nodemon')
|
4 | const paths = require('../config/paths')
|
5 | const once = require('ramda').once
|
6 | const defaultConfig = require('../config/webpack.config')
|
7 | const path = require('path')
|
8 | const fs = require('fs')
|
9 |
|
10 | const options = {
|
11 | env: 'development'
|
12 | }
|
13 |
|
14 | const configPath = path.resolve('backpack.config.js')
|
15 | let userConfig = {}
|
16 |
|
17 | if (fs.existsSync(configPath)) {
|
18 | const userConfigModule = require(configPath)
|
19 | userConfig = userConfigModule.default || userConfigModule
|
20 | }
|
21 |
|
22 | const serverConfig = userConfig.webpack
|
23 | ? userConfig.webpack(defaultConfig(options), options, webpack)
|
24 | : defaultConfig(options)
|
25 |
|
26 | process.on('SIGINT', process.exit)
|
27 |
|
28 | const serverCompiler = webpack(serverConfig)
|
29 |
|
30 | const startServer = () => {
|
31 | const serverPaths = Object
|
32 | .keys(serverCompiler.options.entry)
|
33 | .map(entry => path.join(serverCompiler.options.output.path, `${entry}.js`))
|
34 | nodemon({ script: serverPaths[0], watch: serverPaths, nodeArgs: process.argv.slice(2) })
|
35 | .on('quit', process.exit)
|
36 | }
|
37 |
|
38 | const startServerOnce = once((err, stats) => {
|
39 | if (err) return
|
40 | startServer()
|
41 | })
|
42 | serverCompiler.watch(serverConfig.watchOptions || {}, startServerOnce)
|