Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Added

- Add support for `addBase` plugins using the `@plugin` directive ([#14172](https://github.com/tailwindlabs/tailwindcss/pull/14172))

## [4.0.0-alpha.19] - 2024-08-09

Expand Down
40 changes: 40 additions & 0 deletions packages/tailwindcss/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2074,3 +2074,43 @@ describe('@variant', () => {
`)
})
})

test('addBase', async () => {
let { build } = await compile(
css`
@plugin "my-plugin";
@layer base, utilities;
@layer utilities {
@tailwind utilities;
}
`,

{
loadPlugin: async () => {
return ({ addBase }) => {
addBase({
body: {
'font-feature-settings': '"tnum"',
},
})
}
},
},
)

let compiled = build(['underline'])

expect(optimizeCss(compiled).trim()).toMatchInlineSnapshot(`
"@layer base {
body {
font-feature-settings: "tnum";
}
}

@layer utilities {
.underline {
text-decoration-line: underline;
}
}"
`)
})
2 changes: 1 addition & 1 deletion packages/tailwindcss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ async function parseCss(css: string, { loadPlugin = throwOnPlugin }: CompileOpti
customUtility(designSystem)
}

let pluginApi = buildPluginApi(designSystem)
let pluginApi = buildPluginApi(designSystem, ast)

await Promise.all(pluginLoaders.map((loader) => loader.then((plugin) => plugin(pluginApi))))

Expand Down
9 changes: 7 additions & 2 deletions packages/tailwindcss/src/plugin-api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { substituteAtApply } from './apply'
import { objectToAst, rule, type CssInJs } from './ast'
import { objectToAst, rule, type AstNode, type CssInJs } from './ast'
import type { DesignSystem } from './design-system'
import { withAlpha, withNegative } from './utilities'
import { inferDataType } from './utils/infer-data-type'

export type PluginAPI = {
addBase(base: CssInJs): void
addVariant(name: string, variant: string | string[] | CssInJs): void
addUtilities(utilities: Record<string, CssInJs>, options?: {}): void
matchUtilities(
Expand All @@ -20,8 +21,12 @@ export type PluginAPI = {

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

export function buildPluginApi(designSystem: DesignSystem): PluginAPI {
export function buildPluginApi(designSystem: DesignSystem, ast: AstNode[]): PluginAPI {
return {
addBase(css) {
ast.push(rule('@layer base', objectToAst(css)))
},

addVariant(name, variant) {
// Single selector
if (typeof variant === 'string') {
Expand Down