|
| 1 | +// @flow |
| 2 | +import type { $Application } from "express"; |
| 3 | + |
| 4 | +const _ = require("lodash"); |
| 5 | +const webpack = require("webpack"); |
| 6 | +const webpackDevMiddleware = require("webpack-dev-middleware"); |
| 7 | +const webpackHotMiddleware = require("webpack-hot-middleware"); |
| 8 | +const ProgressBarPlugin = require("progress-bar-webpack-plugin"); |
| 9 | + |
| 10 | +function devMode(app: $Application, config: Object) { |
| 11 | + const entry = _.reduce( |
| 12 | + config.entry, |
| 13 | + (m: Object, v, k: string) => { |
| 14 | + m[k] = [ |
| 15 | + require.resolve("babel-polyfill"), |
| 16 | + require.resolve("react-hot-loader/patch"), |
| 17 | + require.resolve("webpack-hot-middleware/client"), |
| 18 | + v |
| 19 | + ]; |
| 20 | + return m; |
| 21 | + }, |
| 22 | + {} |
| 23 | + ); |
| 24 | + |
| 25 | + const plugins = [ |
| 26 | + new webpack.HotModuleReplacementPlugin(), |
| 27 | + ...config.plugins, |
| 28 | + new webpack.NamedModulesPlugin(), |
| 29 | + new ProgressBarPlugin({ clear: false }), |
| 30 | + new webpack.NoEmitOnErrorsPlugin() |
| 31 | + ]; |
| 32 | + |
| 33 | + const compiler = webpack({ ...config, entry, plugins }); |
| 34 | + |
| 35 | + app.use( |
| 36 | + webpackDevMiddleware(compiler, { |
| 37 | + hot: true, |
| 38 | + quiet: false, |
| 39 | + overlay: false, |
| 40 | + noInfo: false, |
| 41 | + lazy: false, |
| 42 | + clientLogLevel: "none", |
| 43 | + watchContentBase: true, |
| 44 | + stats: { colors: true }, |
| 45 | + watchOptions: { |
| 46 | + ignored: /node_modules/ |
| 47 | + }, |
| 48 | + historyApiFallback: { |
| 49 | + disableDotRule: true |
| 50 | + }, |
| 51 | + |
| 52 | + headers: { "Access-Control-Allow-Origin": "http://localhost" }, |
| 53 | + publicPath: config.output.publicPath |
| 54 | + }) |
| 55 | + ); |
| 56 | + app.use(webpackHotMiddleware(compiler)); |
| 57 | +} |
| 58 | + |
| 59 | +module.exports = devMode; |
0 commit comments