Skip to content

Commit

Permalink
Framework: Remove async component loader from import source
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Oct 10, 2016
1 parent 73cf8db commit e279107
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@
*/
const kebabCase = require( 'lodash/kebabCase' );

/**
* Constants
*/
const RX_ASYNC_LOADER = /^async-component!/;

module.exports = ( babel ) => {
const t = babel.types;

return {
visitor: {
ImportDeclaration( path ) {
// This is a hacky solution to the issue where Mocha becomes
// confused by `async-component` Webpack loader, since we do
// not run modules through Webpack for tests.
if ( ! RX_ASYNC_LOADER.test( path.node.source.value ) ) {
return;
}

return path.replaceWith( t.importDeclaration(
path.node.specifiers,
t.stringLiteral( path.node.source.value.replace( RX_ASYNC_LOADER, '' ) )
) );
},
JSXAttribute( path ) {
// We only transform the require prop on AsyncLoad components.
// The component could have been imported under a different
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ describe( 'babel-plugin-transform-wpcalypso-async', () => {
} ).code;
}

describe( 'import foo from "async-component!foo";', () => {
it( 'should not transform other imports', () => {
const code = transform( 'import foo from "foo";' );

expect( code ).to.equal( 'import foo from "foo";' );
} );

it( 'should remove async-component loader for source', () => {
const code = transform( 'import foo from "async-component!foo";' );

expect( code ).to.equal( 'import foo from "foo";' );
} );
} );

describe( '<AsyncLoad require />', () => {
it( 'should not transform other components', () => {
const code = transform( '<div require="foo" />' );
Expand Down

0 comments on commit e279107

Please sign in to comment.