Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Fixed

- Make `config()` path argument optional in v4 plugin API for backwards compat ([#14799](https://github.com/tailwindlabs/tailwindcss/pull/14799))
adamwathan marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
1 change: 1 addition & 0 deletions packages/tailwindcss/src/compat/config/resolve-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface ResolutionContext {

let minimal: ResolvedConfig = {
blocklist: [],
future: {},
prefix: '',
important: false,
darkMode: null,
Expand Down
9 changes: 9 additions & 0 deletions packages/tailwindcss/src/compat/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>
}
75 changes: 75 additions & 0 deletions packages/tailwindcss/src/compat/plugin-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
4 changes: 3 additions & 1 deletion packages/tailwindcss/src/compat/plugin-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down