Skip to content

Commit

Permalink
optimize build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
inetol committed Oct 26, 2024
1 parent fdbbc0c commit 931233c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 90 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"scripts": {
"build": "bun run build:server",
"build:server": "rm -rf ./dist/ && bun vite build && bun run script:standalone && bun run script:minify && bun run script:compress",
"build:server": "rm -rf ./dist/ && bun vite build && bun run script:standalone && bun run script:compress",
"clean:git:all": "bun run clean:git:untracked && bun run clean:git:gc && bun run clean:git:hooks",
"clean:git:all:force": "bun run clean:git:untracked:force && bun run clean:git:gc && bun run clean:git:hooks",
"clean:git:gc": "git gc --aggressive --prune",
Expand All @@ -21,7 +21,6 @@
"lint:biome": "bun biome lint",
"lint:tsc": "bun tsc --noEmit",
"script:compress": "bun run ./scripts/compress.ts",
"script:minify": "bun run ./scripts/minify.ts",
"script:standalone": "bun run ./scripts/standalone.ts",
"start": "bun run start:server",
"start:dev": "bun vite",
Expand Down
7 changes: 4 additions & 3 deletions scripts/compress.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { brotliCompressSync, gzipSync } from 'node:zlib';
import { logger } from '@x-util/logger.ts';
import { findFiles, writeFile } from './utils.ts';

const rootClientDirectory = './dist/client/';
Expand All @@ -9,15 +10,15 @@ await Promise.all(
rootClientFiles.map(async (file) => {
const fileContent = await Bun.file(file).arrayBuffer();

console.debug('[COMPRESS] Compressing:', file);
logger.info('[BUILD] Compressing:', file);

await writeFile(
`${file}.gz`,
gzipSync(fileContent, {
level: 9
}).buffer
})
);

await writeFile(`${file}.br`, brotliCompressSync(fileContent).buffer);
await writeFile(`${file}.br`, brotliCompressSync(fileContent));
})
);
48 changes: 0 additions & 48 deletions scripts/minify.ts

This file was deleted.

57 changes: 20 additions & 37 deletions scripts/standalone.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,36 @@
import { readdir, rm } from 'node:fs/promises';
import { dirname } from 'node:path';
import { join } from 'node:path/posix';
import esbuild from 'esbuild';
import { logger } from '@x-util/logger.ts';
import { $ } from 'bun';

const root = process.cwd();
const serverOutDir = './dist/server/';
const serverOutDirAbs = join(root, serverOutDir);
const serverEntrypoint = ['./src/server.ts'];

const buildStandalone = async () => {
const result = await esbuild.build({
platform: 'node',
target: 'esnext',
const result = await Bun.build({
entrypoints: serverEntrypoint,
target: 'bun',
outdir: serverOutDirAbs,
naming: 'index.js',
format: 'esm',
bundle: true,
minify: true,
treeShaking: true,
external: ['bun'],
entryPoints: serverEntrypoint,
sourcemap: false,
outfile: `${serverOutDirAbs}index.js`,
splitting: false,
allowOverwrite: true,
metafile: true,
logOverride: { 'ignored-bare-import': 'silent' }
packages: 'bundle',
sourcemap: 'none',
minify: true
});

const bundledFilesFromOutDir = Object.keys(result.metafile.inputs).filter(
(relativeFile) => relativeFile.endsWith(relativeFile) && relativeFile.startsWith('dist/')
);

await Promise.all(
bundledFilesFromOutDir.map(async (relativeFile) => {
await rm(join(root, relativeFile));
})
);

const relativeDirs = new Set(bundledFilesFromOutDir.map((file) => dirname(file)));
for (const relativeDir of relativeDirs) {
const absDir = join(root, relativeDir);
const files = await readdir(absDir);
if (!files.length) {
await rm(absDir, { recursive: true });
if (relativeDir.startsWith(serverOutDir)) {
relativeDirs.add(dirname(relativeDir));
}
}
if (!result.success) {
logger.error(result.logs);
process.exit(1);
}

// Cleanup
await $`rm -rf ./dist/server/assets/`;
await $`rm -rf ./dist/server/chunks/`;
await $`rm -rf ./dist/server/entries/`;
await $`rm -rf ./dist/server/*.mjs`;
};

console.info('[STANDALONE] Running...');
logger.info('[BUILD] Creating standalone...');
await buildStandalone();

0 comments on commit 931233c

Please sign in to comment.