@@ -2,30 +2,31 @@ import type { CodeType } from "lowcoder-core";
2
2
import { relaxedJSONToJSON } from "lowcoder-core" ;
3
3
import { getDynamicStringSegments , isDynamicSegment } from "lowcoder-core" ;
4
4
import { format as formatSQL } from "sql-formatter" ;
5
+ import estree from "prettier/plugins/estree" ;
5
6
import type { Language } from "./codeEditorTypes" ;
6
7
7
8
export 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" ) ;
10
11
return ( await prettier . format ( text , { parser : "css" , plugins : [ parserPlugin ] , semi : false } ) ) . trim ( ) ;
11
12
}
12
13
13
14
export 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" ) ;
16
17
return ( await prettier . format ( text , { parser : "html" , plugins : [ parserPlugin ] , semi : false } ) ) . trim ( ) ;
17
18
}
18
19
19
20
async 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" ) ;
22
23
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 ( ) ;
24
25
}
25
26
26
27
export 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" ) ;
29
30
return async ( text : string ) => ( await prettier . format ( text , { parser : "json" , plugins : [ parserBabel ] } ) ) . trim ( ) ;
30
31
}
31
32
@@ -46,15 +47,16 @@ async function formatJsSegment(formatter: (text: string) => Promise<string>, scr
46
47
async function getJsSegmentFormatter ( ) {
47
48
const formatter = await getJavascriptFormatter ( ) ;
48
49
return async ( segment : string ) => {
49
- return "{{" + formatJsSegment ( formatter , segment . slice ( 2 , - 2 ) ) + "}}" ;
50
+ return "{{" + await formatJsSegment ( formatter , segment . slice ( 2 , - 2 ) ) + "}}" ;
50
51
} ;
51
52
}
52
53
53
54
export async function formatStringWithJsSnippets ( text : string ) : Promise < string > {
54
55
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 ( "" ) ;
58
60
}
59
61
60
62
export async function formatSqlWithJsSnippets ( text : string ) {
0 commit comments