Skip to content

Commit 1238d07

Browse files
Reference imports should not generate utilities (#15307)
We noticed an issue where the new `@import "…" reference` syntax was not throwing away `@tailwind` declarations, effectively causing you to create utility classes whenever you used this feature. This is especially noticed in setups with very strict compilers like Svelte. ## Test Plan ### Before <img width="1142" alt="Screenshot 2024-12-05 at 11 56 00" src="https://github.com/user-attachments/assets/546f0eb3-4401-48c9-9268-76992e899226"> ### After <img width="2560" alt="Screenshot 2024-12-05 at 12 27 30" src="https://github.com/user-attachments/assets/16732000-e02c-49bc-ac6f-91da0cfcc7e8">
1 parent 85da88f commit 1238d07

File tree

5 files changed

+51
-12
lines changed

5 files changed

+51
-12
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+
- Ensure `@import "…" reference` never generates utilities ([#15307](https://github.com/tailwindlabs/tailwindcss/pull/15307))
1113

1214
## [4.0.0-beta.5] - 2024-12-04
1315

integrations/vite/svelte.test.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ test(
4848
target: document.body,
4949
})
5050
`,
51-
'src/index.css': css`@import 'tailwindcss' reference;`,
51+
'src/index.css': css`@import 'tailwindcss';`,
5252
'src/App.svelte': html`
5353
<script>
5454
import './index.css'
@@ -58,7 +58,7 @@ test(
5858
<h1 class="global local underline">Hello {name}!</h1>
5959
6060
<style>
61-
@import 'tailwindcss/theme' theme(reference);
61+
@import 'tailwindcss' reference;
6262
@import './other.css';
6363
</style>
6464
`,
@@ -165,14 +165,11 @@ test(
165165
<h1 class="local global underline">Hello {name}!</h1>
166166
167167
<style>
168-
@import 'tailwindcss/theme' theme(reference);
168+
@import 'tailwindcss' reference;
169169
@import './other.css';
170170
</style>
171171
`,
172-
'src/index.css': css`
173-
@import 'tailwindcss/theme' theme(reference);
174-
@import 'tailwindcss/utilities';
175-
`,
172+
'src/index.css': css` @import 'tailwindcss'; `,
176173
'src/other.css': css`
177174
.local {
178175
@apply text-red-500;

integrations/vite/vue.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ test(
4545
`,
4646
'src/App.vue': html`
4747
<style>
48-
@import 'tailwindcss' reference;
48+
@import 'tailwindcss';
4949
.foo {
5050
@apply text-red-500;
5151
}

packages/tailwindcss/src/index.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -3138,6 +3138,36 @@ describe('`@import "…" reference`', () => {
31383138
`)
31393139
})
31403140

3141+
test('does not generate utilities', async () => {
3142+
let loadStylesheet = async (id: string, base: string) => {
3143+
if (id === './foo/baz.css') {
3144+
return {
3145+
content: css`
3146+
@layer utilities {
3147+
@tailwind utilities;
3148+
}
3149+
`,
3150+
base: '/root/foo',
3151+
}
3152+
}
3153+
return {
3154+
content: css`
3155+
@import './foo/baz.css';
3156+
`,
3157+
base: '/root/foo',
3158+
}
3159+
}
3160+
3161+
let { build } = await compile(
3162+
css`
3163+
@import './foo/bar.css' reference;
3164+
`,
3165+
{ loadStylesheet },
3166+
)
3167+
3168+
expect(build(['text-underline', 'border']).trim()).toMatchInlineSnapshot(`"@layer utilities;"`)
3169+
})
3170+
31413171
test('removes styles when the import resolver was handled outside of Tailwind CSS', async () => {
31423172
await expect(
31433173
compileCss(

packages/tailwindcss/src/index.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,21 @@ async function parseCss(
388388
return WalkAction.Skip
389389
}
390390

391-
// Other at-rules, like `@media`, `@supports`, or `@layer` should
392-
// be recursively traversed as these might be inserted by the
393-
// `@import` resolution.
391+
case '@media':
392+
case '@supports':
393+
case '@layer': {
394+
// These rules should be recursively traversed as these might be
395+
// inserted by the `@import` resolution.
396+
return
397+
}
398+
399+
default: {
400+
replaceWith([])
401+
return WalkAction.Skip
402+
}
394403
}
395404
})
405+
396406
node.nodes = [contextNode({ reference: true }, node.nodes)]
397407
}
398408

0 commit comments

Comments
 (0)