11import { normalizePath } from 'vite'
22import { gen , parse , t , trav } from './babel'
3+ import { matcher } from './matcher'
34import type { types as Babel , NodePath } from '@babel/core'
45import type { ParseResult } from '@babel/parser'
56
@@ -111,14 +112,19 @@ const transformJSX = (
111112 element : NodePath < t . JSXOpeningElement > ,
112113 propsName : string | null ,
113114 file : string ,
115+ ignorePatterns : Array < string | RegExp > ,
114116) => {
115117 const loc = element . node . loc
116118 if ( ! loc ) return
117119 const line = loc . start . line
118120 const column = loc . start . column
119121 const nameOfElement = getNameOfElement ( element . node . name )
120-
121- if ( nameOfElement === 'Fragment' || nameOfElement === 'React.Fragment' ) {
122+ const isIgnored = matcher ( ignorePatterns , nameOfElement )
123+ if (
124+ nameOfElement === 'Fragment' ||
125+ nameOfElement === 'React.Fragment' ||
126+ isIgnored
127+ ) {
122128 return
123129 }
124130 const hasDataSource = element . node . attributes . some (
@@ -151,7 +157,11 @@ const transformJSX = (
151157 return true
152158}
153159
154- const transform = ( ast : ParseResult < Babel . File > , file : string ) => {
160+ const transform = (
161+ ast : ParseResult < Babel . File > ,
162+ file : string ,
163+ ignorePatterns : Array < string | RegExp > ,
164+ ) => {
155165 let didTransform = false
156166
157167 trav ( ast , {
@@ -161,7 +171,12 @@ const transform = (ast: ParseResult<Babel.File>, file: string) => {
161171 )
162172 functionDeclaration . traverse ( {
163173 JSXOpeningElement ( element ) {
164- const transformed = transformJSX ( element , propsName , file )
174+ const transformed = transformJSX (
175+ element ,
176+ propsName ,
177+ file ,
178+ ignorePatterns ,
179+ )
165180 if ( transformed ) {
166181 didTransform = true
167182 }
@@ -172,7 +187,12 @@ const transform = (ast: ParseResult<Babel.File>, file: string) => {
172187 const propsName = getPropsNameFromFunctionDeclaration ( path . node )
173188 path . traverse ( {
174189 JSXOpeningElement ( element ) {
175- const transformed = transformJSX ( element , propsName , file )
190+ const transformed = transformJSX (
191+ element ,
192+ propsName ,
193+ file ,
194+ ignorePatterns ,
195+ )
176196 if ( transformed ) {
177197 didTransform = true
178198 }
@@ -183,7 +203,12 @@ const transform = (ast: ParseResult<Babel.File>, file: string) => {
183203 const propsName = getPropsNameFromFunctionDeclaration ( path . node )
184204 path . traverse ( {
185205 JSXOpeningElement ( element ) {
186- const transformed = transformJSX ( element , propsName , file )
206+ const transformed = transformJSX (
207+ element ,
208+ propsName ,
209+ file ,
210+ ignorePatterns ,
211+ )
187212 if ( transformed ) {
188213 didTransform = true
189214 }
@@ -204,7 +229,12 @@ const transform = (ast: ParseResult<Babel.File>, file: string) => {
204229
205230 path . traverse ( {
206231 JSXOpeningElement ( element ) {
207- const transformed = transformJSX ( element , propsName , file )
232+ const transformed = transformJSX (
233+ element ,
234+ propsName ,
235+ file ,
236+ ignorePatterns ,
237+ )
208238 if ( transformed ) {
209239 didTransform = true
210240 }
@@ -216,17 +246,28 @@ const transform = (ast: ParseResult<Babel.File>, file: string) => {
216246 return didTransform
217247}
218248
219- export function addSourceToJsx ( code : string , id : string ) {
249+ export function addSourceToJsx (
250+ code : string ,
251+ id : string ,
252+ ignore : {
253+ files ?: Array < string | RegExp >
254+ components ?: Array < string | RegExp >
255+ } = { } ,
256+ ) {
220257 const [ filePath ] = id . split ( '?' )
221258 // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
222259 const location = filePath ?. replace ( normalizePath ( process . cwd ( ) ) , '' ) !
223260
261+ const fileIgnored = matcher ( ignore . files || [ ] , location )
262+ if ( fileIgnored ) {
263+ return
264+ }
224265 try {
225266 const ast = parse ( code , {
226267 sourceType : 'module' ,
227268 plugins : [ 'jsx' , 'typescript' ] ,
228269 } )
229- const didTransform = transform ( ast , location )
270+ const didTransform = transform ( ast , location , ignore . components || [ ] )
230271 if ( ! didTransform ) {
231272 return
232273 }
0 commit comments