Skip to content

Commit 01d1b9a

Browse files
authored
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 b57f4d7 commit 01d1b9a

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121
- Don't prefix arbitrary classes in `group` and `peer` variants ([#11454](https://github.com/tailwindlabs/tailwindcss/pull/11454))
2222
- Sort classes using position of first matching rule ([#11504](https://github.com/tailwindlabs/tailwindcss/pull/11504))
2323
- Bump `jiti`, `lightningcss`, `fast-glob` and `browserlist` dependencies and reflect `lightningcss` related improvements in tests ([#11550](https://github.com/tailwindlabs/tailwindcss/pull/11550))
24+
- Make PostCSS plugin async to improve performance ([#11548](https://github.com/tailwindlabs/tailwindcss/pull/11548))
2425

2526
### Added
2627

src/cli/build/plugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,9 @@ export async function createProcessor(args, cliConfigPath) {
294294
let tailwindPlugin = () => {
295295
return {
296296
postcssPlugin: 'tailwindcss',
297-
Once(root, { result }) {
297+
async Once(root, { result }) {
298298
env.DEBUG && console.time('Compiling CSS')
299-
tailwind(({ createContext }) => {
299+
await tailwind(({ createContext }) => {
300300
console.error()
301301
console.error('Rebuilding...')
302302

src/lib/expandTailwindAtRules.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function buildStylesheet(rules, context) {
103103
}
104104

105105
export default function expandTailwindAtRules(context) {
106-
return (root) => {
106+
return async (root) => {
107107
let layerNodes = {
108108
base: null,
109109
components: null,
@@ -160,18 +160,22 @@ export default function expandTailwindAtRules(context) {
160160
}
161161

162162
if (regexParserContent.length > 0) {
163-
for (let [{ file, content }, { transformer, extractor }] of regexParserContent) {
164-
content = file ? fs.readFileSync(file, 'utf8') : content
165-
getClassCandidates(transformer(content), extractor, candidates, seen)
166-
}
163+
await Promise.all(
164+
regexParserContent.map(async ([{ file, content }, { transformer, extractor }]) => {
165+
content = file ? await fs.promises.readFile(file, 'utf8') : content
166+
getClassCandidates(transformer(content), extractor, candidates, seen)
167+
})
168+
)
167169
}
168170
} else {
169-
for (let { file, content, extension } of context.changedContent) {
170-
let transformer = getTransformer(context.tailwindConfig, extension)
171-
let extractor = getExtractor(context, extension)
172-
content = file ? fs.readFileSync(file, 'utf8') : content
173-
getClassCandidates(transformer(content), extractor, candidates, seen)
174-
}
171+
await Promise.all(
172+
context.changedContent.map(async ({ file, content, extension }) => {
173+
let transformer = getTransformer(context.tailwindConfig, extension)
174+
let extractor = getExtractor(context, extension)
175+
content = file ? await fs.promises.readFile(file, 'utf8') : content
176+
getClassCandidates(transformer(content), extractor, candidates, seen)
177+
})
178+
)
175179
}
176180

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

src/plugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = function tailwindcss(configOrPath) {
2323
return root
2424
},
2525
...handleImportAtRules(),
26-
function (root, result) {
26+
async function (root, result) {
2727
// Use the path for the `@config` directive if it exists, otherwise use the
2828
// path for the file being processed
2929
configOrPath = findAtConfigPath(root, result) ?? configOrPath
@@ -42,7 +42,7 @@ module.exports = function tailwindcss(configOrPath) {
4242
return
4343
}
4444

45-
processTailwindFeatures(context)(root, result)
45+
await processTailwindFeatures(context)(root, result)
4646
},
4747
function lightningCssPlugin(_root, result) {
4848
let map = result.map ?? result.opts.map

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)