Calypso Async Babel Transform Plugin
babel-plugin-transform-wpcalypso-async is a Babel plugin to facilitate optional
code-splitting by applying transformations to a asyncRequire global function or the
<AsyncLoad /> React component.
Usage
Include in your Babel configuration as a plugin.
Example Babel config file:
{
"plugins": [ "@automattic/transform-wpcalypso-async" ]
}
See Babel options documentation for more information.
Transformations
asyncRequire will transform to one of:
- dynamic
import()if theignoreplugin option is nottrue - a rejected
Promisethat throws an error that the import is ignored, if theignoreplugin option istrue
asyncRequire expects one required argument, the name of the imported module.
asyncRequire( 'calypso/components/search' ).then( ( { default: Search } ) => {
console.log( Search );
} );
<AsyncLoad /> will transform its require string prop to a function invoking a dynamic import, or throwing an error when the ignore option is enabled.
// Before:
<AsyncLoad require="calypso/components/search" />;
// After:
const ref = () => import( 'calypso/components/search' );
<AsyncLoad require={ ref } />;
Options
ignore- if set totrue, theasyncRequirecall will be returning a rejectedPromise, andAsyncLoadwill show the placeholder forever and won't do any import. Useful for server side rendering where the render is one-pass and doesn't wait for any imports to finish.