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

Add simple JS config migration #14639

Merged
merged 16 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- _Upgrade (experimental)_: Migrate v3 PostCSS setups to v4 in some cases ([#14612](https://github.com/tailwindlabs/tailwindcss/pull/14612))
- _Upgrade (experimental)_: The upgrade tool now automatically discovers your JavaScript config ([#14597](https://github.com/tailwindlabs/tailwindcss/pull/14597))
- _Upgrade (experimental)_: Migrate legacy classes to the v4 alternative ([#14643](https://github.com/tailwindlabs/tailwindcss/pull/14643))
- _Upgrade (experimental)_: Fully convert simple JS configs to CSS ([#14639](https://github.com/tailwindlabs/tailwindcss/pull/14639))
philipp-spiess marked this conversation as resolved.
Show resolved Hide resolved

### Fixed

Expand Down
41 changes: 20 additions & 21 deletions integrations/upgrade/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ test(

--- ./src/input.css ---
@import 'tailwindcss';
@config '../tailwind.config.js';

@source './**/*.{html,js}';
"
`)

Expand Down Expand Up @@ -71,8 +72,9 @@ test(
}
`,
'src/index.html': html`
<h1>🤠👋</h1>
philipp-spiess marked this conversation as resolved.
Show resolved Hide resolved
<div class="!tw__flex sm:!tw__block tw__bg-gradient-to-t flex [color:red]"></div>
<div
class="!tw__flex sm:!tw__block tw__bg-gradient-to-t flex [color:red]"
></div>
`,
'src/input.css': css`
@tailwind base;
Expand All @@ -91,13 +93,14 @@ test(
expect(await fs.dumpFiles('./src/**/*.{css,html}')).toMatchInlineSnapshot(`
"
--- ./src/index.html ---
<h1>🤠👋</h1>
<div class="tw:flex! tw:sm:block! tw:bg-linear-to-t flex tw:[color:red]"></div>
<div
class="tw:flex! tw:sm:block! tw:bg-linear-to-t flex tw:[color:red]"
></div>

--- ./src/input.css ---
@import 'tailwindcss' prefix(tw);

@config '../tailwind.config.js';
@source './**/*.{html,js}';

.btn {
@apply tw:rounded-md! tw:px-2 tw:py-1 tw:bg-blue-500 tw:text-white;
Expand Down Expand Up @@ -145,8 +148,6 @@ test(
--- ./src/index.css ---
@import 'tailwindcss';

@config '../tailwind.config.js';

.a {
@apply flex;
}
Expand Down Expand Up @@ -201,8 +202,6 @@ test(
--- ./src/index.css ---
@import 'tailwindcss';

@config '../tailwind.config.js';

@layer base {
html {
color: #333;
Expand Down Expand Up @@ -262,8 +261,6 @@ test(
--- ./src/index.css ---
@import 'tailwindcss';

@config '../tailwind.config.js';

@utility btn {
@apply rounded-md px-2 py-1 bg-blue-500 text-white;
}
Expand Down Expand Up @@ -631,7 +628,6 @@ test(
--- ./src/index.css ---
@import 'tailwindcss';
@import './utilities.css';
@config '../tailwind.config.js';

--- ./src/utilities.css ---
@utility no-scrollbar {
Expand Down Expand Up @@ -748,7 +744,6 @@ test(
@import './c.1.css' layer(utilities);
@import './c.1.utilities.css';
@import './d.1.css';
@config '../tailwind.config.js';

--- ./src/a.1.css ---
@import './a.1.utilities.css'
Expand Down Expand Up @@ -882,17 +877,14 @@ test(
--- ./src/root.1.css ---
@import 'tailwindcss/utilities' layer(utilities);
@import './a.1.css' layer(utilities);
@config '../tailwind.config.js';

--- ./src/root.2.css ---
@import 'tailwindcss/utilities' layer(utilities);
@import './a.1.css' layer(components);
@config '../tailwind.config.js';

--- ./src/root.3.css ---
@import 'tailwindcss/utilities' layer(utilities);
@import './a.1.css' layer(utilities);
@config '../tailwind.config.js';
"
`)
philipp-spiess marked this conversation as resolved.
Show resolved Hide resolved
},
Expand All @@ -912,11 +904,17 @@ test(
'tailwind.config.ts': js`
export default {
content: ['./src/**/*.{html,js}'],
plugins: [
() => {
// custom stuff which is too complicated to migrate to CSS
},
],
}
`,
'src/index.html': html`
<h1>🤠👋</h1>
<div class="!flex sm:!block bg-gradient-to-t bg-[--my-red]"></div>
<div
class="!flex sm:!block bg-gradient-to-t bg-[--my-red]"
></div>
`,
'src/root.1.css': css`
/* Inject missing @config */
Expand Down Expand Up @@ -968,8 +966,9 @@ test(
expect(await fs.dumpFiles('./src/**/*.{html,css}')).toMatchInlineSnapshot(`
"
--- ./src/index.html ---
<h1>🤠👋</h1>
<div class="flex! sm:block! bg-linear-to-t bg-[var(--my-red)]"></div>
<div
class="flex! sm:block! bg-linear-to-t bg-[var(--my-red)]"
></div>

--- ./src/root.1.css ---
/* Inject missing @config */
Expand Down
152 changes: 152 additions & 0 deletions integrations/upgrade/js-config.test.ts
philipp-spiess marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { expect } from 'vitest'
import { css, json, test, ts } from '../utils'

test(
`upgrades a simple JS config file to CSS`,
{
fs: {
'package.json': json`
{
"dependencies": {
"@tailwindcss/upgrade": "workspace:^"
}
}
`,
'tailwind.config.ts': ts`
import { type Config } from 'tailwindcss'
import defaultTheme from 'tailwindcss/defaultTheme'

module.exports = {
darkMode: 'selector',
content: ['./src/**/*.{html,js}', './my-app/**/*.{html,js}'],
theme: {
boxShadow: {
sm: '0 2px 6px rgb(15 23 42 / 0.08)',
},
colors: {
red: {
400: '#f87171',
500: 'red',
},
},
fontSize: {
xs: ['0.75rem', { lineHeight: '1rem' }],
sm: ['0.875rem', { lineHeight: '1.5rem' }],
base: ['1rem', { lineHeight: '2rem' }],
},
extend: {
colors: {
red: {
500: '#ef4444',
600: '#dc2626',
},
},
fontFamily: {
sans: 'Inter, system-ui, sans-serif',
display: ['Cabinet Grotesk', ...defaultTheme.fontFamily.sans],
},
borderRadius: {
'4xl': '2rem',
},
},
},
plugins: [],
} satisfies Config
`,
'src/input.css': css`
@tailwind base;
@tailwind components;
@tailwind utilities;
`,
},
},
async ({ exec, fs }) => {
await exec('npx @tailwindcss/upgrade')

expect(await fs.dumpFiles('src/**/*.css')).toMatchInlineSnapshot(`
"
--- src/input.css ---
@import 'tailwindcss';

@source './**/*.{html,js}';
@source '../my-app/**/*.{html,js}';

@variant dark (&:where(.dark, .dark *));

@theme {
--box-shadow-*: initial;
--box-shadow-sm: 0 2px 6px rgb(15 23 42 / 0.08);

--color-*: initial;
--color-red-400: #f87171;
--color-red-500: #ef4444;
--color-red-600: #dc2626;

--font-size-*: initial;
--font-size-xs: 0.75rem;
--font-size-xs--line-height: 1rem;
--font-size-sm: 0.875rem;
--font-size-sm--line-height: 1.5rem;
--font-size-base: 1rem;
--font-size-base--line-height: 2rem;

--font-family-sans: Inter, system-ui, sans-serif;
--font-family-display: Cabinet Grotesk, ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";

--radius-4xl: 2rem;
}
"
`)

expect((await fs.dumpFiles('tailwind.config.ts')).trim()).toBe('')
},
)
philipp-spiess marked this conversation as resolved.
Show resolved Hide resolved

test(
`does not upgrade a complex JS config file to CSS`,
{
fs: {
'package.json': json`
{
"dependencies": {
"@tailwindcss/upgrade": "workspace:^"
}
}
`,
'tailwind.config.ts': ts`
import { type Config } from 'tailwindcss'

export default {
plugins: [function complexConfig() {}],
} satisfies Config
`,
'src/input.css': css`
@tailwind base;
@tailwind components;
@tailwind utilities;
`,
},
},
async ({ exec, fs }) => {
await exec('npx @tailwindcss/upgrade')

expect(await fs.dumpFiles('src/**/*.css')).toMatchInlineSnapshot(`
"
--- src/input.css ---
@import 'tailwindcss';
@config '../tailwind.config.ts';
"
`)

expect(await fs.dumpFiles('tailwind.config.ts')).toMatchInlineSnapshot(`
"
--- tailwind.config.ts ---
import { type Config } from 'tailwindcss'

export default {
plugins: [function complexConfig() {}],
} satisfies Config
"
`)
},
)
2 changes: 1 addition & 1 deletion integrations/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function test(
) {
return (only || (!process.env.CI && debug) ? defaultTest.only : defaultTest)(
name,
{ timeout: TEST_TIMEOUT, retry: debug || only ? 0 : 3 },
{ timeout: TEST_TIMEOUT, retry: process.env.CI ? 2 : 0 },
async (options) => {
let rootDir = debug ? path.join(REPO_ROOT, '.debug') : TMP_ROOT
await fs.mkdir(rootDir, { recursive: true })
Expand Down
Loading