@@ -46,20 +46,37 @@ export function recmaJsxBuild(options) {
4646 // When compiling to a function body, replace the import that was just
4747 // generated, and get `jsx`, `jsxs`, and `Fragment` from `arguments[0]`
4848 // instead.
49- if (
50- outputFormat === 'function-body' &&
51- tree . body [ 0 ] &&
52- tree . body [ 0 ] . type === 'ImportDeclaration' &&
53- typeof tree . body [ 0 ] . source . value === 'string' &&
54- / \/ j s x - ( d e v - ) ? r u n t i m e $ / . test ( tree . body [ 0 ] . source . value )
55- ) {
56- tree . body [ 0 ] = {
57- type : 'VariableDeclaration' ,
58- kind : 'const' ,
59- declarations : specifiersToDeclarations (
60- tree . body [ 0 ] . specifiers ,
61- toIdOrMemberExpression ( [ 'arguments' , 0 ] )
62- )
49+ if ( outputFormat === 'function-body' ) {
50+ let index = 0
51+
52+ // Skip directives: JS currently only has `use strict`, but Acorn allows
53+ // arbitrary ones.
54+ // Practically things like `use client` could be used?
55+ while ( index < tree . body . length ) {
56+ const child = tree . body [ index ]
57+ if ( 'directive' in child && child . directive ) {
58+ index ++
59+ } else {
60+ break
61+ }
62+ }
63+
64+ const declaration = tree . body [ index ]
65+
66+ if (
67+ declaration &&
68+ declaration . type === 'ImportDeclaration' &&
69+ typeof declaration . source . value === 'string' &&
70+ / \/ j s x - ( d e v - ) ? r u n t i m e $ / . test ( declaration . source . value )
71+ ) {
72+ tree . body [ index ] = {
73+ type : 'VariableDeclaration' ,
74+ kind : 'const' ,
75+ declarations : specifiersToDeclarations (
76+ declaration . specifiers ,
77+ toIdOrMemberExpression ( [ 'arguments' , 0 ] )
78+ )
79+ }
6380 }
6481 }
6582 }
0 commit comments