From 204b4444ca26cc7b4802d040a0353331892aae4c Mon Sep 17 00:00:00 2001 From: Philipp Spiess Date: Thu, 5 Dec 2024 11:56:43 +0100 Subject: [PATCH] Reference imports should not generate utilities --- CHANGELOG.md | 4 +++- packages/tailwindcss/src/index.test.ts | 30 ++++++++++++++++++++++++++ packages/tailwindcss/src/index.ts | 16 +++++++++++--- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e7e13bff111..07fa9c953329 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- Nothing yet! +### Fixed + +- Ensure `@import "…" reference` never generates utilities ([#15307](https://github.com/tailwindlabs/tailwindcss/pull/15307)) ## [4.0.0-beta.5] - 2024-12-04 diff --git a/packages/tailwindcss/src/index.test.ts b/packages/tailwindcss/src/index.test.ts index 1c4afe2efa93..de59e08be9a4 100644 --- a/packages/tailwindcss/src/index.test.ts +++ b/packages/tailwindcss/src/index.test.ts @@ -3138,6 +3138,36 @@ describe('`@import "…" reference`', () => { `) }) + test('does not generate utilities', async () => { + let loadStylesheet = async (id: string, base: string) => { + if (id === './foo/baz.css') { + return { + content: css` + @layer utilities { + @tailwind utilities; + } + `, + base: '/root/foo', + } + } + return { + content: css` + @import './foo/baz.css'; + `, + base: '/root/foo', + } + } + + let { build } = await compile( + css` + @import './foo/bar.css' reference; + `, + { loadStylesheet }, + ) + + expect(build(['text-underline', 'border']).trim()).toMatchInlineSnapshot(`"@layer utilities;"`) + }) + test('removes styles when the import resolver was handled outside of Tailwind CSS', async () => { await expect( compileCss( diff --git a/packages/tailwindcss/src/index.ts b/packages/tailwindcss/src/index.ts index 9904608edd41..cbfad6b3fded 100644 --- a/packages/tailwindcss/src/index.ts +++ b/packages/tailwindcss/src/index.ts @@ -388,11 +388,21 @@ async function parseCss( return WalkAction.Skip } - // Other at-rules, like `@media`, `@supports`, or `@layer` should - // be recursively traversed as these might be inserted by the - // `@import` resolution. + case '@media': + case '@supports': + case '@layer': { + // These rules should be recursively traversed as these might be + // inserted by the `@import` resolution. + return + } + + default: { + replaceWith([]) + return WalkAction.Skip + } } }) + node.nodes = [contextNode({ reference: true }, node.nodes)] }