Skip to content

Commit d0a1bd6

Browse files
authored
Show Lightning CSS warnings when optimizing/minifying in production (#18918)
This PR improves the DX by showing all the Lightning CSS warnings when using a "production" build (or using `--optimize` or `--minify` flags when using the CLI). Right now Tailwind CSS itself doesn't care about the exact syntax you are using in the CSS as long as it looks valid. We do this because otherwise we would have to parse a lot more CSS syntax and validate it even though it would be valid CSS in 99.99% of the cases. Even worse, if you want to use newer CSS syntax that Tailwind CSS doesn't validate yet, then you would get warnings for valid CSS. Another reason why we don't do this is because the browser already does a great job at ignoring invalid CSS. So the linked issue #15872 would still silently fail in development mode. In this case, everything would work, except the shadow with the invalid syntax. But in production mode, you would now get a proper warning from Lightning CSS, because they try to optimize the CSS and remove invalid CSS. One potential issue here is that we run Lightning CSS on the generated CSS, not on the input CSS. So the current output shows the warnings in the output CSS not the input CSS. Any thoughts if we would just skip the line numbers? ## Test plan 1. Everything works as before 2. In production mode, you would get warnings printed to the terminal. This is done in `@tailwindcss/node` so the CLI/Vite/PostCSS plugins would all get the same behavior. Screenshots: If you have a single issue: <img width="977" height="441" alt="image" src="https://github.com/user-attachments/assets/7b061ee9-b74f-4b40-aa05-cff67a21dfcc" /> If you have multiple issues: <img width="2170" height="711" alt="image" src="https://github.com/user-attachments/assets/a5bc9b0a-964b-465f-80f3-d30dd467e69c" /> Fixes: #15872
1 parent 65bad11 commit d0a1bd6

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
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
- Do not generate `grid-row` utilities when configuring `grid-row-start` or `grid-row-end` ([#18907](https://github.com/tailwindlabs/tailwindcss/pull/18907))
2222
- Prevent duplicate CSS when overwriting a static utility with a theme key ([#18056](https://github.com/tailwindlabs/tailwindcss/pull/18056))
2323
- Do not migrate `variant = 'outline'` during upgrades ([#18922](https://github.com/tailwindlabs/tailwindcss/pull/18922))
24+
- Show Lightning CSS warnings (if any) when optimizing/minifying ([#18918](https://github.com/tailwindlabs/tailwindcss/pull/18918))
2425

2526
## [4.1.13] - 2025-09-03
2627

packages/@tailwindcss-node/src/optimize.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,48 @@ export function optimize(
6060
let result = optimize(Buffer.from(input), map)
6161
map = result.map?.toString()
6262

63+
// Because of `errorRecovery: true`, there could be warnings, so let's let the
64+
// user know about them.
65+
if (process.env.NODE_ENV !== 'test' && result.warnings.length > 0) {
66+
let lines = input.split('\n')
67+
68+
let output = [
69+
`Found ${result.warnings.length} ${result.warnings.length === 1 ? 'warning' : 'warnings'} while optimizing generated CSS:`,
70+
]
71+
72+
for (let [idx, warning] of result.warnings.entries()) {
73+
output.push('')
74+
if (result.warnings.length > 1) {
75+
output.push(`Issue #${idx + 1}:`)
76+
}
77+
78+
let context = 2
79+
80+
let start = Math.max(0, warning.loc.line - context - 1)
81+
let end = Math.min(lines.length, warning.loc.line + context)
82+
83+
let snippet = lines.slice(start, end).map((line, idx) => {
84+
if (start + idx + 1 === warning.loc.line) {
85+
return `${dim(`\u2502`)} ${line}`
86+
} else {
87+
return dim(`\u2502 ${line}`)
88+
}
89+
})
90+
91+
snippet.splice(
92+
warning.loc.line - start,
93+
0,
94+
`${dim('\u2506')}${' '.repeat(warning.loc.column - 1)} ${yellow(`${dim('^--')} ${warning.message}`)}`,
95+
`${dim('\u2506')}`,
96+
)
97+
98+
output.push(...snippet)
99+
}
100+
output.push('')
101+
102+
console.warn(output.join('\n'))
103+
}
104+
63105
result = optimize(result.code, map)
64106
map = result.map?.toString()
65107

@@ -88,3 +130,11 @@ export function optimize(
88130
map,
89131
}
90132
}
133+
134+
function dim(str: string) {
135+
return `\x1B[2m${str}\x1B[22m`
136+
}
137+
138+
function yellow(str: string) {
139+
return `\x1B[33m${str}\x1B[39m`
140+
}

packages/@tailwindcss-node/tsup.config.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,35 @@ export default defineConfig([
66
minify: true,
77
dts: true,
88
entry: ['src/index.cts'],
9+
define: {
10+
'process.env.NODE_ENV': '"production"',
11+
},
912
},
1013
{
1114
format: ['esm'],
1215
minify: true,
1316
dts: true,
1417
entry: ['src/index.ts'],
18+
define: {
19+
'process.env.NODE_ENV': '"production"',
20+
},
1521
},
1622
{
1723
format: ['esm'],
1824
minify: true,
1925
dts: true,
2026
entry: ['src/esm-cache.loader.mts'],
27+
define: {
28+
'process.env.NODE_ENV': '"production"',
29+
},
2130
},
2231
{
2332
format: ['cjs'],
2433
minify: true,
2534
dts: true,
2635
entry: ['src/require-cache.cts'],
36+
define: {
37+
'process.env.NODE_ENV': '"production"',
38+
},
2739
},
2840
])

0 commit comments

Comments
 (0)