Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
zyyv committed Dec 27, 2024
1 parent 81b7c47 commit c3e2192
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
19 changes: 11 additions & 8 deletions packages/core/src/core/postprocess/important.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ function createFilter(
}
}

export function importantProcess(importantOptions: ImportantOptions): Postprocessor {
console.log(importantOptions)

const keyFilter = createFilter(importantOptions.includes || [], importantOptions.excludes || [])
export function importantProcess(importantOptions: Required<ImportantOptions>): Postprocessor {
const keyFilter = createFilter(importantOptions.includes, importantOptions.excludes)

return (util) => {
for (const item of util.entries) {
console.log(item[0], keyFilter(item[0]))

if (keyFilter(item[0]) && item[1] != null && !String(item[1]).includes('!important')) {
item[1] += ' !important'
if (keyFilter(item[0])) {
if (item[1] != null && !String(item[1]).includes('!important')) {
item[1] += ' !important'
}
}
else {
if (item[1] != null && String(item[1]).includes('!important')) {
item[1] = String(item[1]).replace(/\s*!important/g, '')
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,22 @@ const defaultPresetOptions: Record<string, any> = {
} as WebFontsOptions,
}

const defaultImportantOptions = {
excludes: [],
includes: [/.*/g],
}

export async function resolveOptions(options: UsefulOptions) {
const optionsWithDefault = Object.assign({}, defaultOptions, options) as Required<UsefulOptions>
optionsWithDefault.unColor = typeof optionsWithDefault.unColor === 'string'
? optionsWithDefault.unColor
: optionsWithDefault.unColor ? '--un-color' : false

optionsWithDefault.important = optionsWithDefault.important === true ? { excludes: [] } : optionsWithDefault.important
optionsWithDefault.important = typeof optionsWithDefault.important === 'object'
? Object.assign({}, defaultImportantOptions, optionsWithDefault.important)
: optionsWithDefault.important === true
? defaultImportantOptions
: false

const presets = await resolvePresets(optionsWithDefault)
const transformers = await resolveTransformers(optionsWithDefault)
Expand Down
3 changes: 2 additions & 1 deletion test/extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('presetUseful extractor', async () => {
expect(matched.size).toBe(1)
expect(css).toMatchInlineSnapshot(`
"/* layer: default */
.bg-\\[url\\(data\\:image\\/png\\;base64\\,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIwAAAABJRU5ErkJggg\\=\\=\\)\\]{--un-url:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIwAAAABJRU5ErkJggg==);background-image:var(--un-url);}"`)
.bg-\\[url\\(data\\:image\\/png\\;base64\\,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIwAAAABJRU5ErkJggg\\=\\=\\)\\]{--un-url:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIwAAAABJRU5ErkJggg==);background-image:var(--un-url);}"
`)
})
})
44 changes: 44 additions & 0 deletions test/postprocess.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,48 @@ describe('presetUseful postprocess with important', () => {
}"
`)
})

it('base with excludes', async () => {
const uno = await generateUno({
preflights: false,
important: {
excludes: ['color', /bg-/, 'margin'],
},
})

const { css } = await uno.generate(tokens)

expect(css).toMatchInlineSnapshot(`
"/* layer: default */
.important-ma{margin:auto;}
.bg-red{--un-bg-opacity:1;background-color:rgb(248 113 113 / var(--un-bg-opacity));}
.\\!text-xl{font-size:1.25rem !important;line-height:1.75rem !important;}
.text-blue{--un-text-opacity:1 !important;color:rgb(96 165 250 / var(--un-text-opacity));}
@media (min-width: 640px){
.sm\\:text-sm\\!{font-size:0.875rem !important;line-height:1.25rem !important;}
}"
`)
})

it('base with includes', async () => {
const uno = await generateUno({
preflights: false,
important: {
includes: ['color', /bg-/, 'margin'],
},
})

const { css } = await uno.generate(tokens)

expect(css).toMatchInlineSnapshot(`
"/* layer: default */
.important-ma{margin:auto !important;}
.bg-red{--un-bg-opacity:1 !important;background-color:rgb(248 113 113 / var(--un-bg-opacity)) !important;}
.\\!text-xl{font-size:1.25rem;line-height:1.75rem;}
.text-blue{--un-text-opacity:1;color:rgb(96 165 250 / var(--un-text-opacity)) !important;}
@media (min-width: 640px){
.sm\\:text-sm\\!{font-size:0.875rem;line-height:1.25rem;}
}"
`)
})
})

0 comments on commit c3e2192

Please sign in to comment.