@@ -62,36 +62,58 @@ export async function loadDesignSystem(
62
62
63
63
// TODO: Formatting with prettier would be preferable, but it's too slow
64
64
// Need to figure out why and if we can make it faster
65
- css = css . map ( ( str ) => {
66
- if ( ! str ) return null
65
+ let roots = css . map ( ( str ) => {
66
+ if ( str === null ) return postcss . root ( )
67
+
68
+ let result = ''
69
+ for ( let i = 0 ; i < str . length ; ++ i ) {
70
+ if ( str [ i ] === '\\' ) {
71
+ result += str [ i ] + str [ i + 1 ]
72
+ i += 1
73
+ } else if ( str [ i ] === '"' ) {
74
+ let end = str . indexOf ( '"' , i + 1 )
75
+ result += str . slice ( i , end + 1 )
76
+ i = end
77
+ } else if ( str [ i ] === "'" ) {
78
+ let end = str . indexOf ( "'" , i + 1 )
79
+ result += str . slice ( i , end + 1 )
80
+ i = end
81
+ } else if ( str [ i ] === '{' ) {
82
+ result += ' {\n'
83
+ } else if ( str [ i ] === '}' ) {
84
+ result += '}\n'
85
+ } else if ( str [ i ] === ';' ) {
86
+ result += ';\n'
87
+ } else if ( str [ i ] === ':' ) {
88
+ result += ': '
89
+ } else {
90
+ result += str [ i ]
91
+ }
92
+ }
67
93
68
- let lines = str
69
- //
70
- . replaceAll ( '{' , ' {\n' )
71
- . replaceAll ( ';' , ' ;\n' )
72
- . replaceAll ( '}' , ' }\n' )
73
- . split ( '\n' )
94
+ let lines = result . split ( '\n' )
74
95
75
96
let depth = 0
76
97
77
98
for ( let i = 0 ; i < lines . length ; ++ i ) {
78
99
let line = lines [ i ]
79
100
if ( line . includes ( '}' ) ) depth --
80
- let indent = ' ' . repeat ( depth )
101
+ let indent = ' ' . repeat ( Math . max ( 0 , depth ) )
81
102
line = indent + line
82
103
if ( line . includes ( '{' ) ) depth ++
83
104
lines [ i ] = line
84
105
}
85
106
86
- return lines . join ( '\n' )
87
- } )
107
+ let pretty = lines . join ( '\n' ) . trim ( )
88
108
89
- let root = css . map ( ( str ) => {
90
- if ( str === null ) return postcss . root ( )
91
- return postcss . parse ( str )
109
+ try {
110
+ return postcss . parse ( pretty )
111
+ } catch {
112
+ return postcss . parse ( str )
113
+ }
92
114
} )
93
115
94
- return root
116
+ return roots
95
117
} ,
96
118
97
119
toCss ( nodes : postcss . Root | postcss . Node [ ] ) : string {
0 commit comments