RFC: Hot Module Replacement
nicolassenechal opened this issue · comments
Summary
Enables HMR, both for ssr and in the front-end
Motivation
HMR is useful for improving development experience and speed, avoiding to have to rebuild the project manually systematically
Detailed design
The design is based on this article: https://hackernoon.com/hot-reload-all-the-things-ec0fed8ab0
A basic and incomplete implementation can be found at: https://github.com/seek-oss/sku/tree/hmr-with-dev-server
Taks
- Update
webpack.config
to support hmr when in dev mode
- extra entries
- output.publicPath for client side hmr
- externals for server-side rendering
- extra plugins
- Update
babel.config
to support hmr (might not be required) - Refactor
start.js
andbuild.js
to be more re-usable
Usage
- The user calls
sku start
- If the config is of type
render
,start.js
creates the webpack devserver with hmr support - If the config is of type
server
,start.js
creates the webpack devserver with hmr support and build the server-side bundle
API changes
Two port numbers are now required when using server-side rendering. I would keep the current port
option, but allow an array to be passed in. The first value would user by the server-side rendering, the second by the client-side hmr.
How We Teach This
We should point to general documentation about hmr
Drawbacks
webpack.config
is going to become very complex, with test over dev/prod, static/ssr- that functionality is react-centric and relies on
react-hot-loader
Alternatives
I looked at using middleware, instead of webpack dev server. The solution requires a lot more rewriting (branch here: https://github.com/seek-oss/sku/tree/hmr)
Unresolved questions
Initial tests showed some potential issues with async/await. Will need to investigate more
To make the port setup more self-documenting, I propose that the sku option port be either a number or an object of the form {backend:int, client:int}.
Static rendering would only use the backend attribute in dev mode, and ignore the client attribute all the time.
Server-side rendering would use the backend attribute in both dev and prod, and the client attribute only in dev mode.