@@ -2,30 +2,31 @@ import type { CodeType } from "lowcoder-core";
22import { relaxedJSONToJSON } from "lowcoder-core" ;
33import { getDynamicStringSegments , isDynamicSegment } from "lowcoder-core" ;
44import { format as formatSQL } from "sql-formatter" ;
5+ import estree from "prettier/plugins/estree" ;
56import type { Language } from "./codeEditorTypes" ;
67
78export async function cssFormatter ( text : string ) {
8- const prettier = await require ( "prettier/standalone" ) ;
9- const parserPlugin = await require ( "prettier/parser- postcss" ) ;
9+ const prettier = await import ( "prettier/standalone" ) ;
10+ const parserPlugin = await import ( "prettier/plugins/ postcss" ) ;
1011 return ( await prettier . format ( text , { parser : "css" , plugins : [ parserPlugin ] , semi : false } ) ) . trim ( ) ;
1112}
1213
1314export async function htmlFormatter ( text : string ) {
14- const prettier = await require ( "prettier/standalone" ) ;
15- const parserPlugin = await require ( "prettier/parser- html" ) ;
15+ const prettier = await import ( "prettier/standalone" ) ;
16+ const parserPlugin = await import ( "prettier/plugins/ html" ) ;
1617 return ( await prettier . format ( text , { parser : "html" , plugins : [ parserPlugin ] , semi : false } ) ) . trim ( ) ;
1718}
1819
1920async function getJavascriptFormatter ( ) {
20- const prettier = await require ( "prettier/standalone" ) ;
21- const parserBabel = await require ( "prettier/parser- babel" ) ;
21+ const prettier = await import ( "prettier/standalone" ) ;
22+ const parserBabel = await import ( "prettier/plugins/ babel" ) ;
2223 return async ( text : string ) =>
23- ( await prettier . format ( text , { parser : "babel" , plugins : [ parserBabel ] , semi : false } ) ) . trim ( ) ;
24+ ( await prettier . format ( text , { parser : "babel" , plugins : [ parserBabel , estree ] , semi : false } ) ) . trim ( ) ;
2425}
2526
2627export async function getJsonFormatter ( ) {
27- const prettier = await require ( "prettier/standalone" ) ;
28- const parserBabel = await require ( "prettier/parser- babel" ) ;
28+ const prettier = await import ( "prettier/standalone" ) ;
29+ const parserBabel = await import ( "prettier/plugins/ babel" ) ;
2930 return async ( text : string ) => ( await prettier . format ( text , { parser : "json" , plugins : [ parserBabel ] } ) ) . trim ( ) ;
3031}
3132
@@ -46,15 +47,16 @@ async function formatJsSegment(formatter: (text: string) => Promise<string>, scr
4647async function getJsSegmentFormatter ( ) {
4748 const formatter = await getJavascriptFormatter ( ) ;
4849 return async ( segment : string ) => {
49- return "{{" + formatJsSegment ( formatter , segment . slice ( 2 , - 2 ) ) + "}}" ;
50+ return "{{" + await formatJsSegment ( formatter , segment . slice ( 2 , - 2 ) ) + "}}" ;
5051 } ;
5152}
5253
5354export async function formatStringWithJsSnippets ( text : string ) : Promise < string > {
5455 const jsSegmentFormatter = await getJsSegmentFormatter ( ) ;
55- return getDynamicStringSegments ( text )
56- . map ( ( s ) => ( isDynamicSegment ( s ) ? jsSegmentFormatter ( s ) : s ) )
57- . join ( "" ) ;
56+ const formatedSegments = await Promise . all (
57+ getDynamicStringSegments ( text ) . map ( ( s ) => ( isDynamicSegment ( s ) ? jsSegmentFormatter ( s ) : s ) )
58+ ) ;
59+ return formatedSegments . join ( "" ) ;
5860}
5961
6062export async function formatSqlWithJsSnippets ( text : string ) {
0 commit comments