11export const shrink_jsx = ( content : string ) : string => {
2- // Split by newline to process line by line, handling both LF and CRLF
32 const lines = content . split ( / \r ? \n / )
43 const result : string [ ] = [ ]
54 let is_in_block_comment = false
65 let skip_body_depth = 0
7- // Buffer to track context for stripping bodies (e.g. "class Foo ")
86 let last_code_buffer = ''
97
108 for ( const line of lines ) {
11- // Capture indentation for use in body stripping
12- const indent_match = line . match ( / ^ \s * / )
13- const current_indent = indent_match ? indent_match [ 0 ] : ''
14-
159 let processed_line = ''
1610 let i = 0
1711 let is_in_string : false | '"' | "'" | '`' = false
@@ -61,7 +55,7 @@ export const shrink_jsx = (content: string): string => {
6155 }
6256
6357 if ( char == '/' && next_char == '/' ) {
64- break // Ignore the rest of the line
58+ break
6559 }
6660
6761 if ( char == '/' && next_char == '*' ) {
@@ -70,34 +64,28 @@ export const shrink_jsx = (content: string): string => {
7064 continue
7165 }
7266
73- // Handle Function Body Stripping
7467 if ( char == '{' ) {
7568 if ( skip_body_depth > 0 ) {
76- // Already skipping, just deepen the level
7769 skip_body_depth ++
7870 last_code_buffer += char
7971 i ++
8072 continue
8173 } else {
82- // Check if we should preserve this block (class, interface, etc.)
83- // We look at last_code_buffer for keywords
8474 const is_keeper =
85- / ( ^ | \s ) ( c l a s s | i n t e r f a c e | e n u m | n a m e s p a c e | t y p e | i m p o r t | e x p o r t ) ( \s | $ ) / . test (
75+ / ( ^ | \s ) ( c l a s s | i n t e r f a c e | e n u m | n a m e s p a c e | t y p e ) ( \s | $ ) / . test (
8676 last_code_buffer
87- )
88- // Also preserve object literals (preceded by =, :, or open paren for destructuring)
77+ ) || / ( ^ | \s ) ( i m p o r t | e x p o r t | d e f a u l t ) \s * $ / . test ( last_code_buffer )
8978 const is_object_literal = / ( = | : | \( ) \s * $ / . test (
9079 last_code_buffer . trimEnd ( )
9180 )
9281
9382 if ( ! is_keeper && ! is_object_literal ) {
9483 skip_body_depth = 1
95- processed_line += `{\n ${ current_indent } // ...\n ${ current_indent } }`
84+ processed_line += `{}`
9685 last_code_buffer += '{}'
9786 i ++
9887 continue
9988 } else {
100- // Keeper: reset buffer to avoid false positives in nested scopes
10189 last_code_buffer = ''
10290 }
10391 }
@@ -117,16 +105,12 @@ export const shrink_jsx = (content: string): string => {
117105 i ++
118106 }
119107
120- // We trim the line to actually "shrink" the file.
121- // If a line contained only comments, it becomes empty and is skipped.
122108 const trimmed = processed_line . trim ( )
123109 if ( trimmed ) {
124- result . push ( processed_line . trimEnd ( ) )
110+ result . push ( processed_line . trimEnd ( ) . replace ( / ; + $ / , '' ) )
125111 }
126112
127- // Add a space to buffer for newline to avoid concatenation issues (e.g. "class Foo" + "{")
128113 last_code_buffer += ' '
129- // Limit buffer size to avoid memory issues, keeping enough for context
130114 if ( last_code_buffer . length > 200 ) {
131115 last_code_buffer = last_code_buffer . slice ( - 200 )
132116 }
0 commit comments