UNPKG

1.58 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')
9
10const customConfig = require(path.resolve('backpack.config.js')) || {}
11const options = {
12 env: 'development',
13 paths: Object.assign({}, paths, customConfig.paths || {})
14}
15
16let serverConfig
17if (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
24process.on('SIGINT', process.exit)
25
26let serverCompiler
27
28const 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
37const compileServer = () => serverCompiler.run(() => undefined)
38
39const startServerOnce = once(() => startServer())
40
41const watcher = chokidar.watch([options.paths.serverSrcPath])
42
43watcher.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
52serverCompiler = webpack(serverConfig, (err, stats) => {
53 if (err) return
54 startServerOnce()
55})
56
57