Skip to content

Commit

Permalink
feat(core): support custom function for blocklist, #3073
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 4, 2023
1 parent 44b7ebb commit 3392020
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ export class UnoGenerator<Theme extends object = object> {
}

isBlocked(raw: string): boolean {
return !raw || this.config.blocklist.some(e => isString(e) ? e === raw : e.test(raw))
return !raw || this.config.blocklist.some(e => typeof e === 'function' ? e(raw) : isString(e) ? e === raw : e.test(raw))
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export interface Preflight<Theme extends object = object> {
layer?: string
}

export type BlocklistRule = string | RegExp
export type BlocklistRule = string | RegExp | ((selector: string) => boolean | null | undefined)

export interface VariantHandlerContext {
/**
Expand Down
6 changes: 4 additions & 2 deletions test/blocklist.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ describe('blocklist', () => {
blocklist: [
'block',
/^text-/,
i => i.includes('green'),
],
presets: [
presetUno(),
],
})
const { css: css1 } = await uno.generate('block text-red-200 hover:block', { minify: true, preflights: false })
const { css: css2 } = await dos.generate('block text-red-200 hover:block', { minify: true, preflights: false })
const { css: css1 } = await uno.generate('block text-red-200 hover:block bg-green text-green', { minify: true, preflights: false })
const { css: css2 } = await dos.generate('block text-red-200 hover:block bg-green text-green', { minify: true, preflights: false })
expect(css1).contain('.block')
expect(css1).contain('.text-red-200')
expect(css1).contain('.text-green')
expect(css2).eq('')

const { css: css3 } = await dos.generate('block text-red-200 hover:block', { minify: true, preflights: false })
Expand Down

0 comments on commit 3392020

Please sign in to comment.