Skip to content

Commit 8304d71

Browse files
committed
render Lightning CSS warnings
1 parent 65bad11 commit 8304d71

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

packages/@tailwindcss-node/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"jiti": "^2.5.1",
4343
"lightningcss": "catalog:",
4444
"magic-string": "^0.30.18",
45+
"picocolors": "^1.1.1",
4546
"source-map-js": "^1.2.1",
4647
"tailwindcss": "workspace:*"
4748
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import remapping from '@jridgewell/remapping'
22
import { Features, transform } from 'lightningcss'
33
import MagicString from 'magic-string'
4+
import pc from 'picocolors'
45

56
export interface OptimizeOptions {
67
/**
@@ -60,6 +61,49 @@ export function optimize(
6061
let result = optimize(Buffer.from(input), map)
6162
map = result.map?.toString()
6263

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

pnpm-lock.yaml

Lines changed: 3 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)