Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
ensure comments in @layer rules don't crash
  • Loading branch information
RobinMalfait committed Oct 22, 2021
commit eafeac50ce15d769cc6acf76baa4e552a9d6503f
28 changes: 21 additions & 7 deletions src/lib/generateRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,28 +448,42 @@ function generateRules(candidates, context) {
allRules.push(matches)
}

return allRules.flat(1).map(([{ sort, layer, options }, rule]) => {
if (options.respectImportant) {
if (context.tailwindConfig.important === true) {
// Strategy based on `tailwindConfig.important`
let strategy = ((important) => {
if (important === true) {
return (rule) => {
rule.walkDecls((d) => {
if (d.parent.type === 'rule' && !inKeyframes(d.parent)) {
d.important = true
}
})
} else if (typeof context.tailwindConfig.important === 'string') {
}
}

if (typeof important === 'string') {
return (rule) => {
rule.selectors = rule.selectors.map((selector) => {
return `${important} ${selector}`
})
}
}
})(context.tailwindConfig.important)

return allRules.flat(1).map(([{ sort, layer, options }, rule]) => {
if (options.respectImportant) {
if (strategy) {
let container = postcss.root({ nodes: [rule.clone()] })
container.walkRules((r) => {
if (inKeyframes(r)) {
return
}

r.selectors = r.selectors.map((selector) => {
return `${context.tailwindConfig.important} ${selector}`
})
strategy(r)
})
rule = container.nodes[0]
}
}

return [sort | context.layerOrder[layer], rule]
})
}
Expand Down
119 changes: 119 additions & 0 deletions tests/layer-at-rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,125 @@ test('custom user-land utilities', () => {
})
})

test('comments can be used inside layers without crashing', () => {
let config = {
content: [
{
raw: html`<div class="important-utility important-component"></div>`,
},
],
corePlugins: { preflight: false },
theme: {},
plugins: [],
}

let input = css`
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
/* Important base */
div {
background-color: #bada55;
}
}

@layer utilities {
/* Important utility */
.important-utility {
text-align: banana;
}
}

@layer components {
/* Important component */
.important-component {
text-align: banana;
}
}
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
/* Important base */
div {
background-color: #bada55;
}

/* Important component */
.important-component {
text-align: banana;
}

/* Important utility */
.important-utility {
text-align: banana;
}
`)
})
})

test('comments can be used inside layers (with important) without crashing', () => {
let config = {
important: true,
content: [
{
raw: html`<div class="important-utility important-component"></div>`,
},
],
corePlugins: { preflight: false },
theme: {},
plugins: [],
}

let input = css`
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
/* Important base */
div {
background-color: #bada55;
}
}

@layer utilities {
/* Important utility */
.important-utility {
text-align: banana;
}
}

@layer components {
/* Important component */
.important-component {
text-align: banana;
}
}
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
/* Important base */
div {
background-color: #bada55;
}

/* Important component */
.important-component {
text-align: banana;
}

/* Important utility */
.important-utility {
text-align: banana !important;
}
`)
})
})

test('layers are grouped and inserted at the matching @tailwind rule', () => {
let config = {
content: [
Expand Down