|
1 | 1 | import remapping from '@jridgewell/remapping' |
2 | 2 | import { Features, transform } from 'lightningcss' |
3 | 3 | import MagicString from 'magic-string' |
| 4 | +import pc from 'picocolors' |
4 | 5 |
|
5 | 6 | export interface OptimizeOptions { |
6 | 7 | /** |
@@ -60,6 +61,49 @@ export function optimize( |
60 | 61 | let result = optimize(Buffer.from(input), map) |
61 | 62 | map = result.map?.toString() |
62 | 63 |
|
| 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 | + |
63 | 107 | result = optimize(result.code, map) |
64 | 108 | map = result.map?.toString() |
65 | 109 |
|
|
0 commit comments