1- import { PandaContext , loadConfigAndCreateContext } from '@pandacss/node '
1+ import { Generator } from '@pandacss/generator '
22import { runAsWorker } from 'synckit'
3- import { createContext } from 'fixture'
3+ import { createGeneratorContext , v9Config } from 'fixture'
44import { resolveTsPathPattern } from '@pandacss/config/ts-path'
5- import { findConfig } from '@pandacss/config'
5+ import { findConfig , loadConfig } from '@pandacss/config'
66import path from 'path'
7+ import micromatch from 'micromatch'
78import type { ImportResult } from '.'
89
910type Opts = {
1011 currentFile : string
1112 configPath ?: string
1213}
1314
14- const contextCache : { [ configPath : string ] : Promise < PandaContext > } = { }
15+ const contextCache : { [ configPath : string ] : Promise < Generator > } = { }
1516
1617async function _getContext ( configPath : string | undefined ) {
1718 if ( ! configPath ) throw new Error ( 'Invalid config path' )
1819
1920 const cwd = path . dirname ( configPath )
2021
21- const ctx = await loadConfigAndCreateContext ( { configPath, cwd } )
22+ const conf = await loadConfig ( { file : configPath , cwd } )
23+ const ctx = new Generator ( conf )
2224 return ctx
2325}
2426
2527export async function getContext ( opts : Opts ) {
2628 if ( process . env . NODE_ENV === 'test' ) {
27- const ctx = createContext ( ) as unknown as PandaContext
28- ctx . getFiles = ( ) => [ 'App.tsx' ]
29+ const ctx = createGeneratorContext ( {
30+ ...v9Config ,
31+ include : [ '**/*' ] ,
32+ exclude : [ '**/Invalid.tsx' , '**/panda.config.ts' ] ,
33+ importMap : './panda' ,
34+ jsxFactory : 'styled' ,
35+ } )
2936 return ctx
3037 } else {
3138 const configPath = findConfig ( { cwd : opts . configPath ?? opts . currentFile } )
39+ const cwd = path . dirname ( configPath )
3240
3341 // The context cache ensures we don't reload the same config multiple times
3442 if ( ! contextCache [ configPath ] ) {
3543 contextCache [ configPath ] = _getContext ( configPath )
3644 }
3745
38- return await contextCache [ configPath ]
46+ return contextCache [ configPath ]
3947 }
4048}
4149
42- async function filterInvalidTokens ( ctx : PandaContext , paths : string [ ] ) : Promise < string [ ] > {
43- return paths . filter ( ( path ) => ! ctx . utility . tokens . view . get ( path ) )
50+ async function filterInvalidTokens ( ctx : Generator , paths : string [ ] ) : Promise < string [ ] > {
51+ const invalid = paths . filter ( ( path ) => ! ctx . utility . tokens . view . get ( path ) )
52+ console . error ( 'filterInvalidTokens' , { paths, invalid } )
53+ return invalid
4454}
4555
4656export type DeprecatedToken =
@@ -50,67 +60,77 @@ export type DeprecatedToken =
5060 value : string
5161 }
5262
53- async function filterDeprecatedTokens ( ctx : PandaContext , tokens : DeprecatedToken [ ] ) : Promise < DeprecatedToken [ ] > {
63+ async function filterDeprecatedTokens ( ctx : Generator , tokens : DeprecatedToken [ ] ) : Promise < DeprecatedToken [ ] > {
5464 return tokens . filter ( ( token ) => {
5565 const value = typeof token === 'string' ? token : token . category + '.' + token . value
5666 return ctx . utility . tokens . isDeprecated ( value )
5767 } )
5868}
5969
60- async function isColorToken ( ctx : PandaContext , value : string ) : Promise < boolean > {
70+ async function isColorToken ( ctx : Generator , value : string ) : Promise < boolean > {
6171 return ! ! ctx . utility . tokens . view . categoryMap . get ( 'colors' ) ?. get ( value )
6272}
6373
64- async function getPropCategory ( ctx : PandaContext , _attr : string ) {
74+ async function getPropCategory ( ctx : Generator , _attr : string ) {
6575 const longhand = await resolveLongHand ( ctx , _attr )
6676 const attr = longhand || _attr
6777 const attrConfig = ctx . utility . config [ attr ]
6878 return typeof attrConfig ?. values === 'string' ? attrConfig . values : undefined
6979}
7080
71- async function isColorAttribute ( ctx : PandaContext , _attr : string ) : Promise < boolean > {
81+ async function isColorAttribute ( ctx : Generator , _attr : string ) : Promise < boolean > {
7282 const category = await getPropCategory ( ctx , _attr )
7383 return category === 'colors'
7484}
7585
76- const arePathsEqual = ( path1 : string , path2 : string ) = > {
77- const normalizedPath1 = path . resolve ( path1 )
78- const normalizedPath2 = path . resolve ( path2 )
86+ async function isValidFile ( ctx : Generator , fileName : string ) : Promise < boolean > {
87+ const { include , exclude } = ctx . config
88+ const cwd = ctx . config . cwd || process . cwd ( )
7989
80- return normalizedPath1 === normalizedPath2
81- }
90+ const relativePath = path . isAbsolute ( fileName ) ? path . relative ( cwd , fileName ) : fileName
8291
83- async function isValidFile ( ctx : PandaContext , fileName : string ) : Promise < boolean > {
84- return ctx . getFiles ( ) . some ( ( file ) => arePathsEqual ( file , fileName ) )
92+ return micromatch . isMatch ( relativePath , include , { ignore : exclude , dot : true } )
8593}
8694
87- async function resolveShorthands ( ctx : PandaContext , name : string ) : Promise < string [ ] | undefined > {
95+ async function resolveShorthands ( ctx : Generator , name : string ) : Promise < string [ ] | undefined > {
8896 return ctx . utility . getPropShorthandsMap ( ) . get ( name )
8997}
9098
91- async function resolveLongHand ( ctx : PandaContext , name : string ) : Promise < string | undefined > {
99+ async function resolveLongHand ( ctx : Generator , name : string ) : Promise < string | undefined > {
92100 const reverseShorthandsMap = new Map ( )
93101
94- for ( const [ key , values ] of ctx . utility . getPropShorthandsMap ( ) ) {
102+ const shorthands = ctx . utility . getPropShorthandsMap ( )
103+
104+ for ( const [ key , values ] of shorthands ) {
95105 for ( const value of values ) {
96106 reverseShorthandsMap . set ( value , key )
97107 }
98108 }
99109
100- return reverseShorthandsMap . get ( name )
110+ const result = reverseShorthandsMap . get ( name )
111+ return result
101112}
102113
103- async function isValidProperty ( ctx : PandaContext , name : string , patternName ?: string ) {
104- if ( ctx . isValidProperty ( name ) ) return true
105- if ( ! patternName ) return
114+ async function isValidProperty ( ctx : Generator , name : string , patternName ?: string ) {
115+ const isValid = ctx . isValidProperty ( name )
116+ if ( isValid ) return true
117+ if ( ! patternName ) return false
118+
119+ // If the pattern name is the jsxFactory (e.g., 'styled'), we should accept
120+ // any property that is valid according to the global property check
121+ // Since styled components are generic wrappers, we don't need pattern-specific checks
122+ if ( patternName === ctx . config . jsxFactory ) {
123+ // Already checked globally above, so return false if we got here
124+ return false
125+ }
106126
107127 const pattern = ctx . patterns . details . find ( ( p ) => p . baseName === patternName || p . jsx . includes ( patternName ) ) ?. config
108128 . properties
109- if ( ! pattern ) return
129+ if ( ! pattern ) return false
110130 return Object . keys ( pattern ) . includes ( name )
111131}
112132
113- async function matchFile ( ctx : PandaContext , name : string , imports : ImportResult [ ] ) {
133+ async function matchFile ( ctx : Generator , name : string , imports : ImportResult [ ] ) {
114134 const file = ctx . imports . file ( imports )
115135
116136 return file . match ( name )
@@ -121,12 +141,17 @@ type MatchImportResult = {
121141 alias : string
122142 mod : string
123143}
124- async function matchImports ( ctx : PandaContext , result : MatchImportResult ) {
125- return ctx . imports . match ( result , ( mod ) => {
144+ async function matchImports ( ctx : Generator , result : MatchImportResult ) {
145+ const isMatch = ctx . imports . match ( result , ( mod ) => {
126146 const { tsOptions } = ctx . parserOptions
127147 if ( ! tsOptions ?. pathMappings ) return
128148 return resolveTsPathPattern ( tsOptions . pathMappings , mod )
129149 } )
150+ return isMatch
151+ }
152+
153+ async function getJsxFactory ( ctx : Generator ) {
154+ return ctx . config . jsxFactory
130155}
131156
132157export function runAsync ( action : 'filterInvalidTokens' , opts : Opts , paths : string [ ] ) : Promise < string [ ] >
@@ -139,6 +164,7 @@ export function runAsync(action: 'isValidProperty', opts: Opts, name: string, pa
139164export function runAsync ( action : 'matchFile' , opts : Opts , name : string , imports : ImportResult [ ] ) : Promise < boolean >
140165export function runAsync ( action : 'matchImports' , opts : Opts , result : MatchImportResult ) : Promise < boolean >
141166export function runAsync ( action : 'getPropCategory' , opts : Opts , prop : string ) : Promise < string >
167+ export function runAsync ( action : 'getJsxFactory' , opts : Opts ) : Promise < string | undefined >
142168export function runAsync (
143169 action : 'filterDeprecatedTokens' ,
144170 opts : Opts ,
@@ -177,6 +203,8 @@ export async function runAsync(action: string, opts: Opts, ...args: any): Promis
177203 case 'getPropCategory' :
178204 // @ts -expect-error cast
179205 return getPropCategory ( ctx , ...args )
206+ case 'getJsxFactory' :
207+ return getJsxFactory ( ctx )
180208 case 'filterDeprecatedTokens' :
181209 // @ts -expect-error cast
182210 return filterDeprecatedTokens ( ctx , ...args )
@@ -193,6 +221,7 @@ export function run(action: 'isValidProperty', opts: Opts, name: string, pattern
193221export function run ( action : 'matchFile' , opts : Opts , name : string , imports : ImportResult [ ] ) : boolean
194222export function run ( action : 'matchImports' , opts : Opts , result : MatchImportResult ) : boolean
195223export function run ( action : 'getPropCategory' , opts : Opts , prop : string ) : string
224+ export function run ( action : 'getJsxFactory' , opts : Opts ) : string | undefined
196225export function run ( action : 'filterDeprecatedTokens' , opts : Opts , tokens : DeprecatedToken [ ] ) : DeprecatedToken [ ]
197226export function run ( action : string , opts : Opts , ...args : any [ ] ) : any {
198227 // @ts -expect-error cast
0 commit comments