Skip to content

Commit a64e209

Browse files
Warn on use of plugin parameters as function (tailwindlabs#14661)
Quick follow-up to tailwindlabs#14659 base don @thecrypticace's idea: - This behavior is no longer added to the types of the Plugin API to be consistent with v3 - When the plugin argument is used as a function, we now warn the first time
1 parent 99f2127 commit a64e209

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

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

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, test } from 'vitest'
1+
import { expect, test, vi } from 'vitest'
22
import { buildDesignSystem } from '../../design-system'
33
import { Theme } from '../../theme'
44
import { resolveConfig } from './resolve-config'
@@ -172,7 +172,12 @@ test('theme keys can reference other theme keys using the theme function regardl
172172
})
173173
})
174174

175-
test('theme keys can read from the CSS theme', () => {
175+
test('theme keys can read from the CSS theme', ({ onTestFinished }) => {
176+
let warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined)
177+
onTestFinished(() => {
178+
warn.mockReset()
179+
})
180+
176181
let theme = new Theme()
177182
theme.add('--color-green', 'green')
178183

@@ -202,7 +207,7 @@ test('theme keys can read from the CSS theme', () => {
202207
// Gives access to the colors object directly
203208
primary: colors.green,
204209
}),
205-
transitionColor: (theme) => ({
210+
transitionColor: (theme: any) => ({
206211
// The parameter object is also the theme function
207212
...theme('colors'),
208213
}),
@@ -247,4 +252,7 @@ test('theme keys can read from the CSS theme', () => {
247252
},
248253
},
249254
})
255+
expect(warn).toHaveBeenCalledWith(
256+
'Using the plugin object parameter as the theme function is deprecated. Please use the `theme` property instead.',
257+
)
250258
})

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

+19-7
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,8 @@ export function mergeThemeExtension(
116116
return undefined
117117
}
118118

119-
type ThemeFunction = (keypath: string, defaultValue?: any) => any
120-
export type PluginUtils = ThemeFunction & {
121-
theme: ThemeFunction
119+
export type PluginUtils = {
120+
theme: (keypath: string, defaultValue?: any) => any
122121
colors: typeof colors
123122
}
124123

@@ -176,12 +175,25 @@ function extractConfigs(ctx: ResolutionContext, { config, base, path }: ConfigFi
176175
ctx.configs.push(config)
177176
}
178177

178+
let didWarnAboutUsingObjectArgumentAsThemeFn = false
179+
179180
function mergeTheme(ctx: ResolutionContext) {
180181
let themeFn = createThemeFn(ctx.design, () => ctx.theme, resolveValue)
181-
let theme = Object.assign(themeFn, {
182-
theme: themeFn,
183-
colors,
184-
})
182+
let theme = Object.assign(
183+
(path: string, defaultValue?: any) => {
184+
if (!didWarnAboutUsingObjectArgumentAsThemeFn) {
185+
didWarnAboutUsingObjectArgumentAsThemeFn = true
186+
console.warn(
187+
'Using the plugin object parameter as the theme function is deprecated. Please use the `theme` property instead.',
188+
)
189+
}
190+
return themeFn(path, defaultValue)
191+
},
192+
{
193+
theme: themeFn,
194+
colors,
195+
},
196+
)
185197

186198
function resolveValue(value: ThemeValue | null | undefined): ResolvedThemeValue {
187199
if (typeof value === 'function') {

0 commit comments

Comments
 (0)