#! /usr/bin/env node const webpack = require('webpack') const chokidar = require('chokidar') const nodemon = require('nodemon') const paths = require('../config/paths') const once = require('ramda').once const defaultConfig = require('../config/webpack.config') const path = require('path') const customConfig = require(path.resolve('backpack.config.js')) || {} const options = { env: 'development', paths: Object.assign({}, paths, customConfig.paths || {}) } let serverConfig if (customConfig.webpack) { serverConfig = customConfig.webpack(defaultConfig(options), { env: 'development' }) } else { console.log('no custom config file found') serverConfig = defaultConfig(options) } process.on('SIGINT', process.exit) let serverCompiler const startServer = () => { const serverPaths = Object .keys(serverCompiler.options.entry) .map(entry => path.join(serverCompiler.options.output.path, `${entry}.js`)) const mainPath = path.join(serverCompiler.options.output.path, 'main.js') nodemon({ script: mainPath, watch: serverPaths, flags: [] }) .on('quit', process.exit) } const compileServer = () => serverCompiler.run(() => undefined) const startServerOnce = once(() => startServer()) const watcher = chokidar.watch([options.paths.serverSrcPath]) watcher.on('ready', () => { watcher .on('add', compileServer) .on('addDir', compileServer) .on('change', compileServer) .on('unlink', compileServer) .on('unlinkDir', compileServer) }) serverCompiler = webpack(serverConfig, (err, stats) => { if (err) return startServerOnce() })