javascriptwebpackhot-module-replacement

Do I remove webpack-dev-server and hot-module middleware code during production build, or when I get ready for production?


As the title suggests, when I'm ready to host the code for production, should I remove all usages of webpack-dev-middleware and webpack-hot-middleware from my server code as they are dev-dependencies? What's the best way to set this up so maybe I don't have to worry about this?

This is a snapshot of my server code:

// webpack -> HMR
const webpack = require("webpack");
const webpackConfig = require("../webpack.config");
const compiler = webpack(webpackConfig);

// webpack HMR init
app.use(
    require("webpack-dev-middleware")(compiler, {
        noInfo: false,
        publicPath: webpackConfig.output.publicPath,
    })
);
app.use(require("webpack-hot-middleware")(compiler));

...

app.get("/", async (req, res) => {
    const initialContent = await serverRender();
    res.render("index", {
        ...initialContent,
    });
});

app.listen(port, () => {
    console.log(`Express application listening on port ${port}`);
});

Solution

  • You should wrap your HMR code (or really, any development/environment specific setting) into it's own area. I wouldn't recommend taking it out of your code as you may come back to the application and want to update something. Having HMR is a pretty nice luxery, so I would just have you code sniff out the environment, and if it's development, run the associated code. Otherwise, don't run it.

    How do you detect the environment in an express.js app?