Skip to content

Commit

Permalink
Add support for PostCSS Document nodes (#7291)
Browse files Browse the repository at this point in the history
* Run Tailwind CSS once for each root in a postcss document

* Update changelog
  • Loading branch information
thecrypticace committed Feb 25, 2022
1 parent cd8f109 commit 4fed060
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Allow default ring color to be a function ([#7587](https://github.com/tailwindlabs/tailwindcss/pull/7587))
- Add `rgb` and `hsl` color helpers for CSS variables ([#7665](https://github.com/tailwindlabs/tailwindcss/pull/7665))
- Support PostCSS `Document` nodes ([#7291](https://github.com/tailwindlabs/tailwindcss/pull/7291))

## [3.0.23] - 2022-02-16

Expand Down
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 4fed060

Please sign in to comment.