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

Commit fb47a9e

Browse files
committed
Add test for context reuse
1 parent a423346 commit fb47a9e

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

tests/context-reuse.test.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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></body>
11+
</html>

tests/context-reuse.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const path = require('path')
2+
const process = require('child_process')
3+
const { promisify } = require('util')
4+
const exec = promisify(process.exec)
5+
6+
test('a build re-uses the context across multiple files with the same config', async () => {
7+
const filepath = path.resolve(__dirname, './context-reuse.worker.js')
8+
9+
const { stdout } = await exec(`node ${filepath}`)
10+
11+
const results = JSON.parse(stdout.trim())
12+
13+
expect(results[0].activeContexts).toBe(1)
14+
expect(results[1].activeContexts).toBe(1)
15+
expect(results[2].activeContexts).toBe(1)
16+
expect(results[3].activeContexts).toBe(1)
17+
})

tests/context-reuse.worker.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const fs = require("fs")
2+
const path = require("path")
3+
const postcss = require("postcss")
4+
const tailwind = require("../src/index.js")
5+
const sharedState = require("../src/lib/sharedState.js")
6+
const configPath = path.resolve(__dirname, './context-reuse.tailwind.config.js')
7+
8+
function run(input, config = {}, from = null) {
9+
from = from || path.resolve(__filename)
10+
11+
return postcss([tailwind(config)]).process(input, { from })
12+
}
13+
14+
async function runTest() {
15+
let messages = []
16+
17+
let config = {
18+
darkMode: 'class',
19+
purge: [path.resolve(__dirname, './context-reuse.test.html')],
20+
corePlugins: { preflight: false },
21+
theme: {},
22+
plugins: [],
23+
}
24+
25+
let from = path.resolve(__filename)
26+
27+
fs.writeFileSync(configPath, `module.exports = ${JSON.stringify(config)};`)
28+
29+
let results = [
30+
await run(`@tailwind utilities;`, configPath, `${from}?id=1`),
31+
await run(`body { @apply bg-blue-400; }`, configPath, `${from}?id=2`),
32+
await run(`body { @apply text-red-400; }`, configPath, `${from}?id=3`),
33+
await run(`body { @apply mb-4; }`, configPath, `${from}?id=4`),
34+
]
35+
36+
results.forEach(() => {
37+
messages.push({
38+
activeContexts: sharedState.contextSourcesMap.size,
39+
})
40+
})
41+
42+
process.stdout.write(JSON.stringify(messages))
43+
}
44+
45+
runTest().finally(() => {
46+
fs.unlinkSync(configPath)
47+
})

0 commit comments

Comments
 (0)