-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Gutenberg: Add a HMR & React Fast Refresh development mode. #23013
Conversation
Size Change: 0 B Total Size: 1.13 MB ℹ️ View Unchanged
|
@@ -0,0 +1,67 @@ | |||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any way we can bundle this in wp-scripts and offer support for third-party plugin devs too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely, I just wanted to stabilize it first.
Should I bundle it in in this PR or wait for more people who develop Gutenberg to use it and report potential issues?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, sounds like a reasonable plan to me.
cc @gziolo for awareness too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one. It’s super exciting to see that it can be integrated into Gutenberg 👍
@epiqueras Absolutely amazing!! I tested this locally, and it worked as expected!
|
I'm super excited about this, but I couldn't get it to work; I was getting "connection refused" errors on localhost:8081 so the editor couldn't load the file. I couldn't find any evidence that the port had already been allocated to another process either. There were some warnings related to exports from Reakit not being found, but I'm not sure if those would be preventing the server from starting up or if it could be something else. |
That's probably why. Delete node modules and reinstall. |
I've done that three times now, once via Here's the full set of errors…
|
I was looking for HMR in Gutenberg but sadly found nothing. This would make the development a lot better!
Changing the "dev:edit-site": "npm run build:packages && npm run hmr:on && concurrently \"wp-scripts start\" \"npm run dev:packages\" \"webpack-dev-server packages/edit-site/hot.js --config webpack.config.dev.js\"", seems to fix the problem above. |
Superseded by #26922 |
Description
Closes #23013.
Background
Webpack Hot Module Replacement (HMR) is fundamentally at odds with the way we do script overriding in Gutenberg.
After lots of experimentation and failed attempts, I found that the best way to support it is to provide an alternative development mode on a per-page/app basis. For example, the site editor is its own page/app, and this PR enables an HMR mode for its development.
It should be trivial to reuse the same configurations for the post, widgets, and navigation editors.
I also included the new React Fast Refresh experience. The developer experience with this is fantastic. Even error resilience and recovery are flawless, both for runtime and syntax errors. It's nice to be able to iterate on a screen without resetting state.
Implementation
This PR introduces a new Webpack config for running the new dev server as an extension of the one provided by
@wordpress/scripts
.Pages that wish to support an HMR mode, need to create a
hot.js
entry point like this PR does foredit-site
. This entry point should initialize the page and load its root stylesheet.On the server, we use a new config global,
GUTENBERG_HMR
, to determine whether to load the new script from the dev server or not. We manipulate this config variable effortlessly in the scripts introduced by this PR by usingwp-env
. It will enable it before running the new development mode, and disable it before running the old one. This makes it easy to switch between the two, but it does assume you are usingwp-env
to manage your environment.How to test this?
wp-env
if you haven't yet.npm run dev:edit-site
.Screenshots
Checklist: