Skip to content

Commit 5b9cbb3

Browse files
RobinMalfaitthecrypticace
authored andcommitted
Make PostCSS plugin async to improve performance (#11548)
* make main plugin async This way we can improve the `fs.readFileSync` to a bunch of `fs.promises.readFile` in a `Promise.all` instead. * make CLI plugin async * update CHANGELOG
1 parent 1c9bb38 commit 5b9cbb3

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Escape animation names when prefixes contain special characters ([#11470](https://github.com/tailwindlabs/tailwindcss/pull/11470))
1818
- Sort classes using position of first matching rule ([#11504](https://github.com/tailwindlabs/tailwindcss/pull/11504))
1919
- Allow variant to be an at-rule without a prelude ([#11589](https://github.com/tailwindlabs/tailwindcss/pull/11589))
20+
- Make PostCSS plugin async to improve performance ([#11548](https://github.com/tailwindlabs/tailwindcss/pull/11548))
2021

2122
### Added
2223

src/cli/build/plugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@ export async function createProcessor(args, cliConfigPath) {
278278
let tailwindPlugin = () => {
279279
return {
280280
postcssPlugin: 'tailwindcss',
281-
Once(root, { result }) {
281+
async Once(root, { result }) {
282282
env.DEBUG && console.time('Compiling CSS')
283-
tailwind(({ createContext }) => {
283+
await tailwind(({ createContext }) => {
284284
console.error()
285285
console.error('Rebuilding...')
286286

src/lib/expandTailwindAtRules.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function buildStylesheet(rules, context) {
9898
}
9999

100100
export default function expandTailwindAtRules(context) {
101-
return (root) => {
101+
return async (root) => {
102102
let layerNodes = {
103103
base: null,
104104
components: null,
@@ -145,12 +145,14 @@ export default function expandTailwindAtRules(context) {
145145
// getClassCandidatesOxide(file, transformer(content), extractor, candidates, seen)
146146
// }
147147
} else {
148-
for (let { file, content, extension } of context.changedContent) {
149-
let transformer = getTransformer(context.tailwindConfig, extension)
150-
let extractor = getExtractor(context, extension)
151-
content = file ? fs.readFileSync(file, 'utf8') : content
152-
getClassCandidates(transformer(content), extractor, candidates, seen)
153-
}
148+
await Promise.all(
149+
context.changedContent.map(async ({ file, content, extension }) => {
150+
let transformer = getTransformer(context.tailwindConfig, extension)
151+
let extractor = getExtractor(context, extension)
152+
content = file ? await fs.promises.readFile(file, 'utf8') : content
153+
getClassCandidates(transformer(content), extractor, candidates, seen)
154+
})
155+
)
154156
}
155157

156158
env.DEBUG && console.timeEnd('Reading changed files')

src/plugin.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function tailwindcss(configOrPath) {
1313
console.time('JIT TOTAL')
1414
return root
1515
},
16-
function (root, result) {
16+
async function (root, result) {
1717
// Use the path for the `@config` directive if it exists, otherwise use the
1818
// path for the file being processed
1919
configOrPath = findAtConfigPath(root, result) ?? configOrPath
@@ -25,14 +25,14 @@ module.exports = function tailwindcss(configOrPath) {
2525

2626
for (const root of roots) {
2727
if (root.type === 'root') {
28-
processTailwindFeatures(context)(root, result)
28+
await processTailwindFeatures(context)(root, result)
2929
}
3030
}
3131

3232
return
3333
}
3434

35-
processTailwindFeatures(context)(root, result)
35+
await processTailwindFeatures(context)(root, result)
3636
},
3737
__OXIDE__ &&
3838
function lightningCssPlugin(_root, result) {

src/processTailwindFeatures.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { createContext } from './lib/setupContextUtils'
1212
import { issueFlagNotices } from './featureFlags'
1313

1414
export default function processTailwindFeatures(setupContext) {
15-
return function (root, result) {
15+
return async function (root, result) {
1616
let { tailwindDirectives, applyDirectives } = normalizeTailwindDirectives(root)
1717

1818
detectNesting()(root, result)
@@ -44,7 +44,8 @@ export default function processTailwindFeatures(setupContext) {
4444

4545
issueFlagNotices(context.tailwindConfig)
4646

47-
expandTailwindAtRules(context)(root, result)
47+
await expandTailwindAtRules(context)(root, result)
48+
4849
// Partition apply rules that are generated by
4950
// addComponents, addUtilities and so on.
5051
partitionApplyAtRules()(root, result)

0 commit comments

Comments
 (0)