Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 78b17d2

Browse files
committedDec 11, 2020
ensure lookup tree is correctly cached based on used tailwind atrules
1 parent 5ece981 commit 78b17d2

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed
 

‎__tests__/applyAtRule.test.js

+38
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ test('you can apply utility classes that do not actually exist as long as they w
340340
test('shadow lookup will be constructed when we have missing @tailwind atrules', () => {
341341
const input = `
342342
@tailwind base;
343+
343344
.foo { @apply mt-4; }
344345
`
345346

@@ -1362,3 +1363,40 @@ test('declarations within a rule that uses @apply with !important remain not !im
13621363
expect(result.warnings().length).toBe(0)
13631364
})
13641365
})
1366+
1367+
test('lookup tree is correctly cached based on used tailwind atrules', async () => {
1368+
const input1 = `
1369+
@tailwind utilities;
1370+
1371+
.foo { @apply mt-4; }
1372+
`
1373+
1374+
const input2 = `
1375+
@tailwind components;
1376+
1377+
.foo { @apply mt-4; }
1378+
`
1379+
1380+
let config = {
1381+
corePlugins: [],
1382+
plugins: [
1383+
function ({ addUtilities, addComponents }) {
1384+
addUtilities({ '.mt-4': { marginTop: '1rem' } }, [])
1385+
addComponents({ '.container': { maxWidth: '500px' } }, [])
1386+
},
1387+
],
1388+
}
1389+
1390+
let output1 = await run(input1, config)
1391+
let output2 = await run(input2, config)
1392+
1393+
expect(output1.css).toMatchCss(`
1394+
.mt-4 { margin-top: 1rem; }
1395+
.foo { margin-top: 1rem; }
1396+
`)
1397+
1398+
expect(output2.css).toMatchCss(`
1399+
.container { max-width: 500px; }
1400+
.foo { margin-top: 1rem; }
1401+
`)
1402+
})

‎src/lib/substituteClassApplyAtRules.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ function processApplyAtRules(css, lookupTree, config) {
308308
return css
309309
}
310310

311-
let defaultTailwindTree = null
311+
let defaultTailwindTree = new Map()
312312

313313
export default function substituteClassApplyAtRules(config, getProcessedPlugins, configChanged) {
314314
return function (css) {
@@ -330,14 +330,16 @@ export default function substituteClassApplyAtRules(config, getProcessedPlugins,
330330
return processApplyAtRules(css, postcss.root(), config)
331331
}
332332

333+
let lookupKey = requiredTailwindAtRules.join(',')
334+
333335
// We mutated the `requiredTailwindAtRules`, but when we hit this point in
334336
// time, it means that we don't have all the atrules. The missing atrules
335337
// are listed inside the requiredTailwindAtRules, which we can use to fill
336338
// in the missing pieces.
337339
//
338340
// Important for <style> blocks in Vue components.
339341
const generateLookupTree =
340-
configChanged || defaultTailwindTree === null
342+
configChanged || !defaultTailwindTree.has(lookupKey)
341343
? () => {
342344
return postcss([
343345
substituteTailwindAtRules(config, getProcessedPlugins()),
@@ -351,11 +353,11 @@ export default function substituteClassApplyAtRules(config, getProcessedPlugins,
351353
from: undefined,
352354
})
353355
.then((result) => {
354-
defaultTailwindTree = result
355-
return defaultTailwindTree
356+
defaultTailwindTree.set(lookupKey, result)
357+
return result
356358
})
357359
}
358-
: () => Promise.resolve(defaultTailwindTree)
360+
: () => Promise.resolve(defaultTailwindTree.get(lookupKey))
359361

360362
return generateLookupTree().then((result) => {
361363
return processApplyAtRules(css, result.root, config)

0 commit comments

Comments
 (0)
Please sign in to comment.