File size: 2,735 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const { kebabCase } = require( 'lodash' );

function importChunk( t, name ) {
	const chunkName = 'async-load-' + kebabCase( name );

	const argumentWithMagicComments = t.addComment(
		t.stringLiteral( name ),
		'leading',
		`webpackChunkName: "${ chunkName }"`,
		false
	);

	return t.callExpression( t.import(), [ argumentWithMagicComments ] );
}

function importError( t, name ) {
	const chunkName = 'async-load-' + kebabCase( name );

	return t.newExpression( t.identifier( 'Error' ), [
		t.stringLiteral( 'ignoring load of: ' + chunkName ),
	] );
}

module.exports = ( { types: t } ) => {
	/**
	 * Nested visitor for `require` function expression hoisting. This is
	 * assigned here as a shared reference for optimized path traversal.
	 * @see https://github.com/thejameskyle/babel-handbook/blob/HEAD/translations/en/plugin-handbook.md#optimizing-nested-visitors
	 * @type {Object}
	 */
	const asyncAttributeVisitor = {
		ArrowFunctionExpression( path ) {
			// Hoist using the parent JSXAttribute's scope, since the scopes
			// from AST parse stage are not valid for replacement expression
			path.hoist( this.scope );
		},
	};

	return {
		visitor: {
			JSXAttribute( path, state ) {
				// We only transform the require prop on AsyncLoad components.
				// The component could have been imported under a different
				// name, but tracking the identifier to the import would add
				// complexity to the parsing. In other words, I'm lazy.
				const parent = path.parentPath.parent;
				if ( 'AsyncLoad' !== parent.openingElement.name.name ) {
					return;
				}

				const name = path.node.name;
				if ( 'JSXIdentifier' !== name.type || 'require' !== name.name ) {
					return;
				}

				const value = path.node.value;
				if ( 'StringLiteral' !== value.type ) {
					return;
				}

				const body = state.opts.ignore
					? t.blockStatement( [ t.throwStatement( importError( t, value.value ) ) ] )
					: importChunk( t, value.value );

				path.replaceWith(
					t.jSXAttribute( name, t.jSXExpressionContainer( t.arrowFunctionExpression( [], body ) ) )
				);

				// Traverse replacement attribute to hoist function expression
				path.traverse( asyncAttributeVisitor, { scope: path.scope } );
			},
			CallExpression( path, state ) {
				if ( 'asyncRequire' !== path.node.callee.name ) {
					return;
				}

				const argument = path.node.arguments[ 0 ];
				if ( ! argument || 'StringLiteral' !== argument.type ) {
					return;
				}

				const expr = state.opts.ignore
					? t.callExpression(
							t.memberExpression( t.identifier( 'Promise' ), t.identifier( 'reject' ) ),
							[ importError( t, argument.value ) ]
					  )
					: importChunk( t, argument.value );

				path.replaceWith( expr );
			},
		},
	};
};