From 89d8ab1ee58278a9a96cdc31c679d0a0a0d865af Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Tue, 23 Jul 2024 20:53:56 +0700 Subject: [PATCH] fix(apis/websocket): build and runtime issues --- apis/websocket/package.json | 2 +- apis/websocket/scripts/build.ts | 44 ++++++++++++++++++--------------- apis/websocket/src/context.ts | 5 +--- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/apis/websocket/package.json b/apis/websocket/package.json index da32924..681337e 100755 --- a/apis/websocket/package.json +++ b/apis/websocket/package.json @@ -37,4 +37,4 @@ "@types/ws": "^8.5.10", "typed-emitter": "^2.1.0" } -} \ No newline at end of file +} diff --git a/apis/websocket/scripts/build.ts b/apis/websocket/scripts/build.ts index 360fa97..7085b24 100644 --- a/apis/websocket/scripts/build.ts +++ b/apis/websocket/scripts/build.ts @@ -1,29 +1,33 @@ import { createLogger } from '@revanced/bot-shared' import { cp, rm } from 'fs/promises' -async function build(): Promise { - const logger = createLogger() +const logger = createLogger() - logger.info('Cleaning previous build...') - await rm('./dist', { recursive: true }) +logger.info('Cleaning previous build...') +await rm('./dist', { recursive: true }) - logger.info('Building Tesseract.js worker...') - await Bun.build({ - entrypoints: ['../../node_modules/tesseract.js/src/worker-script/node/index.js'], - target: 'bun', - outdir: './dist/worker', - }) +logger.info('Building WebSocket API...') +await Bun.build({ + entrypoints: ['./src/index.ts'], + outdir: './dist', + target: 'bun', + minify: true, + sourcemap: 'external', +}) - logger.info('Copying Tesseract.js WASM...') - await cp('../../node_modules/tesseract.js-core', './dist/worker/core', { recursive: true }) +logger.info('Building Tesseract.js worker...') +await Bun.build({ + entrypoints: ['../../node_modules/tesseract.js/src/worker-script/node/index.js'], + target: 'bun', + outdir: './dist/worker', + minify: true, + sourcemap: 'external', +}) - logger.info('Building WebSocket API...') - await Bun.build({ - entrypoints: ['./src/index.ts'], - outdir: './dist', - target: 'bun', - }) -} +// Tesseract.js is really bad for minification +// It forcefully requires this core module to be present which contains the WASM files +logger.info('Copying Tesseract.js Core...') +await cp('../../node_modules/tesseract.js-core', './dist/node_modules/tesseract.js-core', { recursive: true }) -await build() +logger.info('Copying config...') await cp('config.json', 'dist/config.json') diff --git a/apis/websocket/src/context.ts b/apis/websocket/src/context.ts index 0f5301d..033f473 100644 --- a/apis/websocket/src/context.ts +++ b/apis/websocket/src/context.ts @@ -55,12 +55,9 @@ export interface WitMessageResponse { const TesseractWorkerDirPath = joinPath(import.meta.dir, 'worker') const TesseractWorkerPath = joinPath(TesseractWorkerDirPath, 'index.js') -const TesseractCorePath = joinPath(TesseractWorkerDirPath, 'core') export const tesseract = await createTesseractWorker( 'eng', OEM.DEFAULT, - (await pathExists(TesseractWorkerDirPath)) - ? { workerPath: TesseractWorkerPath, corePath: TesseractCorePath } - : undefined, + (await pathExists(TesseractWorkerDirPath)) ? { workerPath: TesseractWorkerPath } : undefined, )