-
Notifications
You must be signed in to change notification settings - Fork 193
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
$RefreshReg$ is not defined with ChildCompiler usage #176
Comments
This is not really related to enabling/disabling this plugin - it's more about the Babel plugin. For example, if you intentionally exclude The solution to this would be to exclude your entire code path from being processed by |
Shouldn’t prerendering be done in production mode? |
From my understanding this is more of an "edge" case - this is using React as a "templating language" and injecting the created DOM tree into a template (kinda like how SSG works) during compilation to set default content of the I see a general form of this problem: there's some code within a repo yields a separate "branch" of execution (e.g. WebWorkers, Electron Prerender) controlled by some other plugin and that code is stored in close proximity with the other source code (since most of the time Babel configuration can be shared and will be shared). This idea fails when we apply |
It's partially prerendering. I basically just use Not sure if this needs to be fixed on the side of |
Yea I would agree that this is a limitation of I think the workaround I've highlighted above (to use separate Babel configs) is the best way for you to move forward. I'll add a section to the troubleshooting docs in #189 to talk about how to handle cases like this that introduces indirection. |
I'm not sure I know, how to do that. As |
Yes, I do understand the frustration. It is an unfortunate constraint. Though, I think you might be able to workaround this: window.$RefreshReg$ = () => {};
window.$RefreshSig$ = () => () => {}; It is quite a hack, but for your purposes (i.e. code paths where HMR does not make sense share files with code paths where HMR is useful) it would suffice. |
This actually seems quite a smart way of handling it! I don't have time to test it out, but |
Hi, yes it's undefined for any others scripts, and webpack doesn't include it to babel config. In other case it extend |
I'll test it later today/tomorrow and add this to the new troubleshooting guide if I don't hit any issues! |
Seems to have the same result for me :( |
Probably try to add some value to variable? This is mine env var |
It should be the same as Webpack automatically sets this to |
@donaldpipowitch I use fast refresh with webpack-devserver only. But as I see you want to build bundle with fast refresh, and use this bundle in some other server? |
Hmm, well I use |
By the way I'll try that now. In my demo it looks good: https://github.com/donaldpipowitch/react-refresh-webpack-plugin-RefreshSig-bug/pull/3/files |
I've preliminarily tested the |
@pmmmwh Latest storybook show nothing because of refresh babel plugin :( |
Hi, i it possible to provide a reproduction? Did you have the webpack plugin setup? What HMR solution does storybook use? |
@pmmmwh https://github.com/sshmyg/storybook-test this is a link to repo where you can reproduce this bug with storybook. |
Thanks |
hey this worked for me to handle this issue, follow this step by step 1.install cross-env
|
I think a lot of the solutions posted in this thread are actually not related to the problem the OP is facing - they are just how you want to setup the Babel plugin and hooking it into your specific build pipeline, rather than disabling stuff or patching stuff in a constrained Webpack Child Compiler. I'll be closing this cause since solutions I can think of have been listed in the Troubleshooting Guide, and the unrelated solutions on this matter will really confuse people coming later. If you still want to contribute to this issue/have a new way of dealing with it, feel free to open a PR against the guide and I will review it. |
We (me and Dan) had a thorough discussion with the Storybook maintainers themselves, and it seems that at this point in time it is quite difficult for them to support fast-refresh within the scope of storybook because their core story loader is framework agnostic and have custom HMR integration wired up. It also explicitly requires the default export (the storybook config) to be present to consume at runtime, which conflicts with how FR set up boundaries (cause the default will be an unnamed object). They plan to revisit this in the future, but for now you will have to disable the |
FYI: It looks like Storybook enabled fast refresh in a recent alpha. https://twitter.com/mshilman/status/1307722781774151681 Thank you @pmmmwh for your work on this topic. |
A possible solution to my original problem: use a separated webpack compilation inside a compilation. (https://github.com/donaldpipowitch/react-refresh-webpack-plugin-RefreshSig-bug/pull/4/files, original idea from bebraw: styleguidist/mini-html-webpack-plugin#39) |
Is it impossible that react-refresh/babel checks if the expected methods are defined, like every programmer does something similar at least once a day? If not log a warning to inform the user about sth missing, but don't cause an error? if (typeof window.$RefreshReg$ === 'undefined'){ |
The problem is - the expected methods are stubs. It is in place to allow overridable behaviour in any environment. It might be in a alternative JS environment where the It is meant to be low level and crude so frameworks and compilers can do whatever optimisation they want. |
OK. I understand. At least parts of it. Would it be possible to add a config switch like "callOverridableStubs" that defaults to off? |
What issue are you hitting specifically? For files that are configured to be parsed by the Babel plugin and this plugin, there shouldn't be any issues AFAIK? |
I've started a create react app and immediately ejected to get access to the configuration. They had included your plugin in 4.0 without me knowing anything about it. The source of the problem is that webpack configs aren't used just for one thing. I've e.g. a project where I'm using the webpack config to build a) the frontend b) the frontend for server side rendering c) a library for 3rd parties to include plugins of the frontend into their pages d) storybook to preview the components. Separating the webpack config into 4 configs would be an absolute no-go for me. When I'm developing a component in storybook it must work in the other environments a,b,c, too! So the webpack config is built in one place only. And referenced with minimal changes only (like setting libraryTarget or exchanging HtmlWebpackPlugin for Storybook) for the 4 cases. Storybook e.g. is recommending this way also in their docs: https://storybook.js.org/docs/react/configure/webpack#using-your-existing-config It's not only me. A google search for this error delivered many hits. That's a bigger problem for people out there. For now I'm filtering the plugin out at case b, c and d. (The other plugins etc don't crash your app, they just take a little bit longer than needed. But that's worth it for having a consistent build configuration) |
I don't think filtering out stuff is a problem when you share a Webpack config across multiple slightly varying setups? It is expected that some stuff don't work for some particular setups. The error thrown is unfortunate but it also indicates that some part of the integration is broken. In an ideal world it would be up to us the plugin to inject the transform for you, but since Babel does not provide such an API at the moment we couldn't to it. Also to reiterate, Side note - you would probably also want to enable this plugin and the Babel plugin when you SSR to gain the most benefit. |
I'm having this problem with worker-plugin/loader and I decided to use the workaround |
If you have more input feel free to PR them into https://github.com/pmmmwh/react-refresh-webpack-plugin/blob/main/docs/TROUBLESHOOTING.md |
I'm using
|
Storybook support fast refresh since 6.1 version ;)
|
I also had this bug. In module.exports = (api) => {
const reactPlugins = []
if (api.env('development')) {
reactPlugins.push('react-refresh/babel')
}
return {
presets: [
"@babel/preset-env",
"@babel/preset-react"
],
plugins: reactPlugins
}
} In export default function ({ development }) {
return {
...,
module: {
rules: [{
test: /\.js$/,
exclude: /node_modules/,
use: [{
loader: 'babel-loader',
options: {
envName: development ? 'development' : 'production'
}
}]
},
...
} If it throws if (process.env.BABEL_ENV === 'development') {
reactPlugins.push('react-refresh/babel');
} And then in
|
I had a somewhat relevant run-in with this "$RefreshReg$ is not defined" error, though my cause is not the same as the one OP mentions. I'm not sure where I should post this, but let me know if I should post this somewhere else. Just wanted to post in case someone else runs into this issue. options: {
getCustomTransformers: () => ({
before: [(process.env.environment !== "prod") && ReactRefreshTypeScript()].filter(Boolean),
}),
transpileOnly: isDevelopment,
}, to this: options: {
getCustomTransformers: () => ({
before: [(process.env.environment === "local") && ReactRefreshTypeScript()].filter(Boolean),
}),
transpileOnly: isDevelopment,
}, Since react refresh is only really useful for local development anyway, I think it's better to only include the plugin when on your local environment. I know the README checks to see if it is not the production env, but I feel like if you have multiple environments that you deploy to such as dev vs prod, then this plugin will be included when you try to build dev. |
This is completely unrelated to this issue ...
The problem isn't really running this in dev or prod or whatever, the README aims to demonstrate that you really should only run this when you INTENDED to. The problem is basically you enabling the Babel/TS transform while skipping this plugin - they have to be BOTH used or not used, or else you will hit this issue, which is intentional given that the transform is not intended to be used standalone. |
Yes, I should have made it more clear that I meant it was somewhat relevant only because of the "$RefreshReg$ is not defined" error and not at all related to the original child compiler issue originally posted. That is why I was unsure of where to post this, but thought here might be good since this is the only link that shows up when googling for "$RefreshReg$ is not defined". I realize now that there is a separate issue in this repo that goes over my issue exactly. Thank you for the explanation though. |
…ts are working properly. There was of course a monster bug since webpack is hell :). Read [this](pmmmwh/react-refresh-webpack-plugin#176).
@pmmmwh would it be possible to automatically inject |
hello, i check it out every solution, but for this problem in my case, this is the best: If you use react, just add this code in
and that's all, it's very simple and effective |
…efreshReg$ is not defined" #479 - Solution found here: pmmmwh/react-refresh-webpack-plugin#176 (comment)
This is a follow up to #36 (comment).
I created a demo where you can reproduce the error: https://github.com/donaldpipowitch/react-refresh-webpack-plugin-RefreshSig-bug
It looks to happen because of a child compiler. I use
html-webpack-plugin
to prerender a React component into anindex.html
file. (This will render some placeholder graphics in our real app until the app has loaded completely.) Normally the template you use forhtml-webpack-plugin
can use all the loaders you have configured for the rest of your source code which is quite convenient. But I guess we'd need to disableReactRefreshWebpackPlugin
for this somehow 🤔The text was updated successfully, but these errors were encountered: