From fdd26011f1186cf6ebc753fcf0c42896c207bb34 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Thu, 4 Jul 2024 20:04:23 -0400 Subject: [PATCH] Add mechanism to return registered plugins from CSS --- packages/tailwindcss/src/index.ts | 19 ++++++++++++++++++- packages/tailwindcss/src/plugins.test.ts | 12 ++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/tailwindcss/src/index.ts b/packages/tailwindcss/src/index.ts index 431d22bbe..8817c5de7 100644 --- a/packages/tailwindcss/src/index.ts +++ b/packages/tailwindcss/src/index.ts @@ -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({})) } diff --git a/packages/tailwindcss/src/plugins.test.ts b/packages/tailwindcss/src/plugins.test.ts index 274e4a829..6d1075a93 100644 --- a/packages/tailwindcss/src/plugins.test.ts +++ b/packages/tailwindcss/src/plugins.test.ts @@ -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']) +})