Skip to content

Commit 058a925

Browse files
Emit defaults from apply in css modules (#6875)
* Emit defaults from apply in css modules * Update changelog
1 parent 41e32bd commit 058a925

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Fixed
1111

12-
- Fix `@apply` in files without `@tailwind` directives ([#6580](https://github.com/tailwindlabs/tailwindcss/pull/6580))
12+
- Fix `@apply` in files without `@tailwind` directives ([#6580](https://github.com/tailwindlabs/tailwindcss/pull/6580), [#6875](https://github.com/tailwindlabs/tailwindcss/pull/6875))
1313
- CLI: avoid unnecessary writes to output files ([#6550](https://github.com/tailwindlabs/tailwindcss/pull/6550))
1414

1515
## [3.0.9] - 2022-01-03

src/lib/expandTailwindAtRules.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,28 @@ export default function expandTailwindAtRules(context) {
140140
variants: null,
141141
}
142142

143-
// Make sure this file contains Tailwind directives. If not, we can save
144-
// a lot of work and bail early. Also we don't have to register our touch
145-
// file as a dependency since the output of this CSS does not depend on
146-
// the source of any templates. Think Vue <style> blocks for example.
147-
root.walkAtRules('tailwind', (rule) => {
148-
if (Object.keys(layerNodes).includes(rule.params)) {
149-
layerNodes[rule.params] = rule
143+
let hasApply = false
144+
145+
root.walkAtRules((rule) => {
146+
// Make sure this file contains Tailwind directives. If not, we can save
147+
// a lot of work and bail early. Also we don't have to register our touch
148+
// file as a dependency since the output of this CSS does not depend on
149+
// the source of any templates. Think Vue <style> blocks for example.
150+
if (rule.name === 'tailwind') {
151+
if (Object.keys(layerNodes).includes(rule.params)) {
152+
layerNodes[rule.params] = rule
153+
}
154+
}
155+
156+
// We also want to check for @apply because the user can
157+
// apply classes in an isolated environment like CSS
158+
// modules and we still need to inject defaults
159+
if (rule.name === 'apply') {
160+
hasApply = true
150161
}
151162
})
152163

153-
if (Object.values(layerNodes).every((n) => n === null)) {
164+
if (Object.values(layerNodes).every((n) => n === null) && !hasApply) {
154165
return root
155166
}
156167

tests/apply.test.js

+37
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)