@@ -29,7 +29,11 @@ async function loadPrettierBundle(): Promise<PrettierBundle> {
2929 parserBabel,
3030 prettierPluginEstree,
3131 } )
32- ) ;
32+ ) . catch ( ( err ) => {
33+ // Reset cached promise so future attempts can retry
34+ prettierBundlePromise = undefined ;
35+ throw err ;
36+ } ) ;
3337 }
3438
3539 return prettierBundlePromise ;
@@ -120,6 +124,12 @@ export async function formatCode(): Promise<void> {
120124 prettierPluginEstree,
121125 } = await loadPrettierBundle ( ) ;
122126
127+ // Some dynamic imports expose the plugin as default export or as module namespace.
128+ const pHtml = ( parserHtml as any ) . default || parserHtml ;
129+ const pCss = ( parserCss as any ) . default || parserCss ;
130+ const pBabel = ( parserBabel as any ) . default || parserBabel ;
131+ const pEstree = ( prettierPluginEstree as any ) . default || prettierPluginEstree ;
132+
123133 // Format each editor separately with error handling
124134 let formattedHtml = editors . html . view . state . doc . toString ( ) ;
125135 let formattedCss = editors . css . view . state . doc . toString ( ) ;
@@ -128,12 +138,12 @@ export async function formatCode(): Promise<void> {
128138 // Format HTML
129139 try {
130140 if ( formattedHtml . trim ( ) ) {
131- // First normalize the HTML by removing extra whitespace
132- const normalizedHtml = formattedHtml . trim ( ) . replace ( / ^ \s + / gm , '' ) ;
133-
141+ // Trim surrounding whitespace
142+ const normalizedHtml = formattedHtml . trim ( ) ;
143+
134144 formattedHtml = await prettier . format ( normalizedHtml , {
135145 parser : "html" ,
136- plugins : [ parserHtml ] ,
146+ plugins : [ pHtml ] ,
137147 printWidth : 120 ,
138148 tabWidth : 4 ,
139149 htmlWhitespaceSensitivity : "ignore" ,
@@ -144,9 +154,6 @@ export async function formatCode(): Promise<void> {
144154 // Ensure the formatted HTML is well-formed
145155 formattedHtml = formattedHtml . replace ( / > \n \s * \n / g, '>\n' ) ;
146156
147- // Remove two spaces from the beginning of each line
148- formattedHtml = formattedHtml . replace ( / ^ / gm, '' ) ;
149-
150157 }
151158 } catch ( error ) {
152159 console . warn ( "HTML formatting skipped:" , error ) ;
@@ -158,12 +165,11 @@ export async function formatCode(): Promise<void> {
158165 if ( formattedCss . trim ( ) ) {
159166 formattedCss = await prettier . format ( formattedCss , {
160167 parser : "css" ,
161- plugins : [ parserCss ] ,
168+ plugins : [ pCss ] ,
162169 printWidth : 100 ,
163170 tabWidth : 2 ,
164171 } ) ;
165- // Remove two spaces from the beginning of each line
166- formattedCss = formattedCss . replace ( / ^ / gm, '' ) ;
172+
167173 }
168174 } catch ( error ) {
169175 console . warn ( "CSS formatting failed:" , error ) ;
@@ -174,37 +180,29 @@ export async function formatCode(): Promise<void> {
174180 if ( formattedJs . trim ( ) ) {
175181 formattedJs = await prettier . format ( formattedJs , {
176182 parser : "babel" , // Use babel instead of flow
177- plugins : [
178- parserBabel ,
179- ( prettierPluginEstree as any ) . default || prettierPluginEstree ,
180- ] ,
183+ plugins : [ pBabel , pEstree ] ,
181184 printWidth : 100 ,
182185 tabWidth : 2 ,
183186 semi : true ,
184187 singleQuote : true ,
185188 trailingComma : "es5" ,
186189 bracketSpacing : true ,
187190 } ) ;
188- // Remove two spaces from the beginning of each line
189- formattedJs = formattedJs . replace ( / ^ / gm, '' ) ;
191+
190192 }
191193 } catch ( error ) {
192194 console . warn ( "JavaScript formatting failed:" , error ) ;
193195 // Try with a simpler parser as fallback
194196 try {
195197 formattedJs = await prettier . format ( formattedJs , {
196198 parser : "babel-ts" , // Alternative parser
197- plugins : [
198- parserBabel ,
199- ( prettierPluginEstree as any ) . default || prettierPluginEstree ,
200- ] ,
199+ plugins : [ pBabel , pEstree ] ,
201200 printWidth : 100 ,
202201 tabWidth : 2 ,
203202 semi : true ,
204203 singleQuote : true ,
205204 } ) ;
206- // Remove two spaces from the beginning of each line
207- formattedJs = formattedJs . replace ( / ^ / gm, '' ) ;
205+
208206 } catch ( fallbackError ) {
209207 console . warn ( "Fallback JavaScript formatting also failed:" , fallbackError ) ;
210208 }
0 commit comments