|
| 1 | +export const compact_css = (content: string): string => { |
| 2 | + // Split by newline to process line by line, handling both LF and CRLF |
| 3 | + const lines = content.split(/\r?\n/) |
| 4 | + const result: string[] = [] |
| 5 | + let is_in_block_comment = false |
| 6 | + let skip_body_depth = 0 |
| 7 | + // Buffer to track context for stripping bodies (e.g. "@media ... ") |
| 8 | + let last_code_buffer = '' |
| 9 | + |
| 10 | + for (const line of lines) { |
| 11 | + let processed_line = '' |
| 12 | + let i = 0 |
| 13 | + let is_in_string: false | '"' | "'" = false |
| 14 | + |
| 15 | + while (i < line.length) { |
| 16 | + const char = line[i] |
| 17 | + const next_char = line[i + 1] |
| 18 | + |
| 19 | + // Handle Block Comments |
| 20 | + if (is_in_block_comment) { |
| 21 | + if (char == '*' && next_char == '/') { |
| 22 | + is_in_block_comment = false |
| 23 | + i += 2 |
| 24 | + } else { |
| 25 | + i++ |
| 26 | + } |
| 27 | + continue |
| 28 | + } |
| 29 | + |
| 30 | + // Handle Strings (ignore comments inside strings) |
| 31 | + if (is_in_string) { |
| 32 | + if (char == '\\') { |
| 33 | + // Handle escaped characters inside string |
| 34 | + if (skip_body_depth === 0) { |
| 35 | + processed_line += char + (next_char || '') |
| 36 | + } |
| 37 | + last_code_buffer += char + (next_char || '') |
| 38 | + i += 2 |
| 39 | + continue |
| 40 | + } |
| 41 | + if (char === is_in_string) { |
| 42 | + is_in_string = false |
| 43 | + } |
| 44 | + if (skip_body_depth === 0) { |
| 45 | + processed_line += char |
| 46 | + } |
| 47 | + last_code_buffer += char |
| 48 | + i++ |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + // Start of String |
| 53 | + if (char == '"' || char === "'") { |
| 54 | + is_in_string = char |
| 55 | + if (skip_body_depth === 0) { |
| 56 | + processed_line += char |
| 57 | + } |
| 58 | + last_code_buffer += char |
| 59 | + i++ |
| 60 | + continue |
| 61 | + } |
| 62 | + |
| 63 | + // Start of Line Comment (Support // for SCSS/LESS, though not standard CSS) |
| 64 | + if (char == '/' && next_char == '/') { |
| 65 | + break // Ignore the rest of the line |
| 66 | + } |
| 67 | + |
| 68 | + // Start of Block Comment |
| 69 | + if (char == '/' && next_char == '*') { |
| 70 | + is_in_block_comment = true |
| 71 | + i += 2 |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + // Handle Body Stripping |
| 76 | + if (char == '{') { |
| 77 | + if (skip_body_depth > 0) { |
| 78 | + // Already skipping, just deepen the level |
| 79 | + skip_body_depth++ |
| 80 | + last_code_buffer += char |
| 81 | + i++ |
| 82 | + continue |
| 83 | + } else { |
| 84 | + // Check if we should preserve this block (at-rules like @media) |
| 85 | + const is_keeper = |
| 86 | + /(@media|@supports|@keyframes|@layer|@container)\b/.test( |
| 87 | + last_code_buffer |
| 88 | + ) |
| 89 | + |
| 90 | + if (!is_keeper) { |
| 91 | + // Start skipping |
| 92 | + skip_body_depth = 1 |
| 93 | + processed_line += '{}' |
| 94 | + last_code_buffer += '{}' |
| 95 | + i++ |
| 96 | + continue |
| 97 | + } else { |
| 98 | + // Keeper: reset buffer |
| 99 | + last_code_buffer = '' |
| 100 | + } |
| 101 | + } |
| 102 | + } else if (char == '}') { |
| 103 | + if (skip_body_depth > 0) { |
| 104 | + skip_body_depth-- |
| 105 | + last_code_buffer += char |
| 106 | + i++ |
| 107 | + continue |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Regular character |
| 112 | + if (skip_body_depth == 0) { |
| 113 | + processed_line += char |
| 114 | + } |
| 115 | + last_code_buffer += char |
| 116 | + i++ |
| 117 | + } |
| 118 | + |
| 119 | + // We trim the line to actually "compact" the file. |
| 120 | + // If a line contained only comments, it becomes empty and is skipped. |
| 121 | + const trimmed = processed_line.trim() |
| 122 | + if (trimmed) { |
| 123 | + result.push(trimmed) |
| 124 | + } |
| 125 | + |
| 126 | + // Add a space to buffer for newline |
| 127 | + last_code_buffer += ' ' |
| 128 | + // Limit buffer size |
| 129 | + if (last_code_buffer.length > 200) { |
| 130 | + last_code_buffer = last_code_buffer.slice(-200) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return result.length > 0 ? result.join('\n') + '\n' : '' |
| 135 | +} |
0 commit comments