@@ -16,33 +16,75 @@ export function wrapDefaultExport(content: string, withFunction: string): string
1616
1717 let wrappedContent = content ;
1818
19- // Handle ESM exports
20- if ( isESM ) {
21- const esmMatch = content . match ( / e x p o r t \s + d e f a u l t \s + (?: c l a s s | i n t e r f a c e | a b s t r a c t \s + c l a s s ) \s + / ) ;
22- if ( ! esmMatch ) {
23- wrappedContent = wrappedContent . replace (
24- / ( e x p o r t \s + d e f a u l t \s + ) ( [ ^ ; \n ] + (?: { [ ^ } ] * } ) ? [ ^ ; \n ] * ) ( ; ? \s * ) $ / gm,
25- ( _ , prefix , exportValue , semicolon ) => {
26- const trimmedValue = exportValue . trim ( ) ;
27- return `${ prefix } ${ withFunction } (${ trimmedValue } )${ semicolon } ` ;
28- } ,
29- ) ;
19+ const config = isCJS ? EXPORT_CONFIGS . cjs : EXPORT_CONFIGS . esm ;
20+
21+ // Skip if it's a class/interface export
22+ if ( ! content . match ( config . skipPattern ) ) {
23+ const lastExportMatch = wrappedContent . match ( config . matchPattern ) ;
24+ if ( lastExportMatch ) {
25+ const [ fullMatch , prefix , rest ] = lastExportMatch ;
26+ const { exportValue, restContent } = extractExportValue ( rest ) ;
27+ const replacement = createWrappedReplacement ( prefix , exportValue , withFunction , restContent ) ;
28+
29+ // Replace only the last occurrence
30+ const index = wrappedContent . lastIndexOf ( fullMatch ) ;
31+ wrappedContent = wrappedContent . slice ( 0 , index ) + replacement + wrappedContent . slice ( index + fullMatch . length ) ;
3032 }
3133 }
3234
33- // Handle CJS exports
34- if ( isCJS ) {
35- const cjsMatch = content . match ( / m o d u l e \. e x p o r t s \s * = \s * (?: c l a s s | i n t e r f a c e | a b s t r a c t \s + c l a s s ) \s + / ) ;
36- if ( ! cjsMatch ) {
37- wrappedContent = wrappedContent . replace (
38- / ( m o d u l e \. e x p o r t s \s * = \s * ) ( [ ^ ; \n ] + (?: { [ ^ } ] * } ) ? [ ^ ; \n ] * ) ( ; ? \s * ) $ / gm,
39- ( _ , prefix , exportValue , semicolon ) => {
40- const trimmedValue = exportValue . trim ( ) ;
41- return `${ prefix } ${ withFunction } (${ trimmedValue } )${ semicolon } ` ;
42- } ,
43- ) ;
35+ return wrappedContent ;
36+ }
37+
38+ const EXPORT_CONFIGS = {
39+ esm : {
40+ skipPattern : / e x p o r t \s + d e f a u l t \s + (?: c l a s s | i n t e r f a c e | a b s t r a c t \s + c l a s s ) \s + / ,
41+ matchPattern : / ( e x p o r t \s + d e f a u l t \s + ) ( [ \s \S ] * $ ) / m,
42+ } ,
43+ cjs : {
44+ skipPattern : / m o d u l e \. e x p o r t s \s * = \s * (?: c l a s s | i n t e r f a c e | a b s t r a c t \s + c l a s s ) \s + / ,
45+ matchPattern : / ( m o d u l e \. e x p o r t s \s * = \s * ) ( [ \s \S ] * $ ) / m,
46+ } ,
47+ } ;
48+
49+ /**
50+ * Extracts the export value from a string, handling nested structures
51+ */
52+ function extractExportValue ( rest : string ) : { exportValue : string ; restContent : string } {
53+ let depth = 0 ;
54+ let i = 0 ;
55+
56+ // Parse the export value handling nested parentheses and braces
57+ for ( i = 0 ; i < rest . length ; i ++ ) {
58+ const char = rest [ i ] ;
59+ if ( char === "(" || char === "{" ) depth ++ ;
60+ if ( char === ")" || char === "}" ) depth -- ;
61+
62+ // Break on semicolon or newline if we're not inside parentheses/braces
63+ if ( depth === 0 && ( char === ";" || char === "\n" ) ) {
64+ return {
65+ exportValue : rest . slice ( 0 , char === ";" ? i + 1 : i ) ,
66+ restContent : rest . slice ( char === ";" ? i + 1 : i ) ,
67+ } ;
4468 }
4569 }
4670
47- return wrappedContent ;
71+ // If we didn't find a terminator, use the whole rest
72+ return {
73+ exportValue : rest ,
74+ restContent : "" ,
75+ } ;
76+ }
77+
78+ /**
79+ * Creates a wrapped replacement for an export statement
80+ */
81+ function createWrappedReplacement (
82+ prefix : string ,
83+ exportValue : string ,
84+ withFunction : string ,
85+ restContent : string ,
86+ ) : string {
87+ const trimmedValue = exportValue . trim ( ) ;
88+ const hasTrailingSemi = trimmedValue . endsWith ( ";" ) ;
89+ return `${ prefix } ${ withFunction } (${ trimmedValue . replace ( / ; $ / , "" ) } )${ hasTrailingSemi ? ";" : "" } ${ restContent } ` ;
4890}
0 commit comments