Skip to content

Commit

Permalink
Run Tailwind CSS once for each root in a postcss document
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed Feb 21, 2022
1 parent 93e0fdf commit afb4a9c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/index.js
Expand Up @@ -13,7 +13,21 @@ module.exports = function tailwindcss(configOrPath) {
return root
},
function (root, result) {
processTailwindFeatures(setupTrackingContext(configOrPath))(root, result)
let context = setupTrackingContext(configOrPath)

if (root.type === 'document') {
let roots = root.nodes.filter((node) => node.type === 'root')

for (const root of roots) {
if (root.type === 'root') {
processTailwindFeatures(context)(root, result)
}
}

return
}

processTailwindFeatures(context)(root, result)
},
env.DEBUG &&
function (root) {
Expand Down
35 changes: 35 additions & 0 deletions tests/variants.test.js
@@ -1,5 +1,6 @@
import fs from 'fs'
import path from 'path'
import postcss from 'postcss'

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

Expand Down Expand Up @@ -568,3 +569,37 @@ test('The visited variant removes opacity support', () => {
`)
})
})

it('appends variants to the correct place when using postcss documents', () => {
let config = {
content: [{ raw: html`<div class="underline sm:underline"></div>` }],
plugins: [],
corePlugins: { preflight: false },
}

const doc = postcss.document()
doc.append(postcss.parse(`a {}`))
doc.append(postcss.parse(`@tailwind base`))
doc.append(postcss.parse(`@tailwind utilities`))
doc.append(postcss.parse(`b {}`))

const result = doc.toResult()

return run(result, config).then((result) => {
return expect(result.css).toMatchFormattedCss(css`
a {
}
${defaults}
.underline {
text-decoration-line: underline;
}
@media (min-width: 640px) {
.sm\:underline {
text-decoration-line: underline;
}
}
b {
}
`)
})
})

0 comments on commit afb4a9c

Please sign in to comment.