11#!/usr/bin/env bun
2- import plugin from "bun-plugin-tailwind" ;
32import { existsSync } from "fs" ;
43import { rm } from "fs/promises" ;
54import path from "path" ;
65
76if ( process . argv . includes ( "--help" ) || process . argv . includes ( "-h" ) ) {
87 console . log ( `
9- 🏗️ Bun Build Script
8+ 🏗️ Cafe CLI Build Script
109
1110Usage: bun run build.ts [options]
1211
1312Common Options:
1413 --outdir <path> Output directory (default: "dist")
15- --minify Enable minification (or --minify.grayspace, --minify.syntax, etc)
16- --sourcemap <type> Sourcemap type: none|linked|inline|external
17- --target <target> Build target: browser|bun|node
18- --format <format> Output format: esm|cjs|iife
19- --splitting Enable code splitting
20- --packages <type> Package handling: bundle|external
21- --public-path <path> Public path for assets
22- --env <mode> Environment handling: inline|disable|prefix*
23- --conditions <list> Package.json export conditions (comma separated)
24- --external <list> External packages (comma separated)
25- --banner <text> Add banner text to output
26- --footer <text> Add footer text to output
27- --define <obj> Define global constants (e.g. --define.VERSION=1.0.0)
14+ --minify Enable minification
15+ --sourcemap <type> Sourcemap type: none|linked|inline|external
16+ --target <target> Build target: bun|node
2817 --help, -h Show this help message
2918
3019Example:
31- bun run build.ts --outdir=dist --minify --sourcemap=linked --external=react,react-dom
20+ bun run build.ts --outdir=dist --minify --sourcemap=linked
3221` ) ;
3322 process . exit ( 0 ) ;
3423}
3524
36- const toCamelCase = ( str : string ) : string => str . replace ( / - ( [ a - z ] ) / g, g => g [ 1 ] . toUpperCase ( ) ) ;
25+ const toCamelCase = ( str : string ) : string => str . replace ( / - ( [ a - z ] ) / g, ( _ , c : string ) => c . toUpperCase ( ) ) ;
3726
3827const parseValue = ( value : string ) : any => {
3928 if ( value === "true" ) return true ;
@@ -47,8 +36,8 @@ const parseValue = (value: string): any => {
4736 return value ;
4837} ;
4938
50- function parseArgs ( ) : Partial < Bun . BuildConfig > {
51- const config : Partial < Bun . BuildConfig > = { } ;
39+ function parseArgs ( ) : Record < string , unknown > {
40+ const config : Record < string , unknown > = { } ;
5241 const args = process . argv . slice ( 2 ) ;
5342
5443 for ( let i = 0 ; i < args . length ; i ++ ) {
@@ -81,9 +70,9 @@ function parseArgs(): Partial<Bun.BuildConfig> {
8170 key = toCamelCase ( key ) ;
8271
8372 if ( key . includes ( "." ) ) {
84- const [ parentKey , childKey ] = key . split ( "." ) ;
73+ const [ parentKey , childKey ] = key . split ( "." , 2 ) as [ string , string ] ;
8574 config [ parentKey ] = config [ parentKey ] || { } ;
86- config [ parentKey ] [ childKey ] = parseValue ( value ) ;
75+ ( config [ parentKey ] as Record < string , unknown > ) [ childKey ] = parseValue ( value ) ;
8776 } else {
8877 config [ key ] = parseValue ( value ) ;
8978 }
@@ -108,7 +97,7 @@ const formatFileSize = (bytes: number): string => {
10897console . log ( "\n🚀 Starting build process...\n" ) ;
10998
11099const cliConfig = parseArgs ( ) ;
111- const outdir = cliConfig . outdir || path . join ( process . cwd ( ) , "dist" ) ;
100+ const outdir = ( typeof cliConfig . outdir === "string" ? cliConfig . outdir : null ) || path . join ( process . cwd ( ) , "dist" ) ;
112101
113102if ( existsSync ( outdir ) ) {
114103 console . log ( `🗑️ Cleaning previous build at ${ outdir } ` ) ;
@@ -117,18 +106,20 @@ if (existsSync(outdir)) {
117106
118107const start = performance . now ( ) ;
119108
120- const entrypoints = [ ...new Bun . Glob ( "**.html" ) . scanSync ( "src" ) ]
121- . map ( a => path . resolve ( "src" , a ) )
122- . filter ( dir => ! dir . includes ( "node_modules" ) ) ;
123- console . log ( `📄 Found ${ entrypoints . length } HTML ${ entrypoints . length === 1 ? "file" : "files" } to process\n` ) ;
109+ const entrypoints = [ path . resolve ( "src" , "cli.tsx" ) ] ;
110+ console . log ( `📄 Building CLI from ${ entrypoints [ 0 ] } \n` ) ;
124111
125112const result = await Bun . build ( {
126113 entrypoints,
127114 outdir,
128- plugins : [ plugin ] ,
129115 minify : true ,
130- target : "browser " ,
116+ target : "bun " ,
131117 sourcemap : "linked" ,
118+ external : [
119+ "react-devtools-core" ,
120+ "electron" ,
121+ "chromium-bidi" ,
122+ ] ,
132123 define : {
133124 "process.env.NODE_ENV" : JSON . stringify ( "production" ) ,
134125 } ,
0 commit comments