You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Compared to the v0 line, in a wp-scripts build each block should be its own bundle. We can accomplish hot reloading with a wp-scripts start --hot build by adding this code to the block index.js file (see // Block HMR boilerplate comment and below):
the implementation of this hot-blocks module in our test project looks like this:
/** * Provide helper methods to dynamically reload & reregister blocks in hot- * reloading contexts. Inspired by "block-editor-hmr" NPM package. */import{unregisterBlockType}from'@wordpress/blocks';import{dispatch,select}from'@wordpress/data';/** * Deregister a block that's being hot-swapped out, so that the updated version * can be registered afresh without "block already registered" errors. * * Also keeps track of the selected block client ID, which is needed because * we have to deselect a block before unregistering it to swap without error. * The active block will be reselected in the refresh function. * * @param {string} hotBlockName Name of block being hot-reloaded. */exportconstderegister=(hotBlockName)=>(data)=>{unregisterBlockType(hotBlockName);constselectedBlockId=select('core/block-editor').getSelectedBlockClientId();if(selectedBlockId){dispatch('core/block-editor').clearSelectedBlock();}// Pass selected ID through hot-reload cycle.data.value=selectedBlockId;};/** * Process an updated block module, refreshing the editor view as needed. * * @param {string} hotBlockName Name of block being hot-reloaded. * @param {{value?: string}} [data] module.hot.data object, if present. */exportconstrefresh=(hotBlockName,data)=>{// Refresh all copies of our changed block by iteratively selecting them.select('core/block-editor').getBlocks().forEach(({ name, clientId })=>{if(name===hotBlockName){dispatch('core/block-editor').selectBlock(clientId);}});// Reselect whatever block was selected in the beginning.if(data?.value){// Reselect within a timeout to allow other hot-reloaded blocks to finish// processing before changing focus.setTimeout(()=>{dispatch('core/block-editor').selectBlock(data.value);});}};
This adapts our existing logic but lets it work efficiently in the context of a single block, and a world where WP Scripts does the directory scanning for us. I think this would be less overhead to maintain going forward, though it does require additional testing on different types of block and different selection states.
The text was updated successfully, but these errors were encountered:
Compared to the v0 line, in a wp-scripts build each block should be its own bundle. We can accomplish hot reloading with a
wp-scripts start --hot
build by adding this code to the blockindex.js
file (see// Block HMR boilerplate
comment and below):the implementation of this
hot-blocks
module in our test project looks like this:This adapts our existing logic but lets it work efficiently in the context of a single block, and a world where WP Scripts does the directory scanning for us. I think this would be less overhead to maintain going forward, though it does require additional testing on different types of block and different selection states.
The text was updated successfully, but these errors were encountered: