@automattic/babel-plugin-transform-wpcalypso-async

1.0.1 • Public • Published

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", { "async": true } ]
	]
}

See Babel options documentation for more information.

Transformations

asyncRequire will transform to one of:

  • dynamic import() if async plugin option is true
  • static require if async plugin option is false or unset
  • nothing (will be removed and no module will be imported) if the ignore plugin option is true

asyncRequire expects one required argument, with an optional callback:

asyncRequire( 'calypso/components/search', ( Search ) => {
	console.log( Search );
} );

<AsyncLoad /> will transform its require string prop to a function invoking asyncRequire when called.

// Before:

<AsyncLoad require="calypso/components/search" />;
// After:

<AsyncLoad
	require={ function ( callback ) {
		asyncRequire( 'calypso/components/search', callback );
	} }
/>;

Options

  • async - controls whether transformations applied by the plugin should use a dynamic ESM import statement that enables webpack code-splitting or the synchronous CommonJS require function. This defaults to false.
  • ignore - if set to true, the asyncRequire call will be completely removed, and AsyncLoad will 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.

Readme

Keywords

none

Package Sidebar

Install

npm i @automattic/babel-plugin-transform-wpcalypso-async

Weekly Downloads

3

Version

1.0.1

License

GPL-2.0-or-later

Unpacked Size

12.2 kB

Total Files

5

Last publish

Collaborators

  • porada
  • imranh920
  • gmjuhasz
  • rcarvalho
  • briowill
  • bgrgicak
  • dhenridev
  • daledupreez-a8c
  • jeherve
  • yuliyan
  • micbosia8c
  • jeremy.yip
  • wpvip-bot
  • etobiesen
  • kzoschke
  • brunobasto
  • kat3samsin
  • fmfernandes
  • newspack
  • robertsreberski_a8c
  • msurdi-a8c
  • chihsuan
  • manzoorwanijk
  • rjchow
  • andrea-sdl
  • scjr
  • spsiddarthan
  • t2dw4t
  • ehg_
  • wwa
  • sirreal
  • elazzabi
  • royho
  • luismulinari
  • macbre
  • mjangda
  • matticbot
  • a8c
  • blowery
  • noahtallen
  • hanifn
  • sgomes
  • tyxla
  • saroshaga
  • parkcityj
  • nejclovrencic
  • sirbrillig
  • chriszarate
  • robersongomes
  • johngodley