Skip to content

Commit

Permalink
Preserve source maps for generated CSS (#7588)
Browse files Browse the repository at this point in the history
* Preserve source maps for `@apply`

* Overwrite the source for all cloned descendants

* Preserve source maps when expanding defaults

* Verify that source maps are correctly generated

* Update changelog
  • Loading branch information
thecrypticace committed Feb 23, 2022
1 parent d72b277 commit b94d565
Show file tree
Hide file tree
Showing 9 changed files with 532 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Prevent nesting plugin from breaking other plugins ([#7563](https://github.com/tailwindlabs/tailwindcss/pull/7563))
- Recursively collapse adjacent rules ([#7565](https://github.com/tailwindlabs/tailwindcss/pull/7565))
- Allow default ring color to be a function ([#7587](https://github.com/tailwindlabs/tailwindcss/pull/7587))
- Preserve source maps for generated CSS ([#7588](https://github.com/tailwindlabs/tailwindcss/pull/7588))

## [3.0.23] - 2022-02-16

Expand Down
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -52,7 +52,8 @@
"jest-diff": "^27.5.1",
"prettier": "^2.5.1",
"prettier-plugin-tailwindcss": "^0.1.7",
"rimraf": "^3.0.0"
"rimraf": "^3.0.0",
"source-map-js": "^1.0.2"
},
"peerDependencies": {
"autoprefixer": "^10.0.2",
Expand Down
10 changes: 8 additions & 2 deletions src/lib/expandApplyAtRules.js
Expand Up @@ -140,7 +140,7 @@ function processApply(root, context) {
for (let apply of applies) {
let candidates = perParentApplies.get(apply.parent) || []

perParentApplies.set(apply.parent, candidates)
perParentApplies.set(apply.parent, [candidates, apply.source])

let [applyCandidates, important] = extractApplyCandidates(apply.params)

Expand Down Expand Up @@ -178,7 +178,7 @@ function processApply(root, context) {
}
}

for (const [parent, candidates] of perParentApplies) {
for (const [parent, [candidates, atApplySource]] of perParentApplies) {
let siblings = []

for (let [applyCandidate, important, rules] of candidates) {
Expand Down Expand Up @@ -220,6 +220,12 @@ function processApply(root, context) {
}

let root = postcss.root({ nodes: [node.clone()] })

// Make sure every node in the entire tree points back at the @apply rule that generated it
root.walk((node) => {
node.source = atApplySource
})

let canRewriteSelector =
node.type !== 'atrule' || (node.type === 'atrule' && node.name !== 'keyframes')

Expand Down
8 changes: 6 additions & 2 deletions src/lib/resolveDefaultsAtRules.js
Expand Up @@ -120,15 +120,19 @@ export default function resolveDefaultsAtRules({ tailwindConfig }) {
}

for (let [, selectors] of selectorGroups) {
let universalRule = postcss.rule()
let universalRule = postcss.rule({
source: universal.source,
})

universalRule.selectors = [...selectors]

universalRule.append(universal.nodes.map((node) => node.clone()))
universal.before(universalRule)
}
} else {
let universalRule = postcss.rule()
let universalRule = postcss.rule({
source: universal.source,
})

universalRule.selectors = ['*', '::before', '::after']

Expand Down
6 changes: 6 additions & 0 deletions src/util/cloneNodes.js
Expand Up @@ -4,6 +4,12 @@ export default function cloneNodes(nodes, source) {

if (source !== undefined) {
cloned.source = source

if ('walk' in cloned) {
cloned.walk((child) => {
child.source = source
})
}
}

return cloned
Expand Down

0 comments on commit b94d565

Please sign in to comment.