File tree 2 files changed +24
-1
lines changed
2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -216,7 +216,19 @@ export function buildJsx(tree, options) {
216
216
}
217
217
218
218
if ( specifiers . length > 0 ) {
219
- node . body . unshift ( {
219
+ let injectIndex = 0
220
+
221
+ while ( injectIndex < node . body . length ) {
222
+ const child = node . body [ injectIndex ]
223
+
224
+ if ( 'directive' in child && child . directive ) {
225
+ injectIndex ++
226
+ } else {
227
+ break
228
+ }
229
+ }
230
+
231
+ node . body . splice ( injectIndex , 0 , {
220
232
type : 'ImportDeclaration' ,
221
233
specifiers,
222
234
source : {
Original file line number Diff line number Diff line change @@ -1647,6 +1647,17 @@ test('estree-util-build-jsx', async function (t) {
1647
1647
assert . equal ( generate ( tree ) , 'React.createElement("a");\n' )
1648
1648
}
1649
1649
)
1650
+
1651
+ await t . test ( 'should keep directives first' , function ( ) {
1652
+ const tree = parse ( '"use client"\nconst x = <a/>' )
1653
+
1654
+ buildJsx ( tree , { runtime : 'automatic' } )
1655
+
1656
+ assert . equal (
1657
+ generate ( tree ) ,
1658
+ '"use client";\nimport {jsx as _jsx} from "react/jsx-runtime";\nconst x = _jsx("a", {});\n'
1659
+ )
1660
+ } )
1650
1661
} )
1651
1662
1652
1663
/**
You can’t perform that action at this time.
0 commit comments