Skip to content

Commit

Permalink
feat(vscode): annotation for config file
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Apr 11, 2023
1 parent a164fcb commit f1c937d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/vscode/src/annotation.ts
Expand Up @@ -14,6 +14,7 @@ export async function registerAnnotations(
) {
let underline: boolean = workspace.getConfiguration().get('unocss.underline') ?? true
let colorPreview: boolean = workspace.getConfiguration().get('unocss.colorPreview') ?? true

ext.subscriptions.push(workspace.onDidChangeConfiguration((event) => {
if (event.affectsConfiguration('unocss.underline')) {
underline = workspace.getConfiguration().get('unocss.underline') ?? true
Expand Down Expand Up @@ -92,7 +93,12 @@ export async function registerAnnotations(
if (!ctx)
ctx = await contextLoader.resolveClosestContext(code, id)

if (!ctx.filter(code, id) && !code.includes(INCLUDE_COMMENT_IDE) && !isCssId(id))
const isTarget = ctx.filter(code, id) // normal unocss filter
|| code.includes(INCLUDE_COMMENT_IDE) // force include
|| contextLoader.configSources.includes(id) // include config files
|| isCssId(id) // include css files

if (!isTarget)
return reset()

const result = await ctx.uno.generate(code, { id, preflights: false, minify: true })
Expand Down
7 changes: 7 additions & 0 deletions packages/vscode/src/contextLoader.ts
Expand Up @@ -16,6 +16,7 @@ export class ContextLoader {
public ready: Promise<void>
public defaultContext: UnocssPluginContext<UserConfig<any>>
public contextsMap = new Map<string, UnocssPluginContext<UserConfig<any>> | null>()
public configSources: string[] = []

private fileContextCache = new Map<string, UnocssPluginContext<UserConfig<any>> | null>()
private configExistsCache = new Map<string, boolean>()
Expand Down Expand Up @@ -89,6 +90,10 @@ export class ContextLoader {

const load = async () => {
log.appendLine(`🛠 Resolving config for ${dir}`)

// @ts-expect-error support global utils
globalThis.defineNuxtConfig = config => config

const context = createContext(
dir,
this.defaultUnocssConfig,
Expand Down Expand Up @@ -131,6 +136,8 @@ export class ContextLoader {
return null
}

this.configSources = sources

if (!sources.length)
return null

Expand Down

0 comments on commit f1c937d

Please sign in to comment.