-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to block generation of certain utilities (#9812)
* Add blocklist tests * Build initial implementation of blocklist * wip * wip * wip * Update changelog
- Loading branch information
1 parent
4ccc0fa
commit 602101d
Showing
4 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { run, html, css } from './util/run' | ||
|
||
let warn | ||
|
||
beforeEach(() => { | ||
warn = jest.spyOn(require('../src/util/log').default, 'warn') | ||
}) | ||
|
||
afterEach(() => warn.mockClear()) | ||
|
||
it('can block classes matched literally', () => { | ||
let config = { | ||
content: [ | ||
{ | ||
raw: html`<div | ||
class="font-bold uppercase sm:hover:text-sm hover:text-sm bg-red-500/50 my-custom-class" | ||
></div>`, | ||
}, | ||
], | ||
blocklist: ['font', 'uppercase', 'hover:text-sm', 'bg-red-500/50', 'my-custom-class'], | ||
} | ||
|
||
let input = css` | ||
@tailwind utilities; | ||
.my-custom-class { | ||
color: red; | ||
} | ||
` | ||
|
||
return run(input, config).then((result) => { | ||
return expect(result.css).toMatchCss(css` | ||
.font-bold { | ||
font-weight: 700; | ||
} | ||
.my-custom-class { | ||
color: red; | ||
} | ||
@media (min-width: 640px) { | ||
.sm\:hover\:text-sm:hover { | ||
font-size: 0.875rem; | ||
line-height: 1.25rem; | ||
} | ||
} | ||
`) | ||
}) | ||
}) | ||
|
||
it('can block classes inside @layer', () => { | ||
let config = { | ||
content: [ | ||
{ | ||
raw: html`<div class="font-bold my-custom-class"></div>`, | ||
}, | ||
], | ||
blocklist: ['my-custom-class'], | ||
} | ||
|
||
let input = css` | ||
@tailwind utilities; | ||
@layer utilities { | ||
.my-custom-class { | ||
color: red; | ||
} | ||
} | ||
` | ||
|
||
return run(input, config).then((result) => { | ||
return expect(result.css).toMatchCss(css` | ||
.font-bold { | ||
font-weight: 700; | ||
} | ||
`) | ||
}) | ||
}) | ||
|
||
it('blocklists do NOT support regexes', async () => { | ||
let config = { | ||
content: [{ raw: html`<div class="font-bold bg-[#f00d1e]"></div>` }], | ||
blocklist: [/^bg-\[[^]+\]$/], | ||
} | ||
|
||
let result = await run('@tailwind utilities', config) | ||
|
||
expect(result.css).toMatchCss(css` | ||
.bg-\[\#f00d1e\] { | ||
--tw-bg-opacity: 1; | ||
background-color: rgb(240 13 30 / var(--tw-bg-opacity)); | ||
} | ||
.font-bold { | ||
font-weight: 700; | ||
} | ||
`) | ||
|
||
expect(warn).toHaveBeenCalledTimes(1) | ||
expect(warn.mock.calls.map((x) => x[0])).toEqual(['blocklist-invalid']) | ||
}) | ||
|
||
it('can block classes generated by the safelist', () => { | ||
let config = { | ||
content: [{ raw: html`<div class="font-bold"></div>` }], | ||
safelist: [{ pattern: /^bg-red-(400|500)$/ }], | ||
blocklist: ['bg-red-500'], | ||
} | ||
|
||
return run('@tailwind utilities', config).then((result) => { | ||
return expect(result.css).toMatchCss(css` | ||
.bg-red-400 { | ||
--tw-bg-opacity: 1; | ||
background-color: rgb(248 113 113 / var(--tw-bg-opacity)); | ||
} | ||
.font-bold { | ||
font-weight: 700; | ||
} | ||
`) | ||
}) | ||
}) |