From 4271583a5a5fd633754dea447f90f205159c0688 Mon Sep 17 00:00:00 2001 From: redphx <96280+redphx@users.noreply.github.com> Date: Sat, 19 Oct 2024 18:46:13 +0700 Subject: [PATCH] Add MINIFY_SYNTAX flag in build.ts --- build.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/build.ts b/build.ts index 37c0f5a..5a3cbe4 100644 --- a/build.ts +++ b/build.ts @@ -20,6 +20,8 @@ enum BuildTarget { type BuildVariant = 'full' | 'lite'; +const MINIFY_SYNTAX = true; + const postProcess = (str: string): string => { // Unescape unicode charaters str = unescape((str.replace(/\\u/g, '%u'))); @@ -70,9 +72,6 @@ const postProcess = (str: string): string => { // Collapse empty brackets str = str.replaceAll(/\{[\s\n]+\}/g, '{}'); - // Collapse if/else blocks without curly braces - str = str.replaceAll(/((if \(.*?\)|else)\n\s+)/g, '$2 '); - // Remove blank lines str = str.replaceAll(/\n([\s]*)\n/g, "\n"); @@ -91,10 +90,15 @@ const postProcess = (str: string): string => { // str = str.replaceAll(/ \(([^\s,.$()]+)\) =>/g, ' $1 =>'); // Set indent to 1 space - str = str.replaceAll(/\n(\s+)/g, (match, p1) => { - const len = p1.length / 2; - return '\n' + ' '.repeat(len); - }); + if (MINIFY_SYNTAX) { + // Collapse if/else blocks without curly braces + str = str.replaceAll(/((if \(.*?\)|else)\n\s+)/g, '$2 '); + + str = str.replaceAll(/\n(\s+)/g, (match, p1) => { + const len = p1.length / 2; + return '\n' + ' '.repeat(len); + }); + } assert(str.includes('/* ADDITIONAL CODE */')); assert(str.includes('window.BX_EXPOSED = BxExposed')); @@ -128,7 +132,7 @@ const build = async (target: BuildTarget, version: string, variant: BuildVariant outdir: outDir, naming: outputScriptName, minify: { - syntax: true, + syntax: MINIFY_SYNTAX, }, define: { 'Bun.env.BUILD_TARGET': JSON.stringify(target),