Skip to content

Can save 1.2kb per bundle through tree shaking #9

Description

@kadamwhite

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions