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.
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:
to this:
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.