From de096f7cae63d2c8fd473603c90c798b8275e76b Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Fri, 22 Nov 2024 10:39:51 -0500 Subject: [PATCH] Don't print minified code when the build fails (#15106) Fix #15085 We were relying on Node's printing of errors during build but this prints the line of code the error occurs on. Since the code is minified you'd get a really long string of code that wrapped a ton in the console before the actual error. Now we print errors during build like we do in watch mode. --- CHANGELOG.md | 1 + .../@tailwindcss-cli/src/commands/build/index.ts | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6010f06b..0f1e86276 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Use configured `--letter-spacing` values for custom font size utilities ([#15099](https://github.com/tailwindlabs/tailwindcss/pull/15099)) - Ensure `space-x/y-*` and `divide-x/y-*` with variants can undo `space-x/y-reverse` and `divide-x/y-reverse` ([#15094](https://github.com/tailwindlabs/tailwindcss/pull/15094)) +- Don't print minified code when the build fails in the CLI ([#15106](https://github.com/tailwindlabs/tailwindcss/pull/15106)) - _Upgrade (experimental)_: Always add `layer(…)` as the first param to `@import` ([#15102](https://github.com/tailwindlabs/tailwindcss/pull/15102)) ### Changed diff --git a/packages/@tailwindcss-cli/src/commands/build/index.ts b/packages/@tailwindcss-cli/src/commands/build/index.ts index d36a3ab7c..5c90d8ba3 100644 --- a/packages/@tailwindcss-cli/src/commands/build/index.ts +++ b/packages/@tailwindcss-cli/src/commands/build/index.ts @@ -54,6 +54,17 @@ export function options() { } satisfies Arg } +async function handleError(fn: () => T): Promise { + try { + return await fn() + } catch (err) { + if (err instanceof Error) { + eprintln(err.toString()) + } + process.exit(1) + } +} + export async function handle(args: Result>) { let base = path.resolve(args['--cwd']) @@ -159,7 +170,7 @@ export async function handle(args: Result>) { return [compiler, scanner] as const } - let [compiler, scanner] = await createCompiler(input) + let [compiler, scanner] = await handleError(() => createCompiler(input)) // Watch for changes if (args['--watch']) { @@ -288,7 +299,7 @@ export async function handle(args: Result>) { let candidates = scanner.scan() env.DEBUG && console.timeEnd('[@tailwindcss/cli] Scan for candidates') env.DEBUG && console.time('[@tailwindcss/cli] Build CSS') - let output = compiler.build(candidates) + let output = await handleError(() => compiler.build(candidates)) env.DEBUG && console.timeEnd('[@tailwindcss/cli] Build CSS') await write(output, args)