Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit 3c8284d

Browse files
Add test for mutable state interactions (#142)
1 parent f3e00a7 commit 3c8284d

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

src/lib/utils.js

-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,6 @@ function nameClass(...args) {
8888
* Clone generated and/or cached nodes to ensure no future
8989
* postcss plugins can mutate the rules and mess up our cache
9090
*
91-
* NOTE: Only clone the nodes you pass to root.append()
92-
*
9391
* @param {import('postcss').Node[]} nodes
9492
* */
9593
function cloneNodes(nodes) {

tests/11-mutable.test.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.bg-foo {
2+
background-image: url("./foo.png");
3+
}

tests/11-mutable.test.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" href="/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Title</title>
8+
<link rel="stylesheet" href="./tailwind.css" />
9+
</head>
10+
<body>
11+
<div class="bg-foo"></div>
12+
</body>
13+
</html>

tests/11-mutable.test.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const postcss = require('postcss')
2+
const tailwind = require('../src/index.js')
3+
const fs = require('fs')
4+
const path = require('path')
5+
6+
function pluginThatMutatesRules() {
7+
return (root) => {
8+
root.walkRules((rule) => {
9+
rule.nodes.filter(node => node.prop === 'background-image').forEach(node => {
10+
node.value = 'url("./bar.png")'
11+
})
12+
13+
return rule
14+
})
15+
}
16+
}
17+
18+
function run(input, config = {}) {
19+
return postcss([tailwind(config)]).process(input, { from: path.resolve(__filename) })
20+
}
21+
22+
test.only('plugins mutating rules after tailwind doesnt break it', async () => {
23+
let config = {
24+
purge: [path.resolve(__dirname, './11-mutable.test.html')],
25+
theme: {
26+
backgroundImage: {
27+
'foo': 'url("./foo.png")',
28+
}
29+
},
30+
plugins: [],
31+
}
32+
33+
let css = `@tailwind utilities;`
34+
35+
function checkResult(result) {
36+
let expectedPath = path.resolve(__dirname, './11-mutable.test.css')
37+
let expected = fs.readFileSync(expectedPath, 'utf8')
38+
39+
expect(result.css).toMatchCss(expected)
40+
}
41+
42+
// Verify the first run produces the expected result
43+
let firstRun = await run(css, config)
44+
checkResult(firstRun)
45+
46+
// Outside of the context of tailwind jit more postcss plugins may operate on the AST:
47+
// In this case we have a plugin that mutates rules directly
48+
await postcss([ pluginThatMutatesRules() ]).process(firstRun, { from: path.resolve(__filename) })
49+
50+
// Verify subsequent runs don't produce mutated rules
51+
let secondRun = await run(css, config)
52+
checkResult(secondRun)
53+
})

0 commit comments

Comments
 (0)