Skip to content

Commit 19c3000

Browse files
committed
Always include css with apply in context
1 parent e8e64f0 commit 19c3000

5 files changed

+58
-17
lines changed

src/lib/normalizeTailwindDirectives.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import log from '../util/log'
22

33
export default function normalizeTailwindDirectives(root) {
4-
let tailwindDirectives = new Set()
5-
let layerDirectives = new Set()
4+
const tailwindDirectives = new Set()
5+
const layerDirectives = new Set()
6+
const applyDirectives = new Set()
67

78
root.walkAtRules((atRule) => {
9+
if (atRule.name === 'apply') {
10+
applyDirectives.add(atRule)
11+
return
12+
}
813
if (atRule.name === 'import') {
914
if (atRule.params === '"tailwindcss/base"' || atRule.params === "'tailwindcss/base'") {
1015
atRule.name = 'tailwind'
@@ -74,5 +79,5 @@ export default function normalizeTailwindDirectives(root) {
7479
}
7580
}
7681

77-
return tailwindDirectives
82+
return { tailwindDirectives, layerDirectives, applyDirectives }
7883
}

src/lib/setupTrackingContext.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,19 @@ function resolveChangedFiles(candidateFiles, fileModifiedMap) {
112112
// source path), or set up a new one (including setting up watchers and registering
113113
// plugins) then return it
114114
export default function setupTrackingContext(configOrPath) {
115-
return ({ tailwindDirectives, registerDependency }) => {
115+
return ({ tailwindDirectives, registerDependency, applyDirectives }) => {
116116
return (root, result) => {
117117
let [tailwindConfig, userConfigPath, tailwindConfigHash, configDependencies] =
118118
getTailwindConfig(configOrPath)
119119

120120
let contextDependencies = new Set(configDependencies)
121121

122-
// If there are no @tailwind rules, we don't consider this CSS file or it's dependencies
123-
// to be dependencies of the context. Can reuse the context even if they change.
122+
// If there are no @tailwind or @apply rules, we don't consider this CSS file or it's
123+
// dependencies to be dependencies of the context. Can reuse the context even if they change.
124124
// We may want to think about `@layer` being part of this trigger too, but it's tough
125125
// because it's impossible for a layer in one file to end up in the actual @tailwind rule
126126
// in another file since independent sources are effectively isolated.
127-
if (tailwindDirectives.size > 0) {
127+
if (tailwindDirectives.size > 0 || applyDirectives.size > 0) {
128128
// Add current css file as a context dependencies.
129129
contextDependencies.add(result.opts.from)
130130

@@ -147,12 +147,12 @@ export default function setupTrackingContext(configOrPath) {
147147

148148
let candidateFiles = getCandidateFiles(context, tailwindConfig)
149149

150-
// If there are no @tailwind rules, we don't consider this CSS file or it's dependencies
151-
// to be dependencies of the context. Can reuse the context even if they change.
150+
// If there are no @tailwind or @apply rules, we don't consider this CSS file or it's
151+
// dependencies to be dependencies of the context. Can reuse the context even if they change.
152152
// We may want to think about `@layer` being part of this trigger too, but it's tough
153153
// because it's impossible for a layer in one file to end up in the actual @tailwind rule
154154
// in another file since independent sources are effectively isolated.
155-
if (tailwindDirectives.size > 0) {
155+
if (tailwindDirectives.size > 0 || applyDirectives.size > 0) {
156156
let fileModifiedMap = getFileModifiedMap(context)
157157

158158
// Add template paths as postcss dependencies.

src/lib/setupWatchingContext.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -229,19 +229,19 @@ function resolveChangedFiles(context, candidateFiles) {
229229
// source path), or set up a new one (including setting up watchers and registering
230230
// plugins) then return it
231231
export default function setupWatchingContext(configOrPath) {
232-
return ({ tailwindDirectives, registerDependency }) => {
232+
return ({ tailwindDirectives, registerDependency, applyDirectives }) => {
233233
return (root, result) => {
234234
let [tailwindConfig, userConfigPath, tailwindConfigHash, configDependencies] =
235235
getTailwindConfig(configOrPath)
236236

237237
let contextDependencies = new Set(configDependencies)
238238

239-
// If there are no @tailwind rules, we don't consider this CSS file or it's dependencies
240-
// to be dependencies of the context. Can reuse the context even if they change.
239+
// If there are no @tailwind or @apply rules, we don't consider this CSS file or it's
240+
// dependencies to be dependencies of the context. Can reuse the context even if they change.
241241
// We may want to think about `@layer` being part of this trigger too, but it's tough
242242
// because it's impossible for a layer in one file to end up in the actual @tailwind rule
243243
// in another file since independent sources are effectively isolated.
244-
if (tailwindDirectives.size > 0) {
244+
if (tailwindDirectives.size > 0 || applyDirectives.size > 0) {
245245
// Add current css file as a context dependencies.
246246
contextDependencies.add(result.opts.from)
247247

@@ -299,7 +299,7 @@ export default function setupWatchingContext(configOrPath) {
299299
registerDependency({ type: 'dependency', file: touchFile })
300300
}
301301

302-
if (tailwindDirectives.size > 0) {
302+
if (tailwindDirectives.size > 0 || applyDirectives.size > 0) {
303303
for (let changedContent of resolvedChangedContent(context, candidateFiles)) {
304304
context.changedContent.push(changedContent)
305305
}

src/processTailwindFeatures.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import { issueFlagNotices } from './featureFlags'
1212

1313
export default function processTailwindFeatures(setupContext) {
1414
return function (root, result) {
15-
let tailwindDirectives = normalizeTailwindDirectives(root)
15+
const { tailwindDirectives, applyDirectives } = normalizeTailwindDirectives(root)
1616

17-
let context = setupContext({
17+
const context = setupContext({
1818
tailwindDirectives,
19+
applyDirectives,
1920
registerDependency(dependency) {
2021
result.messages.push({
2122
plugin: 'tailwindcss',

tests/apply.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -774,3 +774,38 @@ it('should not apply unrelated siblings when applying something from within atru
774774
`)
775775
})
776776
})
777+
778+
it('should be possible to apply user css without tailwind directives', () => {
779+
let config = {
780+
content: [{ raw: html`<div class="foo"></div>` }],
781+
plugins: [],
782+
}
783+
784+
let input = css`
785+
.bop {
786+
color: red;
787+
}
788+
.bar {
789+
background-color: blue;
790+
}
791+
.foo {
792+
@apply absolute bar bop;
793+
}
794+
`
795+
796+
return run(input, config).then((result) => {
797+
return expect(result.css).toMatchFormattedCss(css`
798+
.bop {
799+
color: red;
800+
}
801+
.bar {
802+
background-color: blue;
803+
}
804+
.foo {
805+
position: absolute;
806+
color: red;
807+
background-color: blue;
808+
}
809+
`)
810+
})
811+
})

0 commit comments

Comments
 (0)