Skip to content

Commit

Permalink
fix(vscode): skip util contains none color define (#1769)
Browse files Browse the repository at this point in the history
  • Loading branch information
azaleta committed Oct 25, 2022
1 parent f8372e8 commit b803ca7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/vscode/src/annotation.ts
Expand Up @@ -97,7 +97,7 @@ export async function registerAnnotations(

const result = await ctx.uno.generate(code, { id, preflights: false, minify: true })

const colorsMap = getColorsMap(ctx.uno, result)
const colorsMap = await getColorsMap(ctx.uno, result)
const colorRanges: DecorationOptions[] = []
const _colorPositionsCache = new Map<string, string>() // cache for avoid duplicated color ranges

Expand Down
10 changes: 9 additions & 1 deletion packages/vscode/src/utils.ts
Expand Up @@ -59,11 +59,14 @@ export function body2ColorValue(body: string, theme: Theme) {
const matchedAttributifyRE = /(?<=^\[.+~=").*(?="\]$)/
const matchedValuelessAttributifyRE = /(?<=^\[).+(?==""\]$)/
const _colorsMapCache = new Map<string, string>()
export function getColorsMap(uno: UnoGenerator, result: GenerateResult) {
export async function getColorsMap(uno: UnoGenerator, result: GenerateResult) {
const theme = uno.config.theme as Theme
const colorsMap = new Map<string, string>()

for (const i of result.matched) {
if (!(await isColorUtility(uno, i)))
continue

const matchedValueless = i.match(matchedValuelessAttributifyRE)?.[0]
const colorKey = matchedValueless ?? i.replace('~="', '="')

Expand Down Expand Up @@ -97,3 +100,8 @@ export function isSubdir(parent: string, child: string) {
const relative = path.relative(parent, child)
return relative && !relative.startsWith('..') && !path.isAbsolute(relative)
}

async function isColorUtility(uno: UnoGenerator, utilName: string) {
const { css } = await uno.generate(utilName, { preflights: false, safelist: false })
return css.includes('color')
}

0 comments on commit b803ca7

Please sign in to comment.