Skip to content

Commit 9b7fdc3

Browse files
PostCSS should not polyfill @property for CSS module files
1 parent 41e383a commit 9b7fdc3

File tree

6 files changed

+23
-9
lines changed

6 files changed

+23
-9
lines changed

integrations/postcss/next.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ test(
4848
'app/page.module.css': css`
4949
@reference './globals.css';
5050
.heading {
51-
@apply text-red-500 animate-ping;
51+
@apply text-red-500 animate-ping skew-7;
5252
}
5353
`,
5454
'app/globals.css': css`
@@ -77,9 +77,10 @@ test(
7777
])
7878

7979
await fs.expectFileToContain(moduleCss!, [
80-
'color:var(--color-red-500,oklch(.637 .237 25.331)',
80+
'color:var(--color-red-500,oklch(63.7% .237 25.331)',
8181
'animation:var(--animate-ping,ping 1s cubic-bezier(0,0,.2,1) infinite)',
8282
/@keyframes page_ping.*{75%,to{transform:scale\(2\);opacity:0}/,
83+
'--tw-skew-x:skewX(7deg);',
8384
])
8485
},
8586
)
@@ -130,7 +131,7 @@ describe.each(['turbo', 'webpack'])('%s', (bundler) => {
130131
'app/page.module.css': css`
131132
@reference './globals.css';
132133
.heading {
133-
@apply text-red-500 animate-ping content-['module'];
134+
@apply text-red-500 animate-ping skew-7 content-['module'];
134135
}
135136
`,
136137
'app/globals.css': css`
@@ -173,6 +174,7 @@ describe.each(['turbo', 'webpack'])('%s', (bundler) => {
173174
let css = await fetchStyles(url)
174175
expect(css).toContain(candidate`underline`)
175176
expect(css).toContain(candidate`bg-red-500`)
177+
expect(css).toContain('--tw-skew-x: skewX(7deg);')
176178
expect(css).toContain('content: var(--tw-content)')
177179
expect(css).toContain('@keyframes')
178180
})

packages/@tailwindcss-node/src/compile.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,29 @@ import {
99
compile as _compile,
1010
compileAst as _compileAst,
1111
Features,
12+
Polyfills,
1213
} from 'tailwindcss'
1314
import type { AstNode } from '../../tailwindcss/src/ast'
1415
import { getModuleDependencies } from './get-module-dependencies'
1516
import { rewriteUrls } from './urls'
1617

17-
export { Features }
18+
export { Features, Polyfills }
1819

1920
export type Resolver = (id: string, base: string) => Promise<string | false | undefined>
2021

2122
export interface CompileOptions {
2223
base: string
2324
onDependency: (path: string) => void
2425
shouldRewriteUrls?: boolean
26+
polyfills?: Polyfills
2527

2628
customCssResolver?: Resolver
2729
customJsResolver?: Resolver
2830
}
2931

3032
function createCompileOptions({
3133
base,
34+
polyfills,
3235
onDependency,
3336
shouldRewriteUrls,
3437

@@ -37,6 +40,7 @@ function createCompileOptions({
3740
}: CompileOptions) {
3841
return {
3942
base,
43+
polyfills,
4044
async loadModule(id: string, base: string) {
4145
return loadModule(id, base, onDependency, customJsResolver)
4246
},

packages/@tailwindcss-postcss/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Features,
66
Instrumentation,
77
optimize as optimizeCss,
8+
Polyfills,
89
} from '@tailwindcss/node'
910
import { clearRequireCache } from '@tailwindcss/node/require-cache'
1011
import { Scanner } from '@tailwindcss/oxide'
@@ -72,6 +73,7 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
7273
using I = new Instrumentation()
7374

7475
let inputFile = result.opts.from ?? ''
76+
let isCSSModuleFile = inputFile.endsWith('.module.css')
7577

7678
DEBUG && I.start(`[@tailwindcss/postcss] ${relative(base, inputFile)}`)
7779

@@ -119,6 +121,10 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
119121
onDependency: (path) => {
120122
context.fullRebuildPaths.push(path)
121123
},
124+
// In CSS Module files, we have to disable the `@property` polyfill since these will
125+
// emit global `*` rules which are considered to be non-pure and will cause builds
126+
// to fail.
127+
polyfills: isCSSModuleFile ? Polyfills.All ^ Polyfills.AtProperty : Polyfills.All,
122128
})
123129
DEBUG && I.end('Create compiler')
124130

packages/tailwindcss/src/ast.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ export function optimizeAst(
392392
// Collect fallbacks for `@property` rules for Firefox support
393393
// We turn these into rules on `:root` or `*` and some pseudo-elements
394394
// based on the value of `inherits``
395-
if (polyfills & Polyfills.PolyfillAtProperty) {
395+
if (polyfills & Polyfills.AtProperty) {
396396
let property = node.params
397397
let initialValue = null
398398
let inherits = false
@@ -556,7 +556,7 @@ export function optimizeAst(
556556
}
557557

558558
// Fallbacks
559-
if (polyfills & Polyfills.PolyfillAtProperty) {
559+
if (polyfills & Polyfills.AtProperty) {
560560
{
561561
let fallbackAst = []
562562

packages/tailwindcss/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ export const enum Polyfills {
4141
None = 0,
4242

4343
// Control if fallbacks for `@property` rules are emitted
44-
PolyfillAtProperty = 1 << 0,
44+
AtProperty = 1 << 0,
4545

4646
// Enable all
47-
All = PolyfillAtProperty,
47+
All = AtProperty,
4848
}
4949

5050
type CompileOptions = {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@reference "./globals.css";
2+
13
.heading {
2-
color: var(--color-red-500);
4+
@apply skew-7;
35
}

0 commit comments

Comments
 (0)