Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve @tailwindcss/postcss performance for initial builds #14565

Merged
merged 19 commits into from
Oct 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions packages/@tailwindcss-postcss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import fixRelativePathsPlugin from './postcss-fix-relative-paths'
interface CacheEntry {
mtimes: Map<string, number>
compiler: null | Awaited<ReturnType<typeof compile>>
scanner: null | Scanner
css: string
optimizedCss: string
fullRebuildPaths: string[]
Expand All @@ -23,7 +24,8 @@ function getContextFromCache(inputFile: string, opts: PluginOptions): CacheEntry
if (cache.has(key)) return cache.get(key)!
let entry = {
mtimes: new Map<string, number>(),
compiler: null as null | Awaited<ReturnType<typeof compile>>,
compiler: null,
scanner: null,
css: '',
optimizedCss: '',
fullRebuildPaths: [] as string[],
Expand Down Expand Up @@ -123,18 +125,20 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {

let css = ''

// Look for candidates used to generate the CSS
let scanner = new Scanner({
detectSources: { base },
sources: context.compiler.globs,
})
if (context.scanner === null) {
// Look for candidates used to generate the CSS
context.scanner = new Scanner({
detectSources: { base },
sources: context.compiler.globs,
})
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to recreate the scanner when doing a full rebuild too, right? Since the globs might've changed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically after line 166 we also want to re-create the scanner


env.DEBUG && console.time('[@tailwindcss/postcss] Scan for candidates')
let candidates = scanner.scan()
let candidates = context.scanner.scan()
env.DEBUG && console.timeEnd('[@tailwindcss/postcss] Scan for candidates')

// Add all found files as direct dependencies
for (let file of scanner.files) {
for (let file of context.scanner.files) {
result.messages.push({
type: 'dependency',
plugin: '@tailwindcss/postcss',
Expand All @@ -146,7 +150,7 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
// Register dependencies so changes in `base` cause a rebuild while
// giving tools like Vite or Parcel a glob that can be used to limit
// the files that cause a rebuild to only those that match it.
for (let { base, pattern } of scanner.globs) {
for (let { base, pattern } of context.scanner.globs) {
result.messages.push({
type: 'dir-dependency',
plugin: '@tailwindcss/postcss',
Expand Down