11import type { BlocklistMeta , UnoGenerator } from '@unocss/core'
2+ import { dirname } from 'node:path'
23import process from 'node:process'
34import { sortRules } from '#integration/sort-rules'
45import { loadConfig } from '@unocss/config'
56import { createGenerator } from '@unocss/core'
67import { runAsWorker } from 'synckit'
78
8- const promises = new Map < string | undefined , Promise < UnoGenerator < any > > | undefined > ( )
9+ const promises = new Map < string , Promise < UnoGenerator < any > > | undefined > ( )
910
1011// bypass icon rules in ESLint
1112process . env . ESLINT ||= 'true'
1213
13- async function _getGenerator ( configPath ?: string ) {
14+ async function _getGenerator ( configPath ?: string , id ?: string ) {
15+ // Determine the search directory:
16+ // 1. If configPath is provided, use process.cwd() (existing behavior for explicit config)
17+ // 2. If filename is provided and no configPath, search from the file's directory (monorepo support)
18+ // 3. Otherwise, use process.cwd() as fallback
19+ const searchFrom = configPath
20+ ? process . cwd ( )
21+ : id
22+ ? dirname ( id )
23+ : process . cwd ( )
24+
1425 const { config, sources } = await loadConfig (
15- process . cwd ( ) ,
26+ searchFrom ,
1627 configPath ,
1728 )
1829 if ( ! sources . length )
@@ -23,25 +34,36 @@ async function _getGenerator(configPath?: string) {
2334 } )
2435}
2536
26- export async function getGenerator ( configPath ?: string ) {
27- let promise = promises . get ( configPath )
37+ function getCacheKey ( configPath ?: string , id ?: string ) : string {
38+ // Create a cache key based on configPath or the directory of the filename
39+ if ( configPath )
40+ return `config:${ configPath } `
41+ if ( id )
42+ return `dir:${ dirname ( id ) } `
43+ return `cwd:${ process . cwd ( ) } `
44+ }
45+
46+ export async function getGenerator ( configPath ?: string , id ?: string ) {
47+ const cacheKey = getCacheKey ( configPath , id )
48+ let promise = promises . get ( cacheKey )
2849 if ( ! promise ) {
29- promise = _getGenerator ( configPath )
30- promises . set ( configPath , promise )
50+ promise = _getGenerator ( configPath , id )
51+ promises . set ( cacheKey , promise )
3152 }
3253 return await promise
3354}
3455
3556export function setGenerator ( generator : Awaited < UnoGenerator < any > > , configPath ?: string | undefined ) {
36- promises . set ( configPath , Promise . resolve ( generator ) )
57+ const cacheKey = configPath ? `config:${ configPath } ` : `cwd:${ process . cwd ( ) } `
58+ promises . set ( cacheKey , Promise . resolve ( generator ) )
3759}
3860
39- async function actionSort ( configPath : string | undefined , classes : string ) {
40- return await sortRules ( classes , await getGenerator ( configPath ) )
61+ async function actionSort ( configPath : string | undefined , classes : string , id ?: string ) {
62+ return await sortRules ( classes , await getGenerator ( configPath , id ) )
4163}
4264
4365async function actionBlocklist ( configPath : string | undefined , classes : string , id ?: string ) : Promise < [ string , BlocklistMeta | undefined ] [ ] > {
44- const uno = await getGenerator ( configPath )
66+ const uno = await getGenerator ( configPath , id )
4567 const blocked = new Map < string , BlocklistMeta | undefined > ( )
4668
4769 const extracted = await uno . applyExtractors ( classes , id )
@@ -81,7 +103,7 @@ async function actionBlocklist(configPath: string | undefined, classes: string,
81103 return [ ...blocked ]
82104}
83105
84- export function runAsync ( configPath : string | undefined , action : 'sort' , classes : string ) : Promise < string >
106+ export function runAsync ( configPath : string | undefined , action : 'sort' , classes : string , id ?: string ) : Promise < string >
85107export function runAsync ( configPath : string | undefined , action : 'blocklist' , classes : string , id ?: string ) : Promise < [ string , BlocklistMeta | undefined ] [ ] >
86108export async function runAsync ( configPath : string | undefined , action : string , ...args : any [ ] ) : Promise < any > {
87109 switch ( action ) {
@@ -94,7 +116,7 @@ export async function runAsync(configPath: string | undefined, action: string, .
94116 }
95117}
96118
97- export function run ( configPath : string | undefined , action : 'sort' , classes : string ) : string
119+ export function run ( configPath : string | undefined , action : 'sort' , classes : string , id ?: string ) : string
98120export function run ( configPath : string | undefined , action : 'blocklist' , classes : string , id ?: string ) : [ string , BlocklistMeta | undefined ] [ ]
99121export function run ( configPath : string | undefined , action : string , ...args : any [ ] ) : any {
100122 // @ts -expect-error cast
0 commit comments