@@ -123,64 +123,44 @@ export async function build(_options: CliOptions) {
123
123
async function generate ( options : ResolvedCliOptions ) {
124
124
const sourceCache = Array . from ( fileCache ) . map ( ( [ id , code ] ) => ( { id, code } ) )
125
125
126
- const preTransform = await transformFiles ( sourceCache , 'pre' )
127
- const defaultTransform = await transformFiles ( preTransform )
128
- const postTransform = await transformFiles ( defaultTransform , 'post' )
126
+ const afterPreTrans = await transformFiles ( sourceCache , 'pre' )
127
+ const afterDefaultTrans = await transformFiles ( afterPreTrans )
128
+ const afterPostTrans = await transformFiles ( afterDefaultTrans , 'post' )
129
129
130
130
// update source file
131
131
if ( options . writeTransformed ) {
132
132
await Promise . all (
133
- postTransform
133
+ afterPostTrans
134
134
. filter ( ( { transformedCode } ) => ! ! transformedCode )
135
- . map ( ( { transformedCode, id } ) => new Promise < void > ( ( resolve ) => {
135
+ . map ( async ( { transformedCode, id } ) => {
136
136
if ( existsSync ( id ) )
137
- fs . writeFile ( id , transformedCode as string , 'utf-8' ) . then ( resolve )
138
- } ) ) ,
137
+ await fs . writeFile ( id , transformedCode as string , 'utf-8' )
138
+ } ) ,
139
139
)
140
140
}
141
- const firstIdType = postTransform [ 0 ] . id . split ( '.' ) . pop ( ) !
142
- const isSameFileType = postTransform . every ( ( { id } ) => id . split ( '.' ) . pop ( ) === firstIdType )
143
- let css ! : string
144
- let matched : Set < string >
145
- if ( isSameFileType ) {
146
- // if all files are the same type, we can generate them all at once
147
- const { css : _css , matched : _matched } = await ctx . uno . generate (
148
- [ ...postTransform . map ( ( { code, transformedCode } ) => ( transformedCode ?? code ) . replace ( SKIP_COMMENT_RE , '' ) ) ] . join ( '\n' ) ,
141
+
142
+ const tokens = new Set < string > ( )
143
+
144
+ for ( const file of afterPostTrans ) {
145
+ const { matched } = await ctx . uno . generate (
146
+ ( file . transformedCode || file . code ) . replace ( SKIP_COMMENT_RE , '' ) ,
149
147
{
150
- preflights : options . preflights ,
151
- minify : options . minify ,
152
- id : postTransform [ 0 ] . id ,
148
+ preflights : false ,
149
+ minify : true ,
150
+ id : file . id ,
153
151
} ,
154
152
)
155
- css = _css
156
- matched = _matched
157
- }
158
- else {
159
- // if files are different types, we need to generate them one by one
160
- const result = await Promise . all ( postTransform . map ( ( { code, transformedCode, id } ) =>
161
- ctx . uno . generate ( transformedCode ?? code , {
162
- preflights : options . preflights ,
163
- minify : options . minify ,
164
- id,
165
- } ) ) )
166
- const cssCollection : Record < string , string [ ] > = { }
167
- result . forEach ( ( { css, layers } ) => {
168
- css
169
- . split ( / \/ \* .* ?\* \/ / g) // Remove inline comments
170
- . filter ( Boolean ) . forEach ( ( c , i ) => {
171
- if ( ! cssCollection [ layers [ i ] ] ) {
172
- cssCollection [ layers [ i ] ] = c . split ( '\n' )
173
- }
174
- else {
175
- // remove duplicates
176
- cssCollection [ layers [ i ] ] = [ ...new Set ( [ ...cssCollection [ layers [ i ] ] , ...c . split ( '\n' ) ] ) ]
177
- }
178
- } )
179
- } )
180
- css = result [ 0 ] . layers . map ( layer => `/* layer: ${ layer } */${ cssCollection [ layer ] . join ( '\n' ) } ` ) . join ( '\n' )
181
- matched = new Set ( result . map ( ( { matched } ) => [ ...matched ] ) . flat ( ) )
153
+ matched . forEach ( i => tokens . add ( i ) )
182
154
}
183
155
156
+ const { css, matched } = await ctx . uno . generate (
157
+ tokens ,
158
+ {
159
+ preflights : options . preflights ,
160
+ minify : options . minify ,
161
+ } ,
162
+ )
163
+
184
164
if ( options . stdout ) {
185
165
process . stdout . write ( css )
186
166
return
0 commit comments