Skip to content
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
Use an LRU for the shared postcss cache
  • Loading branch information
philipp-spiess committed Oct 2, 2024
commit 687ef2a737f29f4f0f8df844cf557e506ed3d7fd
7 changes: 4 additions & 3 deletions packages/@tailwindcss-postcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@
"@tailwindcss/node": "workspace:^",
"@tailwindcss/oxide": "workspace:^",
"lightningcss": "catalog:",
"quick-lru": "^7.0.0",
"tailwindcss": "workspace:^"
},
"devDependencies": {
"@types/node": "catalog:",
"postcss": "^8.4.41",
"postcss-import": "^16.1.0",
"@types/postcss-import": "14.0.3",
"internal-example-plugin": "workspace:*"
"internal-example-plugin": "workspace:*",
"postcss": "^8.4.41",
"postcss-import": "^16.1.0"
}
}
55 changes: 24 additions & 31 deletions packages/@tailwindcss-postcss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,30 @@ import fs from 'fs'
import { Features, transform } from 'lightningcss'
import path from 'path'
import postcss, { type AcceptedPlugin, type PluginCreator } from 'postcss'
import QuickLRU from 'quick-lru'
import fixRelativePathsPlugin from './postcss-fix-relative-paths'

/**
* A Map that can generate default values for keys that don't exist.
* Generated default values are added to the map to avoid recomputation.
*/
class DefaultMap<T = string, V = any> extends Map<T, V> {
constructor(private factory: (key: T, self: DefaultMap<T, V>) => V) {
super()
}

get(key: T): V {
let value = super.get(key)

if (value === undefined) {
value = this.factory(key, this)
this.set(key, value)
}
interface CacheEntry {
mtimes: Map<string, number>
compiler: null | Awaited<ReturnType<typeof compile>>
css: string
optimizedCss: string
fullRebuildPaths: string[]
}
let cache = new QuickLRU<string, CacheEntry>({ maxSize: 1000 })

return value
function getContextFromCache(inputFile: string, opts: PluginOptions): CacheEntry {
let key = `${inputFile}:${opts.base ?? ''}:${opts.optimize ?? ''}`
if (cache.has(key)) return cache.get(key)!
let entry = {
mtimes: new Map<string, number>(),
compiler: null as null | Awaited<ReturnType<typeof compile>>,
css: '',
optimizedCss: '',
fullRebuildPaths: [] as string[],
}
cache.set(key, entry)
return entry
}

export type PluginOptions = {
Expand All @@ -36,34 +39,24 @@ export type PluginOptions = {
optimize?: boolean | { minify?: boolean }
}

let cache = new DefaultMap(() => {
return {
mtimes: new Map<string, number>(),
compiler: null as null | Awaited<ReturnType<typeof compile>>,
css: '',
optimizedCss: '',
fullRebuildPaths: [] as string[],
}
})

function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
let base = opts.base ?? process.cwd()
let optimize = opts.optimize ?? process.env.NODE_ENV === 'production'

return {
postcssPlugin: '@tailwindcss/postcss',
plugins: [
// We need to handle the case where `postcss-import` might have run before the Tailwind CSS
// plugin is run. In this case, we need to manually fix relative paths before processing it
// in core.
// We need to handle the case where `postcss-import` might have run before
// the Tailwind CSS plugin is run. In this case, we need to manually fix
// relative paths before processing it in core.
fixRelativePathsPlugin(),

{
postcssPlugin: 'tailwindcss',
async OnceExit(root, { result }) {
env.DEBUG && console.time('[@tailwindcss/postcss] Total time in @tailwindcss/postcss')
let inputFile = result.opts.from ?? ''
let context = cache.get(inputFile)
let context = getContextFromCache(inputFile, opts)
let inputBasePath = path.dirname(path.resolve(inputFile))

async function createCompiler() {
Expand Down