Skip to content

Commit 8f37263

Browse files
committed
Build pretty.js
1 parent d281db5 commit 8f37263

File tree

4 files changed

+10516
-10137
lines changed

4 files changed

+10516
-10137
lines changed

build.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ build_all () {
55
printf "\033c"
66

77
# Build all variants
8-
bun build.ts --version $1 --variant full
9-
bun build.ts --version $1 --variant lite
8+
bun build.ts --version $1 --variant full --meta
9+
bun build.ts --version $1 --variant full --pretty
10+
# bun build.ts --version $1 --variant lite
1011

1112
# Wait for key
1213
read -p ">> Press Enter to build again..."

build.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ enum BuildTarget {
2121
type BuildVariant = 'full' | 'lite';
2222

2323
const MINIFY_SYNTAX = true;
24-
const INDENT_SPACES = false;
2524

2625
function minifySvgImports(str: string): string {
2726
// Minify SVG imports
@@ -73,7 +72,7 @@ function removeComments(str: string): string {
7372
return str;
7473
}
7574

76-
function postProcess(str: string): string {
75+
function postProcess(str: string, pretty: boolean): string {
7776
// Unescape unicode charaters
7877
str = unescape((str.replace(/\\u/g, '%u')));
7978
// Replace \x00 to normal character
@@ -128,12 +127,16 @@ function postProcess(str: string): string {
128127
if (MINIFY_SYNTAX) {
129128
str = minifyIfElse(str);
130129

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+
}
135138
} else {
136-
return '\n';
139+
return (p1 === '}') ? '}' : '';
137140
}
138141
});
139142
}
@@ -189,7 +192,9 @@ async function buildPatches() {
189192
});
190193
}
191194

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+
193198
console.log('-- Target:', target);
194199
const startTime = performance.now();
195200

@@ -203,6 +208,9 @@ async function build(target: BuildTarget, version: string, variant: BuildVariant
203208
}
204209

205210
let outputMetaName = outputScriptName;
211+
if (pretty) {
212+
outputScriptName += '.pretty';
213+
}
206214
outputScriptName += '.user.js';
207215
outputMetaName += '.meta.js';
208216

@@ -231,7 +239,7 @@ async function build(target: BuildTarget, version: string, variant: BuildVariant
231239

232240
const {path} = output.outputs[0];
233241
// Get generated file
234-
let result = postProcess(await readFile(path, 'utf-8'));
242+
let result = postProcess(await readFile(path, 'utf-8'), pretty);
235243

236244
// Replace [[VERSION]] with real value
237245
let scriptHeader: string;
@@ -246,7 +254,7 @@ async function build(target: BuildTarget, version: string, variant: BuildVariant
246254
await Bun.write(path, scriptHeader + result);
247255

248256
// 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') {
250258
await Bun.write(outDir + '/' + outputMetaName, txtMetaHeader.replace('[[VERSION]]', version));
251259
}
252260

@@ -279,13 +287,25 @@ const { values, positionals } = parseArgs({
279287
type: 'string',
280288
default: 'full',
281289
},
290+
291+
pretty: {
292+
type: 'boolean',
293+
default: false,
294+
},
295+
296+
meta: {
297+
type: 'boolean',
298+
default: false,
299+
},
282300
},
283301
strict: true,
284302
allowPositionals: true,
285303
}) as {
286304
values: {
287305
version: string,
288306
variant: BuildVariant,
307+
pretty: boolean,
308+
meta: boolean,
289309
},
290310
positionals: string[],
291311
};
@@ -304,7 +324,7 @@ async function main() {
304324
const config = {};
305325
console.log(`Building: VERSION=${values['version']}, VARIANT=${values['variant']}`);
306326
for (const target of buildTargets) {
307-
await build(target, values['version']!!, values['variant'], config);
327+
await build(target, values, config);
308328
}
309329

310330
console.log('')

0 commit comments

Comments
 (0)