diff --git a/CHANGELOG.md b/CHANGELOG.md index 547ed68b054e..3d054b026933 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- Nothing yet! +### Fixed + +- Support calling `config()` with no arguments in plugin API ([#14799](https://github.com/tailwindlabs/tailwindcss/pull/14799)) ## [4.0.0-alpha.30] - 2024-10-24 diff --git a/packages/tailwindcss/src/compat/config/resolve-config.ts b/packages/tailwindcss/src/compat/config/resolve-config.ts index 9b09ad63fa77..c486de8ae33d 100644 --- a/packages/tailwindcss/src/compat/config/resolve-config.ts +++ b/packages/tailwindcss/src/compat/config/resolve-config.ts @@ -29,6 +29,7 @@ interface ResolutionContext { let minimal: ResolvedConfig = { blocklist: [], + future: {}, prefix: '', important: false, darkMode: null, diff --git a/packages/tailwindcss/src/compat/config/types.ts b/packages/tailwindcss/src/compat/config/types.ts index 313a7dba8b9b..9d01052e06cd 100644 --- a/packages/tailwindcss/src/compat/config/types.ts +++ b/packages/tailwindcss/src/compat/config/types.ts @@ -96,3 +96,12 @@ export interface UserConfig { export interface ResolvedConfig { important: boolean | string } + +// `future` key support +export interface UserConfig { + future?: 'all' | Record +} + +export interface ResolvedConfig { + future: Record +} diff --git a/packages/tailwindcss/src/compat/plugin-api.test.ts b/packages/tailwindcss/src/compat/plugin-api.test.ts index 9d5fea5911e2..c4da90503419 100644 --- a/packages/tailwindcss/src/compat/plugin-api.test.ts +++ b/packages/tailwindcss/src/compat/plugin-api.test.ts @@ -3791,3 +3791,78 @@ describe('prefix()', () => { expect(fn).toHaveBeenCalledWith('btn') }) }) + +describe('config()', () => { + test('can return the resolved config when passed no arguments', async () => { + let fn = vi.fn() + await compile( + css` + @plugin "my-plugin"; + `, + { + async loadModule(id, base) { + return { + base, + module: ({ config }: PluginAPI) => { + fn(config()) + }, + } + }, + }, + ) + + expect(fn).toHaveBeenCalledWith( + expect.objectContaining({ + theme: expect.objectContaining({ + spacing: expect.any(Object), + }), + }), + ) + }) + + test('can return part of the config', async () => { + let fn = vi.fn() + await compile( + css` + @plugin "my-plugin"; + `, + { + async loadModule(id, base) { + return { + base, + module: ({ config }: PluginAPI) => { + fn(config('theme')) + }, + } + }, + }, + ) + + expect(fn).toHaveBeenCalledWith( + expect.objectContaining({ + spacing: expect.any(Object), + }), + ) + }) + + test('falls back to default value if requested path does not exist', async () => { + let fn = vi.fn() + await compile( + css` + @plugin "my-plugin"; + `, + { + async loadModule(id, base) { + return { + base, + module: ({ config }: PluginAPI) => { + fn(config('somekey', 'defaultvalue')) + }, + } + }, + }, + ) + + expect(fn).toHaveBeenCalledWith('defaultvalue') + }) +}) diff --git a/packages/tailwindcss/src/compat/plugin-api.ts b/packages/tailwindcss/src/compat/plugin-api.ts index 54fd8e00a27a..a03b05a13b5e 100644 --- a/packages/tailwindcss/src/compat/plugin-api.ts +++ b/packages/tailwindcss/src/compat/plugin-api.ts @@ -71,7 +71,7 @@ export type PluginAPI = { ): void theme(path: string, defaultValue?: any): any - config(path: string, defaultValue?: any): any + config(path?: string, defaultValue?: any): any prefix(className: string): string } @@ -399,6 +399,8 @@ export function buildPluginApi( config(path, defaultValue) { let obj: Record = resolvedConfig + if (!path) return obj + let keypath = toKeyPath(path) for (let i = 0; i < keypath.length; ++i) {