@@ -21,7 +21,6 @@ enum BuildTarget {
21
21
type BuildVariant = 'full' | 'lite' ;
22
22
23
23
const MINIFY_SYNTAX = true ;
24
- const INDENT_SPACES = false ;
25
24
26
25
function minifySvgImports ( str : string ) : string {
27
26
// Minify SVG imports
@@ -73,7 +72,7 @@ function removeComments(str: string): string {
73
72
return str ;
74
73
}
75
74
76
- function postProcess ( str : string ) : string {
75
+ function postProcess ( str : string , pretty : boolean ) : string {
77
76
// Unescape unicode charaters
78
77
str = unescape ( ( str . replace ( / \\ u / g, '%u' ) ) ) ;
79
78
// Replace \x00 to normal character
@@ -128,12 +127,16 @@ function postProcess(str: string): string {
128
127
if ( MINIFY_SYNTAX ) {
129
128
str = minifyIfElse ( str ) ;
130
129
131
- str = str . replaceAll ( / \n ( \s + ) / g, ( match , p1 ) => {
132
- if ( INDENT_SPACES ) {
133
- const len = p1 . length / 2 ;
134
- return '\n' + ' ' . repeat ( len ) ;
130
+ str = str . replaceAll ( / \n ( \s + | \} ) / g, ( match , p1 ) => {
131
+ if ( pretty ) {
132
+ if ( p1 === '}' ) {
133
+ return '\n}' ;
134
+ } else {
135
+ const len = p1 . length / 2 ;
136
+ return '\n' + ' ' . repeat ( len ) ;
137
+ }
135
138
} else {
136
- return '\n ';
139
+ return ( p1 === '}' ) ? '}' : ' ';
137
140
}
138
141
} ) ;
139
142
}
@@ -189,7 +192,9 @@ async function buildPatches() {
189
192
} ) ;
190
193
}
191
194
192
- async function build ( target : BuildTarget , version : string , variant : BuildVariant , config : any = { } ) {
195
+ async function build ( target : BuildTarget , params : { version : string , variant : BuildVariant , pretty : boolean , meta : boolean } , config : any = { } ) {
196
+ const { version, variant, pretty, meta } = params ;
197
+
193
198
console . log ( '-- Target:' , target ) ;
194
199
const startTime = performance . now ( ) ;
195
200
@@ -203,6 +208,9 @@ async function build(target: BuildTarget, version: string, variant: BuildVariant
203
208
}
204
209
205
210
let outputMetaName = outputScriptName ;
211
+ if ( pretty ) {
212
+ outputScriptName += '.pretty' ;
213
+ }
206
214
outputScriptName += '.user.js' ;
207
215
outputMetaName += '.meta.js' ;
208
216
@@ -231,7 +239,7 @@ async function build(target: BuildTarget, version: string, variant: BuildVariant
231
239
232
240
const { path} = output . outputs [ 0 ] ;
233
241
// Get generated file
234
- let result = postProcess ( await readFile ( path , 'utf-8' ) ) ;
242
+ let result = postProcess ( await readFile ( path , 'utf-8' ) , pretty ) ;
235
243
236
244
// Replace [[VERSION]] with real value
237
245
let scriptHeader : string ;
@@ -246,7 +254,7 @@ async function build(target: BuildTarget, version: string, variant: BuildVariant
246
254
await Bun . write ( path , scriptHeader + result ) ;
247
255
248
256
// Create meta file (don't build if it's beta version)
249
- if ( ! version . includes ( 'beta' ) && variant === 'full' ) {
257
+ if ( meta && ! version . includes ( 'beta' ) && variant === 'full' ) {
250
258
await Bun . write ( outDir + '/' + outputMetaName , txtMetaHeader . replace ( '[[VERSION]]' , version ) ) ;
251
259
}
252
260
@@ -279,13 +287,25 @@ const { values, positionals } = parseArgs({
279
287
type : 'string' ,
280
288
default : 'full' ,
281
289
} ,
290
+
291
+ pretty : {
292
+ type : 'boolean' ,
293
+ default : false ,
294
+ } ,
295
+
296
+ meta : {
297
+ type : 'boolean' ,
298
+ default : false ,
299
+ } ,
282
300
} ,
283
301
strict : true ,
284
302
allowPositionals : true ,
285
303
} ) as {
286
304
values : {
287
305
version : string ,
288
306
variant : BuildVariant ,
307
+ pretty : boolean ,
308
+ meta : boolean ,
289
309
} ,
290
310
positionals : string [ ] ,
291
311
} ;
@@ -304,7 +324,7 @@ async function main() {
304
324
const config = { } ;
305
325
console . log ( `Building: VERSION=${ values [ 'version' ] } , VARIANT=${ values [ 'variant' ] } ` ) ;
306
326
for ( const target of buildTargets ) {
307
- await build ( target , values [ 'version' ] ! ! , values [ 'variant' ] , config ) ;
327
+ await build ( target , values , config ) ;
308
328
}
309
329
310
330
console . log ( '' )
0 commit comments