Skip to content

Commit 36a55d0

Browse files
committed
Run Tailwind CSS once for each root in a postcss document
1 parent 50802e1 commit 36a55d0

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/index.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ module.exports = function tailwindcss(configOrPath) {
1313
return root
1414
},
1515
function (root, result) {
16-
processTailwindFeatures(setupTrackingContext(configOrPath))(root, result)
16+
let context = setupTrackingContext(configOrPath)
17+
18+
if (root.type === 'document') {
19+
let roots = root.nodes.filter((node) => node.type === 'root')
20+
21+
for (const root of roots) {
22+
if (root.type === 'root') {
23+
processTailwindFeatures(context)(root, result)
24+
}
25+
}
26+
27+
return
28+
}
29+
30+
processTailwindFeatures(context)(root, result)
1731
},
1832
env.DEBUG &&
1933
function (root) {

tests/variants.test.js

+35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from 'fs'
22
import path from 'path'
3+
import postcss from 'postcss'
34

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

@@ -539,3 +540,37 @@ it('variants for utilities should not be produced in a file without a utilities
539540
`)
540541
})
541542
})
543+
544+
fit('appends variants to the correct place when using postcss documents', () => {
545+
let config = {
546+
content: [{ raw: html`<div class="underline sm:underline"></div>` }],
547+
plugins: [],
548+
corePlugins: { preflight: false },
549+
}
550+
551+
const doc = postcss.document()
552+
doc.append(postcss.parse(`a {}`))
553+
doc.append(postcss.parse(`@tailwind base`))
554+
doc.append(postcss.parse(`@tailwind utilities`))
555+
doc.append(postcss.parse(`b {}`))
556+
557+
const result = doc.toResult()
558+
559+
return run(result, config).then((result) => {
560+
return expect(result.css).toMatchFormattedCss(css`
561+
a {
562+
}
563+
${defaults}
564+
.underline {
565+
text-decoration-line: underline;
566+
}
567+
@media (min-width: 640px) {
568+
.sm\:underline {
569+
text-decoration-line: underline;
570+
}
571+
}
572+
b {
573+
}
574+
`)
575+
})
576+
})

0 commit comments

Comments
 (0)