|
| 1 | +import type { BundlerEngine, BundlerEngineOptions, BundlerEngineOutput } from '@mastra/core/bundler'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Configuration options for the Bun bundler engine. |
| 5 | + */ |
| 6 | +export interface BunBundlerEngineConfig { |
| 7 | + /** |
| 8 | + * Whether to minify the output. |
| 9 | + * @default true |
| 10 | + */ |
| 11 | + minify?: boolean; |
| 12 | + |
| 13 | + /** |
| 14 | + * Target environment for the bundle. |
| 15 | + * @default 'bun' |
| 16 | + */ |
| 17 | + target?: 'bun' | 'node' | 'browser'; |
| 18 | + |
| 19 | + /** |
| 20 | + * Additional packages to mark as external (in addition to those passed via options). |
| 21 | + */ |
| 22 | + external?: string[]; |
| 23 | + |
| 24 | + /** |
| 25 | + * Enable splitting for code splitting. |
| 26 | + * @default true |
| 27 | + */ |
| 28 | + splitting?: boolean; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Bun-based bundler engine for Mastra. |
| 33 | + * |
| 34 | + * This engine uses Bun's native bundler for fast, efficient bundling. |
| 35 | + * It requires Bun to be installed and available in the environment. |
| 36 | + * |
| 37 | + * @example |
| 38 | + * ```typescript |
| 39 | + * import { Mastra } from '@mastra/core'; |
| 40 | + * import { BunBundlerEngine } from '@mastra/bundler-bun'; |
| 41 | + * |
| 42 | + * export const mastra = new Mastra({ |
| 43 | + * agents: { myAgent }, |
| 44 | + * bundler: { |
| 45 | + * engine: new BunBundlerEngine(), |
| 46 | + * }, |
| 47 | + * }); |
| 48 | + * ``` |
| 49 | + * |
| 50 | + * @example With configuration |
| 51 | + * ```typescript |
| 52 | + * import { BunBundlerEngine } from '@mastra/bundler-bun'; |
| 53 | + * |
| 54 | + * const engine = new BunBundlerEngine({ |
| 55 | + * minify: true, |
| 56 | + * target: 'bun', |
| 57 | + * splitting: true, |
| 58 | + * }); |
| 59 | + * ``` |
| 60 | + */ |
| 61 | +export class BunBundlerEngine implements BundlerEngine { |
| 62 | + readonly name = 'bun'; |
| 63 | + |
| 64 | + private config: BunBundlerEngineConfig; |
| 65 | + |
| 66 | + constructor(config: BunBundlerEngineConfig = {}) { |
| 67 | + this.config = { |
| 68 | + minify: true, |
| 69 | + target: 'bun', |
| 70 | + splitting: true, |
| 71 | + ...config, |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + async bundle(options: BundlerEngineOptions): Promise<BundlerEngineOutput> { |
| 76 | + // Check if Bun is available |
| 77 | + if (typeof globalThis.Bun === 'undefined') { |
| 78 | + throw new Error( |
| 79 | + 'BunBundlerEngine requires Bun runtime. ' + |
| 80 | + 'Please run with Bun or install Bun: https://bun.sh\n' + |
| 81 | + 'If you want to use Rollup instead, remove the engine option from your bundler config.', |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + const entrypoints = Object.values(options.input); |
| 86 | + |
| 87 | + // Merge external dependencies |
| 88 | + const external = [...options.external, ...(this.config.external || [])]; |
| 89 | + |
| 90 | + // Store the build result for writing |
| 91 | + let buildResult: Awaited<ReturnType<typeof Bun.build>> | undefined; |
| 92 | + |
| 93 | + return { |
| 94 | + write: async () => { |
| 95 | + buildResult = await Bun.build({ |
| 96 | + entrypoints, |
| 97 | + outdir: options.outputDir, |
| 98 | + target: this.config.target, |
| 99 | + minify: this.config.minify, |
| 100 | + splitting: this.config.splitting, |
| 101 | + sourcemap: options.sourcemap ? 'external' : 'none', |
| 102 | + external, |
| 103 | + define: options.define, |
| 104 | + naming: { |
| 105 | + // Match Rollup's output naming convention |
| 106 | + entry: '[dir]/[name].mjs', |
| 107 | + chunk: '[name]-[hash].mjs', |
| 108 | + }, |
| 109 | + }); |
| 110 | + |
| 111 | + if (!buildResult.success) { |
| 112 | + const errors = buildResult.logs |
| 113 | + .filter(log => log.level === 'error') |
| 114 | + .map(log => log.message) |
| 115 | + .join('\n'); |
| 116 | + |
| 117 | + throw new Error(`Bun bundler failed:\n${errors}`); |
| 118 | + } |
| 119 | + |
| 120 | + return buildResult; |
| 121 | + }, |
| 122 | + close: async () => { |
| 123 | + // Bun.build doesn't require explicit cleanup |
| 124 | + buildResult = undefined; |
| 125 | + }, |
| 126 | + }; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Create a Bun bundler engine with the given configuration. |
| 132 | + * This is a convenience function for creating a BunBundlerEngine instance. |
| 133 | + * |
| 134 | + * @example |
| 135 | + * ```typescript |
| 136 | + * import { createBunEngine } from '@mastra/bundler-bun'; |
| 137 | + * |
| 138 | + * export const mastra = new Mastra({ |
| 139 | + * bundler: { |
| 140 | + * engine: createBunEngine({ minify: true }), |
| 141 | + * }, |
| 142 | + * }); |
| 143 | + * ``` |
| 144 | + */ |
| 145 | +export function createBunEngine(config?: BunBundlerEngineConfig): BunBundlerEngine { |
| 146 | + return new BunBundlerEngine(config); |
| 147 | +} |
| 148 | + |
| 149 | +// Re-export types for convenience |
| 150 | +export type { BundlerEngine, BundlerEngineOptions, BundlerEngineOutput } from '@mastra/core/bundler'; |
0 commit comments