From d9f80ba12827bdeba18e0dabd7a7788583193e8a Mon Sep 17 00:00:00 2001 From: Adam Wathan <4323180+adamwathan@users.noreply.github.com> Date: Thu, 7 Mar 2024 21:31:57 -0500 Subject: [PATCH 1/2] Don't crash on @apply with extra whitespace --- packages/tailwindcss/src/index.test.ts | 16 ++++++++++++++++ packages/tailwindcss/src/index.ts | 1 + 2 files changed, 17 insertions(+) diff --git a/packages/tailwindcss/src/index.test.ts b/packages/tailwindcss/src/index.test.ts index 521841cbe5ce..cf7025102e5b 100644 --- a/packages/tailwindcss/src/index.test.ts +++ b/packages/tailwindcss/src/index.test.ts @@ -288,6 +288,22 @@ describe('@apply', () => { `[Error: Cannot apply unknown utility class: hocus:bg-red-500]`, ) }) + + it('should not error with trailing whitespace', () => { + expect( + compileCss(` + @tailwind utilities; + + .foo { + @apply flex ; + } + `), + ).toMatchInlineSnapshot(` + ".foo { + display: flex; + }" + `) + }) }) describe('arbitrary variants', () => { diff --git a/packages/tailwindcss/src/index.ts b/packages/tailwindcss/src/index.ts index 59fe7a21c870..d066a5a1f335 100644 --- a/packages/tailwindcss/src/index.ts +++ b/packages/tailwindcss/src/index.ts @@ -115,6 +115,7 @@ export function compile(css: string, rawCandidates: string[]) { if (node.kind === 'rule' && node.selector[0] === '@' && node.selector.startsWith('@apply')) { let candidates = node.selector .slice(7 /* Ignore `@apply ` when parsing the selector */) + .trim() .split(/\s+/g) // Replace the `@apply` rule with the actual utility classes From dff173b67a349a9fef04e35ba0340d1e6124dd59 Mon Sep 17 00:00:00 2001 From: Adam Wathan <4323180+adamwathan@users.noreply.github.com> Date: Thu, 7 Mar 2024 21:48:37 -0500 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c67efe3e85ab..745f31cf2135 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Don't error on `@apply` with leading/trailing whitespace ([#13144](https://github.com/tailwindlabs/tailwindcss/pull/13144)) + ## [4.0.0-alpha.6] - 2024-03-07 ### Fixed