Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace committed Feb 25, 2022
1 parent e27413d commit 4dbb78c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/lib/cacheInvalidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ function getHash(str) {
}

/**
* Determine if the CSS tree is different from the
* previous version for the given `sourcePath`.
*
* @param {string} sourcePath
* @param {import('postcss').Node} root
*/
Expand Down
34 changes: 17 additions & 17 deletions tests/context-reuse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const configPath = path.resolve(__dirname, './context-reuse.tailwind.config.js')
const { css } = require('./util/run.js')

function run(input, config = {}, from = null) {
from = from || path.resolve(__filename)
from = [
`${path.resolve(__filename)}?test=${expect.getState().currentTestName}`,
from
].join('&')

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

it('re-uses the context across multiple files with the same config', async () => {
let from = path.resolve(__filename)

let results = [
await run(`@tailwind utilities;`, configPath, `${from}?id=1`),
await run(`@tailwind utilities;`, configPath, `id=1`),

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

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

it('invalidates the context when any CSS containing @tailwind directives changes', async () => {
sharedState.contextInvalidationCount = 0

let from = path.resolve(__filename)
sharedState.sourceHashMap.clear()

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

expect(sharedState.contextInvalidationCount).toBe(1)

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

expect(sharedState.contextInvalidationCount).toBe(2)

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

expect(sharedState.contextInvalidationCount).toBe(4)

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

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

0 comments on commit 4dbb78c

Please sign in to comment.