Skip to content

Commit 4dbb78c

Browse files
committed
Refactor
1 parent e27413d commit 4dbb78c

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

src/lib/cacheInvalidation.js

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ function getHash(str) {
2727
}
2828

2929
/**
30+
* Determine if the CSS tree is different from the
31+
* previous version for the given `sourcePath`.
32+
*
3033
* @param {string} sourcePath
3134
* @param {import('postcss').Node} root
3235
*/

tests/context-reuse.test.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ const configPath = path.resolve(__dirname, './context-reuse.tailwind.config.js')
77
const { css } = require('./util/run.js')
88

99
function run(input, config = {}, from = null) {
10-
from = from || path.resolve(__filename)
10+
from = [
11+
`${path.resolve(__filename)}?test=${expect.getState().currentTestName}`,
12+
from
13+
].join('&')
1114

1215
return postcss(tailwind(config)).process(input, { from })
1316
}
@@ -26,16 +29,14 @@ afterEach(async () => {
2629
})
2730

2831
it('re-uses the context across multiple files with the same config', async () => {
29-
let from = path.resolve(__filename)
30-
3132
let results = [
32-
await run(`@tailwind utilities;`, configPath, `${from}?id=1`),
33+
await run(`@tailwind utilities;`, configPath, `id=1`),
3334

3435
// Using @apply directives should still re-use the context
3536
// They depend on the config but do not the other way around
36-
await run(`body { @apply bg-blue-400; }`, configPath, `${from}?id=2`),
37-
await run(`body { @apply text-red-400; }`, configPath, `${from}?id=3`),
38-
await run(`body { @apply mb-4; }`, configPath, `${from}?id=4`),
37+
await run(`body { @apply bg-blue-400; }`, configPath, `id=2`),
38+
await run(`body { @apply text-red-400; }`, configPath, `id=3`),
39+
await run(`body { @apply mb-4; }`, configPath, `id=4`),
3940
]
4041

4142
let dependencies = results.map((result) => {
@@ -88,36 +89,35 @@ it('re-uses the context across multiple files with the same config', async () =>
8889

8990
it('invalidates the context when any CSS containing @tailwind directives changes', async () => {
9091
sharedState.contextInvalidationCount = 0
91-
92-
let from = path.resolve(__filename)
92+
sharedState.sourceHashMap.clear()
9393

9494
// Save the file a handful of times with no changes
9595
// This builds the context at most once
9696
for (let n = 0; n < 5; n++) {
97-
await run(`@tailwind utilities;`, configPath, `${from}?id=1`)
97+
await run(`@tailwind utilities;`, configPath, `id=1`)
9898
}
9999

100100
expect(sharedState.contextInvalidationCount).toBe(1)
101101

102102
// Save the file twice with a change
103103
// This should rebuild the context again but only once
104-
await run(`@tailwind utilities; .foo {}`, configPath, `${from}?id=1`)
105-
await run(`@tailwind utilities; .foo {}`, configPath, `${from}?id=1`)
104+
await run(`@tailwind utilities; .foo {}`, configPath, `id=1`)
105+
await run(`@tailwind utilities; .foo {}`, configPath, `id=1`)
106106

107107
expect(sharedState.contextInvalidationCount).toBe(2)
108108

109109
// Save the file twice with a content but not length change
110110
// This should rebuild the context two more times
111-
await run(`@tailwind utilities; .bar {}`, configPath, `${from}?id=1`)
112-
await run(`@tailwind utilities; .baz {}`, configPath, `${from}?id=1`)
111+
await run(`@tailwind utilities; .bar {}`, configPath, `id=1`)
112+
await run(`@tailwind utilities; .baz {}`, configPath, `id=1`)
113113

114114
expect(sharedState.contextInvalidationCount).toBe(4)
115115

116116
// Save a file with a change that does not affect the context
117117
// No invalidation should occur
118-
await run(`.foo { @apply mb-1; }`, configPath, `${from}?id=2`)
119-
await run(`.foo { @apply mb-1; }`, configPath, `${from}?id=2`)
120-
await run(`.foo { @apply mb-1; }`, configPath, `${from}?id=2`)
118+
await run(`.foo { @apply mb-1; }`, configPath, `id=2`)
119+
await run(`.foo { @apply mb-1; }`, configPath, `id=2`)
120+
await run(`.foo { @apply mb-1; }`, configPath, `id=2`)
121121

122122
expect(sharedState.contextInvalidationCount).toBe(4)
123123
})

0 commit comments

Comments
 (0)