Add mechanism to return registered plugins from CSS

This commit is contained in:
Jordan Pittman 2024-07-04 20:04:23 -04:00
parent 103de984eb
commit fdd26011f1
2 changed files with 30 additions and 1 deletions

View file

@ -11,8 +11,10 @@ export function compile(
config?: UserConfig,
): {
build(candidates: string[]): string
readonly plugins: string[]
} {
let resolved = resolve(config ?? {})
let plugins: string[] = []
let ast = CSS.parse(css)
@ -53,6 +55,20 @@ export function compile(
replaceWith(node.nodes)
}
if (node.selector.startsWith('@plugin')) {
let path = node.selector
// Ignore `@plugin` when parsing the selector
.slice(7)
// Remove any surrounding spaces
.trim()
// Remove the surrounding quotes
.slice(1, -1)
plugins.push(path)
replaceWith([])
return WalkAction.Skip
}
if (node.selector !== '@theme' && node.selector !== '@theme reference') return
let isReference = node.selector === '@theme reference'
@ -194,6 +210,7 @@ export function compile(
let previousAstNodeCount = 0
return {
plugins,
build(newRawCandidates: string[]) {
let didChange = false
@ -254,5 +271,5 @@ export function __unstable__loadDesignSystem(css: string) {
})
})
return buildDesignSystem(theme)
return buildDesignSystem(theme, resolve({}))
}

View file

@ -1,7 +1,10 @@
import { test, vi } from 'vitest'
import { compile } from '.'
import type { UserConfig } from './config'
import { run } from './test-utils/run'
const css = String.raw
test('Plugins are called', ({ expect }) => {
let fn = vi.fn()
let handler = vi.fn()
@ -140,3 +143,12 @@ test('Custom functional variants', ({ expect }) => {
`)
expect(run(['in-[:checked]/foo:underline'], config)).toEqual('')
})
test('Plugins registered in CSS can be returned', ({ expect }) => {
let builder = compile(css`
@plugin "my-plugin";
@plugin "./my-plugin";
`)
expect(builder.plugins).toEqual(['my-plugin', './my-plugin'])
})