#! /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 fs = require('fs') const options = { env: 'development' } const configPath = path.resolve('backpack.config.js') let userConfig = {} if (fs.existsSync(configPath)) { const userConfigModule = require(configPath) userConfig = userConfigModule.default || userConfigModule } const serverConfig = userConfig.webpack ? userConfig.webpack(defaultConfig(options), options) : 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`)) nodemon({ script: serverPaths[0], watch: serverPaths, nodeArgs: process.argv.slice(2) }) .on('quit', process.exit) } const compileServer = () => serverCompiler.run(() => undefined) const startServerOnce = once(() => startServer()) const watcher = chokidar.watch([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() })