diff --git a/bun.lockb b/bun.lockb index 7fd744c..75023fb 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index bfae418..3dcb14d 100644 --- a/package.json +++ b/package.json @@ -4,19 +4,28 @@ "module": "src/index.ts", "type": "module", "main": "out/index.js", + "types": "out/index.d.ts", + "files": [ + "out" + ], "scripts": { "test": "bun run testing/demo.test.ts", "test:inspect": "bun --inspect testing/demo.test.ts", - "build": "bun run build:types && bun build src/index.ts --outdir out", - "build:packages": "bun run build:types && bun build src/index.ts --packages external --outdir out", - "build:nocheck": "bun run build:types:nocheck && bun build src/index.ts --outdir out --noCheck", + + "build": "bun run build:types && bun run build:core", + "build:packages": "bun run build:types && bun run build:core -- --packages external", + "build:nocheck": "bun run build:types:nocheck && bun run build:core", + + "build:core": "bun build src/index.ts --outdir out --drop=console,debugger", "build:types": "tsc --emitDeclarationOnly --project tsconfig.json", - "build:types:nocheck": "tsc --emitDeclarationOnly --project tsconfig.json --noCheck", - "build:watch": "concurrently \"tsc --emitDeclarationOnly --project tsconfig.json --watch\" \"bun build src/index.ts --outdir out --watch\"", - "build:packages:watch": "concurrently \"tsc --emitDeclarationOnly --project tsconfig.json --watch\" \"bun build src/index.ts --packages external --outdir out --watch\"" + "build:types:nocheck": "bun run build:types -- --noCheck", + + "build:watch": "concurrently \"bun run build:types -- --watch\" \"bun run build:core -- --watch\"", + "build:packages:watch": "concurrently \"bun run build:types -- --watch\" \"bun run build:core -- --packages external --watch\"" }, "devDependencies": { "@types/bun": "latest", + "commander": "^13.0.0", "concurrently": "^9.1.0" }, "peerDependencies": { diff --git a/scripts/build.ts b/scripts/build.ts new file mode 100644 index 0000000..7657a41 --- /dev/null +++ b/scripts/build.ts @@ -0,0 +1,92 @@ +#!/usr/bin/env bun +/** + * Not used at the moment, was just playing around with the idea of using the bun JavaScript API to build the project. + */ +import { Command } from 'commander'; +import { build, type BuildConfig } from 'bun'; +import concurrently from 'concurrently'; + +// Initialize Commander to handle CLI commands and options +const program = new Command(); + +// Paths and base TypeScript compilation command +const srcFile = 'src/index.ts'; +const outDir = 'out'; +const tscBaseCmd = 'tsc --emitDeclarationOnly --project tsconfig.json'; + +/** + * Handles the build process with optional watch and type emission. + * + * @param options - Object containing flags for watch, packages, noCheck, and typesOnly. + */ +async function runBuild(options: { + watch?: boolean; + packages?: boolean; + noCheck?: boolean; + typesOnly?: boolean; +}) { + // Handle TypeScript-only builds (e.g., for emitting declaration files without bundling) + if (options.typesOnly) { + // Construct the TypeScript command dynamically based on flags + const tscCmd = `${tscBaseCmd}${options.noCheck ? ' --noCheck' : ''}${options.watch ? ' --watch' : ''}`; + + // Run TypeScript compilation (watch mode if specified) + await Bun.spawn(tscCmd.split(' ')).exited; + return; // Exit early since no Bun build is needed for types-only mode + } + + // Define options for the Bun build process + const buildOptions: BuildConfig = { + entrypoints: [srcFile], + outdir: outDir, + drop: ['console', 'debugger'], // Strip console and debugger statements + packages: options.packages ? 'external' : undefined + }; + + // Handle watch mode: run both TypeScript and Bun build concurrently + if (options.watch) { + concurrently([ + { + // Run TypeScript watch for continuous type emission + command: `${tscBaseCmd} --watch${options.noCheck ? ' --noCheck' : ''}`, + name: 'tsc', // Label the TypeScript process for logging + }, + { + // Run Bun build in watch mode + command: `bun build ${srcFile} --outdir ${outDir} ${ + options.packages ? '--packages external' : '' + } ${options.noCheck ? '--noCheck' : ''} --watch`, + name: 'bun', // Label the Bun process for logging + }, + ], { + + }); + } else { + // Non-watch mode: Run TypeScript once, then trigger Bun build + await Bun.spawn(tscBaseCmd.split(' ')).exited; + + // Perform a standard Bun build + await build(buildOptions); + console.log(`Build completed.`); + } +} + +// Register the build command and attach optional flags +program + .command('build') + .description('Build project with options') + .option('--watch', 'Watch for changes and rebuild') + .option('--packages', 'Bundle external packages') + .option('--nocheck', 'Skip type checking') + .option('--types-only', 'Emit types without compiling the project') + .action((opts) => { + runBuild({ + watch: opts.watch, + packages: opts.packages, + noCheck: opts.nocheck, + typesOnly: opts.typesOnly, + }); + }); + +// Parse and execute CLI input +program.parse(); \ No newline at end of file