@@ -43,7 +43,7 @@ export function getOxcParseAstAsync(): Promise<ParseAstAsync | null> {
4343 return oxcParseAstAsyncPromise ;
4444}
4545
46- // Keep this as a superset of JSX tag starts Babel can annotate , because a miss suppresses Babel fallback .
46+ // Keep this as a superset of all JSX tag starts, because a miss skips the file .
4747const JSX_TAG_START_REGEXP = / < [ $ _ \p{ ID_Start} ] [ $ _ \u200c \u200d \p{ ID_Continue} . : - ] * | < > / u;
4848const JSX_FILE_REGEXP = / \. [ j t ] s x $ / ;
4949
@@ -98,37 +98,26 @@ function getMagicString(
9898
9999async function annotateWithOxcParser (
100100 code : string ,
101- id : string ,
101+ idWithoutQueryAndHash : string ,
102102 ignoredComponents : string [ ] ,
103103 parseAstAsync : ParseAstAsync ,
104+ injectIntoHtml : boolean ,
104105 meta ?: ComponentAnnotationTransformMeta ,
105106) : Promise < ComponentAnnotationTransformResult > {
106- const idWithoutQueryAndHash = stripQueryAndHashFromPath ( id ) ;
107-
108- if (
109- ! idWithoutQueryAndHash ||
110- ! isAnnotationFile ( idWithoutQueryAndHash ) ||
111- ! shouldTryParse ( code ) ||
112- shouldSkipIncompatibleFile ( idWithoutQueryAndHash )
113- ) {
114- return null ;
115- }
116-
117107 const ast = await parseAstAsync ( code , {
118108 lang : idWithoutQueryAndHash . endsWith ( '.jsx' ) ? 'jsx' : 'tsx' ,
119109 } ) ;
120110
121- // Fall through to the Babel fallback (return undefined, not null) when the parser yields an
122- // unexpected result, mirroring the catch path — null would suppress Babel and drop annotations.
123111 if ( ! isAstNode ( ast ) ) {
124- return undefined ;
112+ return null ;
125113 }
126114
127115 const insertions = collectOxcComponentAnnotationInsertions (
128116 code ,
129117 ast ,
130118 ignoredComponents ,
131119 path . basename ( idWithoutQueryAndHash ) ,
120+ injectIntoHtml ,
132121 ) ;
133122
134123 if ( insertions . length === 0 ) {
@@ -147,8 +136,9 @@ async function annotateWithOxcParser(
147136
148137 return {
149138 code : magicString . toString ( ) ,
139+ // No `file`, because magic-string would then make `source` relative to it
140+ // and drop the directory from `sources`.
150141 map : magicString . generateMap ?.( {
151- file : id ,
152142 source : idWithoutQueryAndHash ,
153143 includeContent : true ,
154144 hires : true ,
@@ -159,6 +149,7 @@ async function annotateWithOxcParser(
159149export function createOxcComponentNameAnnotateHooks (
160150 ignoredComponents : string [ ] ,
161151 getParseAstAsync : ( ) => Promise < ParseAstAsync | null > ,
152+ injectIntoHtml = false ,
162153) : {
163154 transform (
164155 code : string ,
@@ -168,16 +159,35 @@ export function createOxcComponentNameAnnotateHooks(
168159} {
169160 return {
170161 async transform ( code , id , meta ) {
162+ const idWithoutQueryAndHash = stripQueryAndHashFromPath ( id ) ;
163+
164+ if (
165+ ! idWithoutQueryAndHash ||
166+ ! isAnnotationFile ( idWithoutQueryAndHash ) ||
167+ ! shouldTryParse ( code ) ||
168+ shouldSkipIncompatibleFile ( idWithoutQueryAndHash )
169+ ) {
170+ return null ;
171+ }
172+
171173 try {
172174 const parseAstAsync = await getParseAstAsync ( ) ;
173175
174176 if ( ! parseAstAsync ) {
175- return undefined ;
177+ return null ;
176178 }
177179
178- return await annotateWithOxcParser ( code , id , ignoredComponents , parseAstAsync , meta ) ;
180+ return await annotateWithOxcParser (
181+ code ,
182+ idWithoutQueryAndHash ,
183+ ignoredComponents ,
184+ parseAstAsync ,
185+ injectIntoHtml ,
186+ meta ,
187+ ) ;
179188 } catch {
180- return undefined ;
189+ // Leave files the parser cannot read unchanged. The bundler reports the syntax error.
190+ return null ;
181191 }
182192 } ,
183193 } ;
0 commit comments