Skip to content

Commit 9c59d6d

Browse files
committed
Emit defaults from apply in css modules
1 parent 41e32bd commit 9c59d6d

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

src/lib/expandTailwindAtRules.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,16 @@ export default function expandTailwindAtRules(context) {
150150
}
151151
})
152152

153-
if (Object.values(layerNodes).every((n) => n === null)) {
153+
// We also want to check for @apply because the user can
154+
// apply classes in an isolated environment like CSS
155+
// modules and we still need to inject defaults
156+
let hasApply = false
157+
158+
root.walkAtRules('apply', () => {
159+
hasApply = true
160+
})
161+
162+
if (Object.values(layerNodes).every((n) => n === null) && !hasApply) {
154163
return root
155164
}
156165

tests/apply.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs'
22
import path from 'path'
3+
import { DEFAULTS_LAYER } from '../src/lib/expandTailwindAtRules.js'
34

45
import { run, html, css } from './util/run'
56

@@ -810,3 +811,39 @@ it('should be possible to apply user css without tailwind directives', () => {
810811
`)
811812
})
812813
})
814+
815+
fit('apply can emit defaults in isolated environments without @tailwind directives', () => {
816+
let config = {
817+
[DEFAULTS_LAYER]: true,
818+
experimental: { optimizeUniversalDefaults: true },
819+
820+
content: [{ raw: html`<div class="foo"></div>` }],
821+
}
822+
823+
let input = css`
824+
.foo {
825+
@apply focus:rotate-90;
826+
}
827+
`
828+
829+
return run(input, config).then((result) => {
830+
return expect(result.css).toMatchFormattedCss(css`
831+
.foo {
832+
--tw-translate-x: 0;
833+
--tw-translate-y: 0;
834+
--tw-rotate: 0;
835+
--tw-skew-x: 0;
836+
--tw-skew-y: 0;
837+
--tw-scale-x: 1;
838+
--tw-scale-y: 1;
839+
--tw-transform: translateX(var(--tw-translate-x)) translateY(var(--tw-translate-y))
840+
rotate(var(--tw-rotate)) skewX(var(--tw-skew-x)) skewY(var(--tw-skew-y))
841+
scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y));
842+
}
843+
.foo:focus {
844+
--tw-rotate: 90deg;
845+
transform: var(--tw-transform);
846+
}
847+
`)
848+
})
849+
})

0 commit comments

Comments
 (0)