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.
webpack-dev-middleware
(and potentiallywebpack-hot-middleware
) if you wanted to write your own custom development server. Unless there's a specific feature you want thatwebpack-dev-server
doesn't have, you should just use that. – Melo