Make config() path arg optional in v4 plugin API (#14799)

Co-authored-by: Adam Wathan <adam.wathan@gmail.com>
This commit is contained in:
Jordan Pittman 2024-10-28 12:05:57 -04:00 • committed by GitHub
parent 5b2f6c7506
commit e14ab1f9ee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 91 additions and 2 deletions

View file

@ -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

View file

@ -29,6 +29,7 @@ interface ResolutionContext {
let minimal: ResolvedConfig = {
blocklist: [],
future: {},
prefix: '',
important: false,
darkMode: null,

View file

@ -96,3 +96,12 @@ export interface UserConfig {
export interface ResolvedConfig {
important: boolean | string
}
// `future` key support
export interface UserConfig {
future?: 'all' | Record<string, boolean>
}
export interface ResolvedConfig {
future: Record<string, boolean>
}

View file

@ -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')
})
})

View file

@ -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<any, any> = resolvedConfig
if (!path) return obj
let keypath = toKeyPath(path)
for (let i = 0; i < keypath.length; ++i) {