-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HMR for @nrwl/react #1881
Comments
Hi, sorry about this. This was mislabeled as stale. We are testing ways to mark not reproducible issues as stale so that we can focus on actionable items but our initial experiment was too broad and unintentionally labeled this issue as stale. |
So, as...
We have decided to wait until there is a webpack solution for React Fast Refresh solution for Webpack and we'll implement HMR for our React projects with that when it arrives |
Makes sense! Thx |
Hi @BigAB! Seems like webpack is now supported 😄 . Here is a copy-paste of the current state in https://github.com/gaearon/react-hot-loader
Thanks! |
Create react app 4.0 now also released with Fast Refresh using this plugin: pmmmwh/react-refresh-webpack-plugin#1 |
any update on this? |
@BigAB Wondering if there is any news here? This would be a great improvement for the nrwl react community 🙂 Thanks! |
Just a reminder, would be nice to have an answer here to know of the progress. |
@nemanjamilosavljevic-newtron actually we managed to integrate https://github.com/pmmmwh/react-refresh-webpack-plugin into our nx setup (we have few react based application). It is working great so far. We followed https://github.com/pmmmwh/react-refresh-webpack-plugin/tree/main/examples/webpack-dev-server example. Only thing that was needed is to upgrade webpack to at least So maybe there is no need to wait for this feature to land into NX, you can jump in right now. |
@capJavert Thanks for responding so quickly :) I'm new to NX so I hope you could help me with the following: |
Just adjust webpack dependency version in you package.json or add it through yarn add [email protected] |
Thank you @capJavert |
I have had a go to get HMR, but no luck. Stil compiles, but WDS only, no HMR. webpack.config.js is as below. I also added "react-refresh/babel" to .babelrc plugins list. No affect what so ever. I am lucky it still compiles, no idea of how babel/webpack works together. const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const webpack = require('webpack');
const isDevelopment = process.env.NODE_ENV !== 'production';
module.exports = (config, context) => {
return {
...config,
mode: isDevelopment ? 'development' : 'production',
module: {
...config.module,
rules: [
...config.module.rules,
// ... other rules
{
test: /\.([jt])sx?$/,
exclude: /node_modules/,
use: [
{
loader:
'@nrwl/web/src/utils/web-babel-loader',
options: {
rootMode: 'upward',
cwd: 'apps/myapp/src',
isModern: true,
envName: undefined,
babelrc: true,
cacheDirectory: true,
cacheCompression: false,
plugins: [
// ... other plugins
isDevelopment && require.resolve('react-refresh/babel'),
].filter(Boolean),
},
},
],
},
],
},
plugins: [
...config.plugins,
isDevelopment && new webpack.HotModuleReplacementPlugin(),
isDevelopment && new ReactRefreshWebpackPlugin(),
].filter(Boolean),
};
}; |
@rexebin you are probably missing // ...
webpackConfig.devServer = {
...webpackConfig.devServer,
hot: true
}
// ... More here: https://webpack.js.org/guides/hot-module-replacement/#enabling-hmr |
I got messages like: |
Problem hides in React version - React Fast Refresh works only with 16.9+ |
@SDemonUA React Fast Refresh is not yet ready for official use. I have it in another project on version 17.0.1 and it works when other pages were changed, but not when the index.ts gets changed. Even then, I have to jump-start the tracking by refreshing the page and, then it works. In case you want to have HMR, then you can easily disable the fast refresh to have it working. |
This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. |
Not stale, I'm waiting for this! 😄 |
I tried react-react-app with fast refresh and found myself constantly refreshing manually and eventually turn off fast refresh all together. It works ok most cases but requires manual refreshing in some cases. The thing is, this small chance of non-op is making me doubtful all the time. I do not want to waste time trying to figure out why something is not working as intended, then find out a manual refresh is all I needed. Some may not have this problem. Now I am back to NX with all the goodies Nx has to offer, feeling at home, not missing HMR, not a tiny bit. |
@rexebin I agree that the nagging doubt about whether refresh has happened is a pain with fast refresh. I'm using Snowpack and it works really well most of the time. There is an issue that changing a file with a React context provider often makes the context null for child components and the page then fails with an error, but I can live with that. For me the huge advantage is being able to work on something like a modal without having it disappear all the time due to a full page reload |
@jugglingcats working on a modal is really a good use case for fast refresh. I can tell when or if the fast refresh happens from the network tab. Maybe it is just me tiered of manual refresh just in case. No doubt Fast refresh is a good to have feature! |
@jugglingcats are you using Snowpack with NX? |
@rexebin yes and no. I have an nx monorepo that has shared libs and an aws lambda project, and in it there is a webapp that I run with snowpack by carefully crafting the snowpack config... NX doesn't really know anything about it |
@jugglingcats searching about snowpack and nx, found your nx-snowpack repo. From one of the post from @vsavkin, it was hinted adding Snowpack to nx project is trivial.. interesting but at the same time feel uncertain. |
@rexebin we are getting a bit off topic from the original post perhaps, but I am interested in the difference (if any) between Snowpack refresh (using native modules/ESM) and NX (webpack) fast refresh. I am using |
As mentioned by @jugglingcats, I think this thread has gotten slightly off-topic as well. |
Can anyone provide a minimal webpack override to enable fast refresh that works? |
Firstly you need to have minimal version of Minimal config would be (pretty much following install instructions at const getWebpackConfig = require('@nrwl/react/plugins/webpack')
const ReactRefreshPlugin = require('@pmmmwh/react-refresh-webpack-plugin')
function getCustomWebpackConfig(webpackConfig) {
const config = getWebpackConfig(webpackConfig)
const isProduction = webpackConfig.mode === 'production'
if (!isProduction) {
webpackConfig.devServer = {
...webpackConfig.devServer,
hot: true
}
config.plugins.push(new ReactRefreshPlugin())
}
return config
}
module.exports = getCustomWebpackConfig Also you need to add const commonPlugins = [/* your plugins here if you have any */]
module.exports = {
presets: ['@nrwl/react/babel'],
env: {
development: {
plugins: [...commonPlugins, 'react-refresh/babel']
},
production: {
plugins: [...commonPlugins]
}
}
} |
Thanks for the code example, @capJavert! @vsavkin, @FrozenPandaz could you please address this request by at least providing proper documentation on how to handle this use case? |
@nemonemi i tried with the custom webpack on a simple app: https://github.com/xiongemi/nx-react-counter-custom-webpack and it seems to work fine for me for nested components. when I update a nested component, it does not trigger page reload. In the console, it says HMR is updating the components.
This is what my custom webpack looks like: https://github.com/xiongemi/nx-react-counter-custom-webpack/pull/1/files Maybe disable the
|
Actually, I found what the problem was, and it was having multiple named exports in a component, which is a known behavior limit of the HMR library. |
I can confirm following the configuration showing at https://github.com/xiongemi/nx-react-counter-custom-webpack, I am able to get the HMR working in my Nx React Typescript project. Nice console logs clearly showing the update progress like below:
I need to install three packages to get it working:
|
Good to know, worth it to keep one export per file. |
Also, I found that no additional configuration of the .babelrc files is necessary. {
"presets": ["@nrwl/react/babel"],
"plugins": [
[
"styled-components",
{
"pure": true,
"ssr": true
}
]
]
} @vsavkin |
After two weeks of using the shining new HMR above, I am back to report that I am very happy with the solution. Most important of all is that nx tells me when exactly the app is hot loaded and up to date, making it better than A few things to note:
|
I'd like to add that I've tested the native HMR support by generating a new workspace with the latest version, and there it worked. At the moment I am unable to migrate my project to the latest version of NX because of some issues with Yarn2, but it is worth investigating if HMR is supported natively by the NX with the latest versions. p.s. I've checked the releases, and nowhere it is mentioned that they did anything in this direction, however, it works. |
the react fast fresh is added in or #5612, which is merged in https://github.com/nrwl/nx/releases/tag/12.3.5.
so when you run or you could just run serve command with hmr flag passed in like |
Closing this issue as it's now implemented in #5612 🎉 |
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context. |
How to enable HMR for @nrwl/react. Is it possible to achieve it without custom webpack.config.json? (Like in Angular)
There should be something in docs for HMR.
In workspace.json "hmr": true
Expected Behavior
module.hot should not be undefined
Current Behavior
HMR doesn´t work.
Context
The text was updated successfully, but these errors were encountered: