Skip to content

Commit e14ab1f

Browse files
Make config() path arg optional in v4 plugin API (#14799)
Co-authored-by: Adam Wathan <adam.wathan@gmail.com>
1 parent 5b2f6c7 commit e14ab1f

File tree

5 files changed

+91
-2
lines changed

5 files changed

+91
-2
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
- Nothing yet!
10+
### Fixed
11+
12+
- Support calling `config()` with no arguments in plugin API ([#14799](https://github.com/tailwindlabs/tailwindcss/pull/14799))
1113

1214
## [4.0.0-alpha.30] - 2024-10-24
1315

packages/tailwindcss/src/compat/config/resolve-config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ interface ResolutionContext {
2929

3030
let minimal: ResolvedConfig = {
3131
blocklist: [],
32+
future: {},
3233
prefix: '',
3334
important: false,
3435
darkMode: null,

packages/tailwindcss/src/compat/config/types.ts

+9
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,12 @@ export interface UserConfig {
9696
export interface ResolvedConfig {
9797
important: boolean | string
9898
}
99+
100+
// `future` key support
101+
export interface UserConfig {
102+
future?: 'all' | Record<string, boolean>
103+
}
104+
105+
export interface ResolvedConfig {
106+
future: Record<string, boolean>
107+
}

packages/tailwindcss/src/compat/plugin-api.test.ts

+75
Original file line numberDiff line numberDiff line change
@@ -3791,3 +3791,78 @@ describe('prefix()', () => {
37913791
expect(fn).toHaveBeenCalledWith('btn')
37923792
})
37933793
})
3794+
3795+
describe('config()', () => {
3796+
test('can return the resolved config when passed no arguments', async () => {
3797+
let fn = vi.fn()
3798+
await compile(
3799+
css`
3800+
@plugin "my-plugin";
3801+
`,
3802+
{
3803+
async loadModule(id, base) {
3804+
return {
3805+
base,
3806+
module: ({ config }: PluginAPI) => {
3807+
fn(config())
3808+
},
3809+
}
3810+
},
3811+
},
3812+
)
3813+
3814+
expect(fn).toHaveBeenCalledWith(
3815+
expect.objectContaining({
3816+
theme: expect.objectContaining({
3817+
spacing: expect.any(Object),
3818+
}),
3819+
}),
3820+
)
3821+
})
3822+
3823+
test('can return part of the config', async () => {
3824+
let fn = vi.fn()
3825+
await compile(
3826+
css`
3827+
@plugin "my-plugin";
3828+
`,
3829+
{
3830+
async loadModule(id, base) {
3831+
return {
3832+
base,
3833+
module: ({ config }: PluginAPI) => {
3834+
fn(config('theme'))
3835+
},
3836+
}
3837+
},
3838+
},
3839+
)
3840+
3841+
expect(fn).toHaveBeenCalledWith(
3842+
expect.objectContaining({
3843+
spacing: expect.any(Object),
3844+
}),
3845+
)
3846+
})
3847+
3848+
test('falls back to default value if requested path does not exist', async () => {
3849+
let fn = vi.fn()
3850+
await compile(
3851+
css`
3852+
@plugin "my-plugin";
3853+
`,
3854+
{
3855+
async loadModule(id, base) {
3856+
return {
3857+
base,
3858+
module: ({ config }: PluginAPI) => {
3859+
fn(config('somekey', 'defaultvalue'))
3860+
},
3861+
}
3862+
},
3863+
},
3864+
)
3865+
3866+
expect(fn).toHaveBeenCalledWith('defaultvalue')
3867+
})
3868+
})

packages/tailwindcss/src/compat/plugin-api.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export type PluginAPI = {
7171
): void
7272

7373
theme(path: string, defaultValue?: any): any
74-
config(path: string, defaultValue?: any): any
74+
config(path?: string, defaultValue?: any): any
7575
prefix(className: string): string
7676
}
7777

@@ -399,6 +399,8 @@ export function buildPluginApi(
399399
config(path, defaultValue) {
400400
let obj: Record<any, any> = resolvedConfig
401401

402+
if (!path) return obj
403+
402404
let keypath = toKeyPath(path)
403405

404406
for (let i = 0; i < keypath.length; ++i) {

0 commit comments

Comments
 (0)