Skip to content

Commit 9c41ce8

Browse files
Plugin API: Allow custom utilities to start with @ (#14793)
Closes #14791 Add support to the JS Plugin interop layer for utilities that _start with_ `@`. This ensures no breaking when trying to load plugins that contribute utilities like `@container` from `@tailwindcss/container-queries` (even though the `@container` utility is now part of core). ## Test Plan Added the `@tailwindcss/container-queries` plugin to to the Vite example: ![Screenshot 2024-10-25 at 11.18.19.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/0Y77ilPI2WoJfMLFiAEw/3761c0a8-8c54-42eb-a1fd-213c4215c024.png) However, in order for the Vite example to load the extension, I also had to apply the following patch: ![Screenshot 2024-10-25 at 11.18.54.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/0Y77ilPI2WoJfMLFiAEw/bd151684-ff7b-4805-b305-71ac0378c610.png) I think this is related to our dev system though, the compiled plugin file is going to be a flat file with no requires in our public release. --------- Co-authored-by: Adam Wathan <[email protected]>
1 parent 3244da8 commit 9c41ce8

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3939
- Ensure spacing utilities with no value (e.g. `px` or `translate-y`) don't generate CSS ([#14911](https://github.com/tailwindlabs/tailwindcss/pull/14911))
4040
- Don't override user-agent background color for input elements in Preflight ([#14913](https://github.com/tailwindlabs/tailwindcss/pull/14913))
4141
- Don't attempt to convert CSS variables (which should already be percentages) to percentages when used as opacity modifiers ([#14916](https://github.com/tailwindlabs/tailwindcss/pull/14916))
42+
- Ensure custom utilities registered with the plugin API can start with `@` ([#14793](https://github.com/tailwindlabs/tailwindcss/pull/14793))
4243
- _Upgrade (experimental)_: Install `@tailwindcss/postcss` next to `tailwindcss` ([#14830](https://github.com/tailwindlabs/tailwindcss/pull/14830))
4344
- _Upgrade (experimental)_: Remove whitespace around `,` separator when print arbitrary values ([#14838](https://github.com/tailwindlabs/tailwindcss/pull/14838))
4445
- _Upgrade (experimental)_: Fix crash during upgrade when content globs escape root of project ([#14896](https://github.com/tailwindlabs/tailwindcss/pull/14896))

packages/tailwindcss/src/compat/plugin-api.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2943,6 +2943,50 @@ describe('matchUtilities()', () => {
29432943
).toEqual('')
29442944
})
29452945

2946+
test('custom functional utilities can start with @', async () => {
2947+
async function run(candidates: string[]) {
2948+
let compiled = await compile(
2949+
css`
2950+
@plugin "my-plugin";
2951+
@tailwind utilities;
2952+
`,
2953+
2954+
{
2955+
async loadModule(id, base) {
2956+
return {
2957+
base,
2958+
module: ({ matchUtilities }: PluginAPI) => {
2959+
matchUtilities(
2960+
{ '@w': (value) => ({ width: value }) },
2961+
{
2962+
values: {
2963+
1: '1px',
2964+
},
2965+
},
2966+
)
2967+
},
2968+
}
2969+
},
2970+
},
2971+
)
2972+
2973+
return compiled.build(candidates)
2974+
}
2975+
2976+
expect(optimizeCss(await run(['@w-1','hover:@w-1'])).trim())
2977+
.toMatchInlineSnapshot(`
2978+
".\\@w-1 {
2979+
width: 1px;
2980+
}
2981+
2982+
@media (hover: hover) {
2983+
.hover\\:\\@w-1:hover {
2984+
width: 1px;
2985+
}
2986+
}"
2987+
`)
2988+
})
2989+
29462990
test('custom functional utilities can return an array of rules', async () => {
29472991
let compiled = await compile(
29482992
css`

packages/tailwindcss/src/compat/plugin-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export type PluginAPI = {
7575
prefix(className: string): string
7676
}
7777

78-
const IS_VALID_UTILITY_NAME = /^[a-z][a-zA-Z0-9/%._-]*$/
78+
const IS_VALID_UTILITY_NAME = /^[a-z@][a-zA-Z0-9/%._-]*$/
7979

8080
export function buildPluginApi(
8181
designSystem: DesignSystem,

0 commit comments

Comments
 (0)