UNPKG

1.54 kBPlain TextView Raw
1#! /usr/bin/env node
2const webpack = require('webpack')
3const chokidar = require('chokidar')
4const nodemon = require('nodemon')
5const paths = require('../config/paths')
6const once = require('ramda').once
7const defaultConfig = require('../config/webpack.config')
8const path = require('path')
9const fs = require('fs')
10
11const options = {
12 env: 'development'
13}
14
15const configPath = path.resolve('backpack.config.js')
16let userConfig = {}
17
18if (fs.existsSync(configPath)) {
19 const userConfigModule = require(configPath)
20 userConfig = userConfigModule.default || userConfigModule
21}
22
23const serverConfig = userConfig.webpack
24 ? userConfig.webpack(defaultConfig(options), options)
25 : defaultConfig(options)
26
27process.on('SIGINT', process.exit)
28
29let serverCompiler
30
31const 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
39const compileServer = () => serverCompiler.run(() => undefined)
40
41const startServerOnce = once(() => startServer())
42
43const watcher = chokidar.watch([paths.serverSrcPath])
44
45watcher.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
54serverCompiler = webpack(serverConfig, (err, stats) => {
55 if (err) return
56 startServerOnce()
57})
58
59