Webpack vs webpack-dev-server vs webpack-dev-middl

2019-03-07 14:23发布

问题:

I'm starting working with webpack with a node/express environment developing a ReactJS server side rendered application with react-router. I'm getting very confused about the role of each webpack package for dev and prod (runtime) environments.

Here is the summary of my understanding:

webpack: Is a package, a tool to join together different pieces of an web application and bundle then in a single .js file (normally bundle.js). The result file is then served in a prod environment to be loaded by the application and contains all necessary components to run the code. Features include shrinking code, minifying, etc.

webpack-dev-server: Is a package that offers a server to process the website files. It also builds a single .js file (bundle.js) from client components but serves it in memory. It also has the option (-hot) to monitor all the building files and build a new bundle in memory in case of code changes. The server is served directly in the browser (ex: http:/localhost:8080/webpack-dev-server/whatever). The combination of in memory loading, hot processing and browser serving let the user get the application updated on browser when the code changes, ideal for development environment.

If I have doubts about the above text, I'm really not sure about the content below, so please advise me if necessary

A common problem when using webpack-dev-server with node/express is that webpack-dev-server is a server, as is node/express. That makes this environment tricky to run both the client and some node/express code (an API etc.). NOTE: This is what I've faced but would be great to understand why does that happens in more details...

webpack-dev-middleware: This is a middleware with same functions of webpack-dev-server (inmemory bundling, hot reloading), but in format that can be injected to the server/express application. In that way, you have a sort of server (the webpack-dev-server) insider the node server. Oops: Is this a crazy dream ??? How can this piece solve the dev and prod equation and makes life simpler

webpack-hot-middleware: This... Stuck here... found this piece when looking for webpack-dev-middleware... No idea how to use it.

ENDNOTE: Sorry is there is any wrong thinking. I really need help in order to undestand these variants in a complex environment. If conveninent, please add more packages/data that will build the whole scenario.

回答1:

webpack

As you've described, Webpack is a module bundler, it bundles various module formats primarily so they can be run in a browser. It offers both a CLI and Node API.

webpack-dev-middleware

Webpack Dev Middleware is middleware which can be mounted in an express server to serve the latest compilation of your bundle during development. This uses webpack's Node API in watch mode and instead of outputting to the file system it outputs to memory.

For comparison, you might use something like express.static instead of this middleware in production.

webpack-dev-server

Webpack Dev Server is itself an express server which uses webpack-dev-middleware to serve the latest bundle and additionally handles hot module replacement (HMR) requests for live module updates in the client.

webpack-hot-middleware

Webpack Hot Middleware is an alternative to webpack-dev-server but instead of starting a server itself it allows you to mount it in an existing / custom express server alongside webpack-dev-middleware.

Also...

webpack-hot-server-middleware

Just to confuse things even more, there's also Webpack Hot Server Middleware which is designed to be used alongside webpack-dev-middleware and webpack-hot-middleware to handle hot module replacement of server rendered apps.



回答2:

As of update in 2018 and considering the webpack-dev-server note on its official GitHub page:

Project in Maintenance Please note that webpack-dev-server is presently in a maintenance-only mode and will not be accepting any additional features in the near term. Most new feature requests can be accomplished with Express middleware; please look into using the before and after hooks in the documentation.

What would be the natural choice to build a webpack HMR ?