1

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}`);
});
StarLlama
  • 380
  • 2
  • 12
  • 1
    Angular uses WebPack for its builds. In Angular, you just do `ng build -prod`, then copy your app (now sans WebPack dependencies) from `./dist`. It sounds like your project is in NodeJS? Look [here](https://stackoverflow.com/questions/35054082/) or [here](https://www.valentinog.com/blog/webpack/). – paulsm4 May 31 '19 at 22:13
  • @paulsm4 This isn't angular. HMR is not exclusively associated with angular. It's Webpack. Webpack is used all over the place. – mwilson May 31 '19 at 22:34
  • I know that. I was just mentioning the one context I've used WebPack, hoping it might suggest something helpful to the OP. Q: Do you have any idea what the OP is using WebPack for (you assumed "expressJS", which implies "NodeJS")? Q: The OP has hard-coded webpack dependencies in his app. Can these be parameterized (and hence "eliminated" from a "production build")? Q: What about the other hard-coded dependencies, for the "middleware" and "hot module loading"? – paulsm4 Jun 01 '19 at 00:08

1 Answers1

1

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?

mwilson
  • 12,295
  • 7
  • 55
  • 95