Skip to content

Commit 9b15a5c

Browse files
Don't generate invalid CSS when migrating a complex screens config (#14691)
Co-authored-by: Robin Malfait <[email protected]>
1 parent 5179faf commit 9b15a5c

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
- Ensure `theme` values defined outside of `extend` in JS configuration files overwrite all existing values for that namespace ([#14672](https://github.com/tailwindlabs/tailwindcss/pull/14672))
1717
- _Upgrade (experimental)_: Speed up template migrations ([#14679](https://github.com/tailwindlabs/tailwindcss/pull/14679))
18+
- _Upgrade (experimental)_: Don't generate invalid CSS when migrating a complex `screens` config ([#14691](https://github.com/tailwindlabs/tailwindcss/pull/14691))
1819

1920
## [4.0.0-alpha.27] - 2024-10-15
2021

integrations/upgrade/js-config.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,62 @@ test(
369369
`)
370370
},
371371
)
372+
373+
test(
374+
`does not upgrade JS config files with non-simple screens object`,
375+
{
376+
fs: {
377+
'package.json': json`
378+
{
379+
"dependencies": {
380+
"@tailwindcss/upgrade": "workspace:^"
381+
}
382+
}
383+
`,
384+
'tailwind.config.ts': ts`
385+
import { type Config } from 'tailwindcss'
386+
387+
export default {
388+
theme: {
389+
screens: {
390+
xl: { min: '1024px', max: '1279px' },
391+
tall: { raw: '(min-height: 800px)' },
392+
},
393+
},
394+
} satisfies Config
395+
`,
396+
'src/input.css': css`
397+
@tailwind base;
398+
@tailwind components;
399+
@tailwind utilities;
400+
`,
401+
},
402+
},
403+
async ({ exec, fs }) => {
404+
await exec('npx @tailwindcss/upgrade')
405+
406+
expect(await fs.dumpFiles('src/**/*.css')).toMatchInlineSnapshot(`
407+
"
408+
--- src/input.css ---
409+
@import 'tailwindcss';
410+
@config '../tailwind.config.ts';
411+
"
412+
`)
413+
414+
expect(await fs.dumpFiles('tailwind.config.ts')).toMatchInlineSnapshot(`
415+
"
416+
--- tailwind.config.ts ---
417+
import { type Config } from 'tailwindcss'
418+
419+
export default {
420+
theme: {
421+
screens: {
422+
xl: { min: '1024px', max: '1279px' },
423+
tall: { raw: '(min-height: 800px)' },
424+
},
425+
},
426+
} satisfies Config
427+
"
428+
`)
429+
},
430+
)

packages/@tailwindcss-upgrade/src/migrate-js-config.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,9 @@ function canMigrateConfig(unresolvedConfig: Config, source: string): boolean {
226226
// migrated
227227
let theme = unresolvedConfig.theme
228228
if (theme && typeof theme === 'object') {
229-
if (theme.extend && !onlyUsesAllowedTopLevelKeys(theme.extend)) return false
229+
if (theme.extend && !onlyAllowedThemeValues(theme.extend)) return false
230230
let { extend: _extend, ...themeCopy } = theme
231-
if (!onlyUsesAllowedTopLevelKeys(themeCopy)) return false
231+
if (!onlyAllowedThemeValues(themeCopy)) return false
232232
}
233233

234234
return true
@@ -239,12 +239,20 @@ const DEFAULT_THEME_KEYS = [
239239
// Used by @tailwindcss/container-queries
240240
'containers',
241241
]
242-
function onlyUsesAllowedTopLevelKeys(theme: ThemeConfig): boolean {
242+
function onlyAllowedThemeValues(theme: ThemeConfig): boolean {
243243
for (let key of Object.keys(theme)) {
244244
if (!DEFAULT_THEME_KEYS.includes(key)) {
245245
return false
246246
}
247247
}
248+
249+
if ('screens' in theme && typeof theme.screens === 'object' && theme.screens !== null) {
250+
for (let screen of Object.values(theme.screens)) {
251+
if (typeof screen === 'object' && screen !== null && ('max' in screen || 'raw' in screen)) {
252+
return false
253+
}
254+
}
255+
}
248256
return true
249257
}
250258

0 commit comments

Comments
 (0)