Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move custom CSS within layers to corresponding Tailwind layer #2312

Merged
merged 2 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- New `place-items`, `place-content`, `place-self`, `justify-items`, and `justify-self` utilities ([#2306](https://github.com/tailwindlabs/tailwindcss/pull/2306))
- Support configuring variants as functions ([#2309](https://github.com/tailwindlabs/tailwindcss/pull/2309))

### Changed

- CSS within `@layer` at-rules are now grouped with the corresponding `@tailwind` at-rule ([#2312](https://github.com/tailwindlabs/tailwindcss/pull/2312))

### Deprecated

- `conservative` purge mode, deprecated in favor of `layers`
Expand Down
93 changes: 93 additions & 0 deletions __tests__/layerAtRule.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import postcss from 'postcss'
import tailwind from '../src/index'

function run(input, config = {}) {
return postcss([tailwind({ corePlugins: [], ...config })]).process(input, { from: undefined })
}

test('layers are grouped and inserted at the matching @tailwind rule', () => {
const input = `
@layer vanilla {
strong { font-weight: medium }
}

@tailwind base;
@tailwind components;
@tailwind utilities;

@layer components {
.btn { background: blue }
}

@layer utilities {
.align-banana { text-align: banana }
}

@layer base {
h1 { font-weight: bold }
}

@layer components {
.card { border-radius: 12px }
}

@layer base {
p { font-weight: normal }
}

@layer utilities {
.align-sandwich { text-align: sandwich }
}

@layer chocolate {
a { text-decoration: underline }
}
`

const expected = `
@layer vanilla {
strong { font-weight: medium }
}

body { margin: 0 }
h1 { font-weight: bold }
p { font-weight: normal }

.input { background: white }
.btn { background: blue }
.card { border-radius: 12px }

.float-squirrel { float: squirrel }
.align-banana { text-align: banana }
.align-sandwich { text-align: sandwich }

@layer chocolate {
a { text-decoration: underline }
}
`

expect.assertions(2)

return run(input, {
plugins: [
function({ addBase, addComponents, addUtilities }) {
addBase({
body: {
margin: 0,
},
})

addComponents({
'.input': { background: 'white' },
})

addUtilities({
'.float-squirrel': { float: 'squirrel' },
})
},
],
}).then(result => {
expect(result.css).toMatchCss(expected)
expect(result.warnings().length).toBe(0)
})
})
5 changes: 5 additions & 0 deletions src/lib/convertLayerAtRulesToControlComments.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export default function convertLayerAtRulesToControlComments() {
return function(css) {
css.walkAtRules('layer', atRule => {
const layer = atRule.params

if (!['base', 'components', 'utilities'].includes(layer)) {
return
}

atRule.before(postcss.comment({ text: `tailwind start ${layer}` }))
atRule.before(atRule.nodes)
atRule.before(postcss.comment({ text: `tailwind end ${layer}` }))
Expand Down
16 changes: 16 additions & 0 deletions src/lib/substituteTailwindAtRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ export default function(
})

let includesScreensExplicitly = false
const layers = {
base: [],
components: [],
utilities: [],
}

css.walkAtRules('layer', atRule => {
if (!['base', 'components', 'utilities'].includes(atRule.params)) {
return
}

layers[atRule.params].push(atRule)
})

css.walkAtRules('tailwind', atRule => {
if (atRule.params === 'preflight') {
Expand All @@ -49,14 +62,17 @@ export default function(
}

if (atRule.params === 'base') {
atRule.after(layers.base)
atRule.after(updateSource(pluginBase, atRule.source))
}

if (atRule.params === 'components') {
atRule.after(layers.components)
atRule.after(updateSource(pluginComponents, atRule.source))
}

if (atRule.params === 'utilities') {
atRule.after(layers.utilities)
atRule.after(updateSource(pluginUtilities, atRule.source))
}

Expand Down