Skip to content

Commit 2dcb1fc

Browse files
committed
Fix source maps of variant utilities that come from an @layer rule (#12508)
* Refactor * Keep traversing sibling nodes * Make sure the root node has a source location for the end * Add source map test for at-rule variants * Update changelog
1 parent adb6f15 commit 2dcb1fc

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
- Ensure configured `font-feature-settings` for `mono` are included in Preflight ([#12342](https://github.com/tailwindlabs/tailwindcss/pull/12342))
1616
- Don't crash when given applying a variant to a negated version of a simple utility ([#12514](https://github.com/tailwindlabs/tailwindcss/pull/12514))
1717
- Fix support for slashes in arbitrary modifiers ([#12515](https://github.com/tailwindlabs/tailwindcss/pull/12515))
18+
- Fix source maps of variant utilities that come from an `@layer` rule ([#12508](https://github.com/tailwindlabs/tailwindcss/pull/12508))
1819

1920
## [3.3.5] - 2023-10-25
2021

src/lib/expandTailwindAtRules.js

+3
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ export default function expandTailwindAtRules(context) {
265265
)
266266
}
267267

268+
// TODO: Why is the root node having no source location for `end` possible?
269+
root.source.end = root.source.end ?? root.source.start
270+
268271
// If we've got a utility layer and no utilities are generated there's likely something wrong
269272
const hasUtilityVariants = variantNodes.some(
270273
(node) => node.raws.tailwind?.parentLayer === 'utilities'

src/util/cloneNodes.js

+35-14
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
1+
/**
2+
* @param {import('postcss').Container[]} nodes
3+
* @param {any} source
4+
* @param {any} raws
5+
* @returns {import('postcss').Container[]}
6+
*/
17
export default function cloneNodes(nodes, source = undefined, raws = undefined) {
28
return nodes.map((node) => {
39
let cloned = node.clone()
410

5-
// We always want override the source map
6-
// except when explicitly told not to
7-
let shouldOverwriteSource = node.raws.tailwind?.preserveSource !== true || !cloned.source
8-
9-
if (source !== undefined && shouldOverwriteSource) {
10-
cloned.source = source
11-
12-
if ('walk' in cloned) {
13-
cloned.walk((child) => {
14-
child.source = source
15-
})
16-
}
17-
}
18-
1911
if (raws !== undefined) {
2012
cloned.raws.tailwind = {
2113
...cloned.raws.tailwind,
2214
...raws,
2315
}
2416
}
2517

18+
if (source !== undefined) {
19+
traverse(cloned, (node) => {
20+
// Do not traverse nodes that have opted
21+
// to preserve their original source
22+
let shouldPreserveSource = node.raws.tailwind?.preserveSource === true && node.source
23+
if (shouldPreserveSource) {
24+
return false
25+
}
26+
27+
// Otherwise we can safely replace the source
28+
// And continue traversing
29+
node.source = source
30+
})
31+
}
32+
2633
return cloned
2734
})
2835
}
36+
37+
/**
38+
* Traverse a tree of nodes and don't traverse children if the callback
39+
* returns false. Ideally we'd use Container#walk instead of this
40+
* function but it stops traversing siblings too.
41+
*
42+
* @param {import('postcss').Container} node
43+
* @param {(node: import('postcss').Container) => boolean} onNode
44+
*/
45+
function traverse(node, onNode) {
46+
if (onNode(node) !== false) {
47+
node.each?.((child) => traverse(child, onNode))
48+
}
49+
}

tests/source-maps.test.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ crosscheck(({ stable, oxide }) => {
485485
'source maps for layer rules are not rewritten to point to @tailwind directives',
486486
async () => {
487487
let config = {
488-
content: [{ raw: `font-normal foo hover:foo` }],
488+
content: [{ raw: `font-normal foo hover:foo lg:foo` }],
489489
}
490490

491491
let utilitiesFile = postcss.parse(
@@ -535,6 +535,11 @@ crosscheck(({ stable, oxide }) => {
535535
'3:12 -> 7:12',
536536
'4:14-35 -> 8:14-35',
537537
'5:12 -> 9:12',
538+
'1:0 -> 10:12',
539+
'3:12 -> 11:12',
540+
'4:14-35 -> 12:14-35',
541+
'5:12 -> 13:12',
542+
'1:0 -> 14:0',
538543
])
539544
}
540545
)

0 commit comments

Comments
 (0)