Skip to content

Instantly share code, notes, and snippets.

@aduth
Created September 20, 2016 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aduth/e4d2eaf11e04b56077d0ac229e44d7d8 to your computer and use it in GitHub Desktop.
Save aduth/e4d2eaf11e04b56077d0ac229e44d7d8 to your computer and use it in GitHub Desktop.
export default function( babel ) {
return {
visitors: {
CallExpression( path ) {
if ( 'MemberExpression' !== path.callee.type ||
'React' !== path.callee.object.name ||
'createElement' !== path.callee.property ||
path.arguments.length < 2 ||
'Identifier' !== path.arguments[ 0 ].type ||
'AsyncLoad' !== path.arguments[ 0 ].name ) {
return;
}
const requireProperty = path.arguments[ 1 ].properties.find( ( property ) => {
return 'require' === property.key.name;
} );
if ( ! requireProperty ) {
return;
}
const module = requireProperty.value.value;
requireProperty.value.replaceWithSourceString(
`function( callback ) {
require.ensure( '${ module }', function() {
callback( require( '${ module }' ) );
} );
}`
);
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment