1- import { normalizePath } from "vite" ;
2- import { gen , parse , t , trav } from "./babel" ;
3- import type { types as Babel } from "@babel/core"
4- import type { ParseResult } from "@babel/parser"
5-
1+ import { normalizePath } from 'vite'
2+ import { gen , parse , t , trav } from './babel'
3+ import type { types as Babel } from '@babel/core'
4+ import type { ParseResult } from '@babel/parser'
65
76const transform = ( ast : ParseResult < Babel . File > , file : string ) => {
8- let didTransform = false ;
7+ let didTransform = false
98 trav ( ast , {
10- JSXOpeningElement ( path , ) {
11- const loc = path . node . loc ;
12- if ( ! loc ) return ;
13- const line = loc . start . line ;
14- const column = loc . start . column ;
9+ JSXOpeningElement ( path ) {
10+ const loc = path . node . loc
11+ if ( ! loc ) return
12+ const line = loc . start . line
13+ const column = loc . start . column
1514
1615 // Inject data-source as a string: "<file>:<line>:<column>"
1716 path . node . attributes . push (
1817 t . jsxAttribute (
1918 t . jsxIdentifier ( 'data-source' ) ,
20- t . stringLiteral ( `${ file } :${ line } :${ column } ` )
21- )
22- ) ;
19+ t . stringLiteral ( `${ file } :${ line } :${ column } ` ) ,
20+ ) ,
21+ )
2322
24- didTransform = true ;
25- }
23+ didTransform = true
24+ } ,
2625 } )
2726
2827 return didTransform
2928}
3029
31-
3230export function addSourceToJsx ( code : string , id : string ) {
33- const [ filePath ] = id . split ( "?" )
31+ const [ filePath ] = id . split ( '?' )
3432 // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
3533 const location = filePath ?. replace ( normalizePath ( process . cwd ( ) ) , '' ) !
3634
3735 try {
38- const ast = parse ( code , { sourceType : "module" , plugins : [ "jsx" , "typescript" ] } ) ;
36+ const ast = parse ( code , {
37+ sourceType : 'module' ,
38+ plugins : [ 'jsx' , 'typescript' ] ,
39+ } )
3940 const didTransform = transform ( ast , location )
4041 if ( ! didTransform ) {
4142 return { code }
4243 }
43- return gen ( ast , { sourceMaps : true , filename : id , sourceFileName : filePath } )
44+ return gen ( ast , {
45+ sourceMaps : true ,
46+ filename : id ,
47+ sourceFileName : filePath ,
48+ } )
4449 } catch ( e ) {
45-
4650 return { code }
4751 }
48- }
52+ }
0 commit comments