Open
Description
When working on a single-file bundle, we identified today that the HMR scaffolding cost about 1.19kb which remains in the production build if following the README verbatim. None of the HMR code is needed in a non-hotloading environment, so switching from this:
import { autoloadBlocks } from '@humanmade/webpack-helpers/hmr';
autoloadBlocks(
{
getContext: () => require.context( './', false, /block.js/ ),
},
( context, loadModules ) => {
if ( module.hot ) {
module.hot.accept( context.id, loadModules );
}
}
);to this:
import { registerBlockType } from '@wordpress/blocks';
import blockSettings from './block';
if ( ! module.hot ) {
registerBlockType( blockSettings );
} else {
require( '@humanmade/webpack-helpers/hmr' ).autoloadBlocks(
{
getContext: () => require.context( './', false, /block.js/ ),
},
( context, loadModules ) => {
if ( module.hot ) {
module.hot.accept( context.id, loadModules );
}
}
);
}results (in our test app) in 1.19kb less code per output bundle. Particularly when setting up a plugin which registers individual scripts per-block, that dead weight code will add up.
The example above only works with a single file, but we could potentially expose different top-level functions in hot mode versus normal mode to achieve the same result for multi-block bundles.
The core library could possibly be changed to alter what gets output in a non-HMR space.
Metadata
Metadata
Assignees
Labels
No labels
Activity
kadamwhite commentedon Apr 13, 2023
The best boilerplate I've been able to slim down using the library as-is:
block-hmr.jsEach block file
See