Skip to content

Commit

Permalink
add incremental rebuilds to @tailwindcss/cli
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Mar 11, 2024
1 parent 1c057fd commit 2707322
Showing 1 changed file with 44 additions and 52 deletions.
96 changes: 44 additions & 52 deletions packages/@tailwindcss-cli/src/commands/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import watcher from '@parcel/watcher'
import {
IO,
Parsing,
clearCache,
scanDir,
scanFiles,
type ChangedContent,
} from '@tailwindcss/oxide'
import { IO, Parsing, scanDir, scanFiles, type ChangedContent } from '@tailwindcss/oxide'
import { existsSync } from 'node:fs'
import fs from 'node:fs/promises'
import path from 'node:path'
Expand Down Expand Up @@ -99,25 +92,42 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
args['--input'] ?? base,
)

// Compile the input
let { build } = compile(input)
let result = build(candidates)

// Optimize the output
if (args['--minify'] || args['--optimize']) {
result = optimizeCss(result, {
file: args['--input'] ?? 'input.css',
minify: args['--minify'] ?? false,
})
let previous = {
css: '',
optimizedCss: '',
}

// Write the output
if (args['--output']) {
await outputFile(args['--output'], result)
} else {
println(result)
async function write(css: string, args: Result<ReturnType<typeof options>>) {
let output = css

// Optimize the output
if (args['--minify'] || args['--optimize']) {
if (css !== previous.css) {
let optimizedCss = optimizeCss(css, {
file: args['--input'] ?? 'input.css',
minify: args['--minify'] ?? false,
})
previous.css = css
previous.optimizedCss = optimizedCss
output = optimizedCss
} else {
output = previous.optimizedCss
}
}

// Write the output
if (args['--output']) {
await outputFile(args['--output'], output)
} else {
println(output)
}
}

// Compile the input
let { build } = compile(input)

await write(build(candidates), args)

let end = process.hrtime.bigint()
eprintln(header())
eprintln()
Expand Down Expand Up @@ -162,26 +172,14 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
// Re-compile the input
let start = process.hrtime.bigint()

// Track the compiled CSS
let compiledCss = ''

// Scan the entire `base` directory for full rebuilds.
if (rebuildStrategy === 'full') {
// Clear the cache because we need to re-scan the entire directory.
clearCache()

// Re-scan the directory to get the new `candidates`.
candidates = scanDir({ base }).candidates
}

// Scan changed files only for incremental rebuilds.
else if (rebuildStrategy === 'incremental') {
let uniqueCandidates = new Set(candidates)
for (let candidate of scanFiles(changedFiles, IO.Sequential | Parsing.Sequential)) {
uniqueCandidates.add(candidate)
}
candidates = Array.from(uniqueCandidates)
}

// Resolve the input
if (rebuildStrategy === 'full') {
// Collect the new `input` and `cssImportPaths`.
;[input, cssImportPaths] = await handleImports(
args['--input']
Expand All @@ -191,25 +189,19 @@ export async function handle(args: Result<ReturnType<typeof options>>) {
`,
args['--input'] ?? base,
)

build = compile(input).build
compiledCss = build(candidates)
}

// Compile the input
result = compile(input).build(candidates)
// Scan changed files only for incremental rebuilds.
else if (rebuildStrategy === 'incremental') {
let newCandidates = scanFiles(changedFiles, IO.Sequential | Parsing.Sequential)

// Optimize the output
if (args['--minify'] || args['--optimize']) {
result = optimizeCss(result, {
file: args['--input'] ?? 'input.css',
minify: args['--minify'] ?? false,
})
compiledCss = build(newCandidates)
}

// Write the output
if (args['--output']) {
await outputFile(args['--output'], result)
} else {
println(result)
}
await write(compiledCss, args)

let end = process.hrtime.bigint()
eprintln(`Done in ${formatDuration(end - start)}`)
Expand Down

0 comments on commit 2707322

Please sign in to comment.