### Bunup Package with Multiple Entry Points and Formats Source: https://bunup.dev/docs/guide/workspaces This example shows a package ('main') configured with multiple build targets, including a main entry point with both ESM and CJS formats, and a separate CLI entry point with only ESM format. ```typescript export default defineWorkspace([ { name: "main", root: "packages/main", config: [ { entry: "src/index.ts", name: "main", format: ["esm", "cjs"], }, { entry: "src/cli.ts", name: "cli", format: ["esm"], }, ], }, ]); ``` -------------------------------- ### Bunup Path Resolution Example Source: https://bunup.dev/docs/guide/workspaces Illustrates how paths for `entry` and `outDir` in Bunup package configurations are resolved relative to the package's root directory. This ensures consistent path handling within each package. ```typescript { name: "core", root: "packages/core", config: { entry: "src/index.ts", // resolves to packages/core/src/index.ts outDir: "dist", // outputs to packages/core/dist/ }, } ``` -------------------------------- ### Using Built-in Copy Plugin in Bunup Source: https://bunup.dev/docs/guide/plugins Shows an example of utilizing Bunup's built-in `copy` plugin to transfer files and directories to the build output directory. This is useful for including static assets. ```typescript import { defineConfig } from "bunup"; import { copy } from "bunup/plugins"; export default defineConfig({ entry: "src/index.ts", plugins: [copy(["README.md", "assets/**/*"])], }); ``` -------------------------------- ### Using Bunup-Specific Plugins Source: https://bunup.dev/docs/guide/plugins Illustrates how to define and use custom Bunup plugins, which offer additional lifecycle hooks like `onBuildStart` and `onBuildDone` to interact with the build process. ```typescript import { defineConfig, type BunupPlugin } from "bunup"; const myBunupPlugin: BunupPlugin = { name: "my-bunup-plugin", hooks: { onBuildStart(options) { // Called before build starts }, onBuildDone(context) { // Called after build completes }, }, }; export default defineConfig({ entry: "src/index.ts", plugins: [myBunupPlugin], }); ``` -------------------------------- ### Example package.json with Exports Source: https://bunup.dev/docs/extra-options/exports Illustrates the structure of a package.json file after Bunup has automatically generated the 'exports' field, including ESM/CJS formats and type declarations. ```json { "name": "my-package", "version": "1.0.0", "type": "module", "files": [ // [!code ++] "dist" // [!code ++] ], // [!code ++] "module": "./dist/index.js", // [!code ++] "main": "./dist/index.cjs", // [!code ++] "types": "./dist/index.d.ts", // [!code ++] "exports": { // [!code ++] ".": { // [!code ++] "import": { // [!code ++] "types": "./dist/index.d.ts", // [!code ++] "default": "./dist/index.js" // [!code ++] }, // [!code ++] "require": { // [!code ++] "types": "./dist/index.d.cts", // [!code ++] "default": "./dist/index.cjs" // [!code ++] } // [!code ++] } // [!code ++] } // [!code ++] } ``` -------------------------------- ### Enable Basic Minification (CLI & Config) Source: https://bunup.dev/docs/guide/options Activate all available minification options to reduce the size of your bundled output. This is a convenient way to apply comprehensive optimizations for production builds. ```sh bunup --minify ``` ```typescript export default defineConfig({ minify: true, }); ``` -------------------------------- ### Install Bunup as a Dev Dependency Source: https://bunup.dev/index Installs Bunup as a development dependency in the project using the 'bun add' command. This makes Bunup available for build scripts within the project. ```shell bun add --dev bunup ``` -------------------------------- ### Basic Bunup Configuration Source: https://bunup.dev/docs/guide/config-file Sets up a basic `bunup.config.ts` file in the project root. This is the simplest way to centralize build settings. It requires importing `defineConfig` from 'bunup'. ```typescript import { defineConfig } from "bunup"; export default defineConfig({ // ...your configuration options go here }); ``` -------------------------------- ### Install React Compiler Plugin Source: https://bunup.dev/docs/recipes/react Install the React Compiler plugin for Bunup using npm or yarn. This plugin optimizes React components automatically during the build process. ```bash bun add --dev @bunup/plugin-react-compiler ``` -------------------------------- ### Use Glob Patterns for Entry Points (CLI & Config) Source: https://bunup.dev/docs/guide/typescript-declarations Leverage glob patterns to match multiple files for declaration generation. This includes simple patterns like `src/**/*.ts` and exclusion patterns starting with `!`. Both CLI and `bunup.config.ts` support these patterns for flexible file selection. ```sh # Single glob pattern bunup --dts.entry "src/public/**/*.ts" # Multiple patterns (including exclusions) bunup --dts.entry "src/public/**/*.ts,!src/public/dev/**/*" ``` ```typescript export default defineConfig({ dts: { entry: ["src/public/**/*.ts", "!src/public/dev/**/*"], }, }); ``` -------------------------------- ### Create New Project with Bunup CLI Source: https://bunup.dev/docs/scaffold-with-bunup This command initiates the project creation process using the Bunup CLI. It prompts the user with questions to configure the new project, offering 'Minimal' and 'Full' setup variants. ```sh bunx @bunup/cli@latest create ``` -------------------------------- ### Configure Build Report Source: https://bunup.dev/docs/guide/options Configures the build report to display file sizes and compression statistics. Supports enabling reporting for different compression algorithms like brotli. ```sh # Enable brotli compression reporting (gzip is enabled by default) bunup --report.brotli ``` -------------------------------- ### Enable tsgo for Faster Declaration Generation (CLI & Config) Source: https://bunup.dev/docs/guide/typescript-declarations This snippet shows how to enable tsgo, TypeScript's experimental native compiler, for faster declaration generation. It requires installing the `@typescript/native-preview` package and can be configured via CLI flags or the `bunup.config.ts` file. `tsgo` only works when `inferTypes` is enabled. ```sh bun add --dev @typescript/native-preview bunup --dts.infer-types --dts.tsgo ``` ```typescript export default defineConfig({ dts: { inferTypes: true, tsgo: true, }, }); ``` -------------------------------- ### Multiple Bunup Configurations Source: https://bunup.dev/docs/guide/config-file Exports an array of configurations from `bunup.config.ts` to build for multiple environments or formats in a single run. Each configuration requires a `name` property. ```typescript export default defineConfig([ { entry: "src/index.ts", name: "node", format: "esm", target: "node", }, { entry: "src/browser.ts", name: "browser", format: ["esm", "iife"], target: "browser", outDir: "dist/browser", }, ]); ``` ```typescript export default defineConfig([ { entry: "src/index.ts", name: "main", format: ["esm", "cjs"], }, { entry: "src/cli.ts", name: "cli", format: ["esm"], }, { entry: "src/browser.ts", name: "browser", format: ["esm", "iife"], outDir: "dist/browser", }, ]); ``` -------------------------------- ### Basic Copy Plugin Configuration Source: https://bunup.dev/docs/builtin-plugins/copy Demonstrates the basic setup of the copy plugin within a bunup.config.ts file. It shows how to import the plugin and include it in the defineConfig configuration to copy specified files and directories. ```typescript import { defineConfig } from "bunup"; import { copy } from "bunup/plugins"; export default defineConfig({ plugins: [copy(["README.md", "assets/**/*"])], }); ``` -------------------------------- ### Create a TypeScript Function Source: https://bunup.dev/index Defines a simple TypeScript function 'greet' that takes a name as a string and returns a greeting string. This serves as an example of a basic TypeScript module. ```typescript export function greet(name: string): string { return `Hello, ${name}`; } ``` -------------------------------- ### Customizing Output Directory for Executable Source: https://bunup.dev/docs/advanced/compile Customize the output directory for compiled executables using the 'outDir' option. This example changes the default 'bin/' to 'dist/'. ```typescript export default defineConfig({ entry: "src/cli.ts", compile: true, outDir: "dist", // Output to dist/ instead of bin/ }); ``` -------------------------------- ### Install Tailwind CSS Plugin Source: https://bunup.dev/docs/builtin-plugins/tailwindcss Installs the official Bunup plugin for Tailwind CSS as a development dependency using npm or yarn. ```bash bun add --dev @bunup/plugin-tailwindcss ``` -------------------------------- ### TypeScript: Minified vs Original Declaration Example Source: https://bunup.dev/docs/guide/typescript-declarations Compares an original TypeScript declaration file with its minified version. Minification shortens type names and removes comments, significantly reducing file size while preserving the public API. ```typescript // Original: type DeepPartial = { [P in keyof T]?: DeepPartial }; interface Response { data: T; error?: string; meta?: Record; } declare function fetchData(url: string, options?: RequestInit): Promise>; export { fetchData, Response, DeepPartial }; // Minified: type e = { [P in keyof T]?: e }; interface t { data: T; error?: string; meta?: Record; } declare function n(url: string, options?: RequestInit): Promise>; export { n as fetchData, t as Response, e as DeepPartial }; ``` -------------------------------- ### Initial Publish for Monorepo Packages Source: https://bunup.dev/docs/scaffold-with-bunup For monorepo setups, this command is used to perform the initial publish of individual packages. It requires navigating to the specific package directory before running the publish command with the '--access public' flag. ```sh cd packages/my-first-package bun publish --access public ``` -------------------------------- ### Define Single Entry Point (CLI & Config) Source: https://bunup.dev/docs/guide/options Specify a single source file as the entry point for your Bunup build. This can be done directly via the CLI or within the bunup.config.ts file. ```sh bunup src/index.ts ``` ```typescript export default defineConfig({ entry: "src/index.ts", }); ``` -------------------------------- ### Cross-Compiling to a Specific Target Platform Source: https://bunup.dev/docs/advanced/compile Compile your code for a specific target platform using the 'compile' option with a target string. This example targets Linux x64. ```typescript export default defineConfig({ entry: "src/cli.ts", compile: "bun-linux-x64", // Cross-compile for Linux }); ``` -------------------------------- ### Import CSS in TypeScript Source: https://bunup.dev/docs/guide/css Demonstrates how to import CSS files directly into TypeScript for automatic bundling by Bunup. This is the simplest way to include styles in your project. ```typescript import "./styles.css"; import { Button } from "./components/button"; export { Button }; ``` -------------------------------- ### Use Glob Patterns for Entry Points (CLI & Config) Source: https://bunup.dev/docs/guide/options Leverage glob patterns to dynamically include multiple files for bundling, with support for exclusions. This is configurable via the CLI or the bunup.config.ts file. ```sh bunup 'src/**/*.ts' '!src/**/*.test.ts' ``` ```typescript export default defineConfig({ entry: ["src/**/*.ts", "!src/**/*.test.ts", "!src/internal/**/*.ts"], }); ``` -------------------------------- ### Basic CSS Stylesheet Source: https://bunup.dev/docs/guide/css A simple CSS file defining styles for a button element. Bunup will bundle this into the main CSS output. ```css .button { background-color: #007bff; color: white; padding: 8px 16px; border: none; border-radius: 4px; } ``` -------------------------------- ### Using Native Bun Plugins with Bunup Source: https://bunup.dev/docs/guide/plugins Demonstrates how to incorporate native Bun plugins directly into your Bunup configuration. These plugins are passed to Bun's underlying bundler to modify its behavior. ```typescript import { defineConfig } from "bunup"; import type { BunPlugin } from "bun"; const myBunPlugin: BunPlugin = { name: "my-plugin", setup(build) { // Bun plugin setup }, }; export default defineConfig({ entry: "src/index.ts", plugins: [myBunPlugin], }); ``` -------------------------------- ### Configure onSuccess Command Execution Source: https://bunup.dev/docs/guide/options Allows specifying a command to run after a successful build, with options for working directory, environment variables, timeout, and kill signal. This is useful for restarting servers or running post-build scripts. Available only in the configuration file. ```typescript export default defineConfig({ onSuccess: { cmd: "bun run ./scripts/server.ts", options: { cwd: "./app", env: { ...process.env, FOO: "bar" }, timeout: 30000, // 30 seconds killSignal: "SIGKILL", }, }, }); ``` -------------------------------- ### Add Banner and Footer to Bundles Source: https://bunup.dev/docs/guide/options Adds custom text to the beginning (banner) and end (footer) of generated bundle files. Useful for directives like 'use client' or license information. ```sh bunup --banner 'use client' --footer '// built with love in SF' ``` ```typescript export default defineConfig({ // Add text to the beginning of bundle files banner: '"use client";', // Add text to the end of bundle files footer: "// built with love in SF", }); ``` -------------------------------- ### Bunup Package with Multiple Build Configurations Source: https://bunup.dev/docs/guide/workspaces Demonstrates configuring a single package ('web') with multiple, distinct build configurations. Each configuration requires a `name` property for identification and can specify different entry points, formats, and targets. ```typescript export default defineWorkspace([ { name: "web", root: "packages/web", config: [ { entry: "src/index.ts", name: "node", format: "esm", target: "node", }, { entry: "src/browser.ts", name: "browser", format: ["esm", "iife"], target: "browser", outDir: "dist/browser", }, ], }, ]); ``` -------------------------------- ### Compiled Scoped CSS Output Source: https://bunup.dev/docs/builtin-plugins/tailwindcss Example of the compiled CSS output in dist/index.css, showing scoped Tailwind CSS classes with the specified prefix (yuku). ```css @layer theme { :root, :host { --yuku-color-blue-500: oklch(62.3% 0.214 259.815); --yuku-color-blue-600: oklch(54.6% 0.245 262.881); --yuku-color-white: #fff; --yuku-spacing: 0.25rem; --yuku-radius-md: 0.375rem; } } @layer base, components; @layer utilities { .yuku\:rounded-md { border-radius: var(--yuku-radius-md); } .yuku\:bg-blue-500 { background-color: var(--yuku-color-blue-500); } .yuku\:px-4 { padding-inline: calc(var(--yuku-spacing) * 4); } .yuku\:py-2 { padding-block: calc(var(--yuku-spacing) * 2); } .yuku\:text-white { color: var(--yuku-color-white); } @media (hover: hover) { .yuku\:hover\:bg-blue-600:hover { background-color: var(--yuku-color-blue-600); } } } ``` -------------------------------- ### Configuring Copy Plugin Options: excludeDotfiles Source: https://bunup.dev/docs/builtin-plugins/copy Demonstrates how to use the `excludeDotfiles` option with the copy plugin. When set to `true`, files starting with a dot (e.g., `.env`) will be excluded from the copy operation. ```typescript copy("assets/**/*").with({ excludeDotfiles: true, }); ``` -------------------------------- ### Define Multiple Entry Points (CLI & Config) Source: https://bunup.dev/docs/guide/options Configure Bunup to bundle multiple source files. Supports specifying files directly on the CLI, using the --entry flag, or providing an array of paths in the configuration file. ```sh bunup src/index.ts src/cli.ts ``` ```sh bunup --entry src/index.ts --entry src/cli.ts # or using alias bunup -e src/index.ts -e src/cli.ts ``` ```typescript export default defineConfig({ entry: ["src/index.ts", "src/cli.ts"], }); ``` -------------------------------- ### Handle Environment Variables (CLI & Config) Source: https://bunup.dev/docs/guide/options Manage how environment variables are handled in your bundled code. Options include inlining all variables, disabling inlining, inlining variables with a specific prefix, or explicitly defining variables. ```sh # Inline all environment variables available at build time FOO=bar API_KEY=secret bunup --env inline # Disable all environment variable inlining bunup --env disable # Only inline environment variables with a specific prefix (e.g., PUBLIC_) PUBLIC_URL=https://example.com bunup --env PUBLIC_* # Explicitly provide specific environment variables bunup --env.NODE_ENV="production" --env.API_URL="https://api.example.com" ``` ```typescript export default defineConfig({ // Inline all available environment variables at build time env: "inline", // Or disable inlining entirely (keep process.env.FOO in the output) // env: "disable", // Or inline only variables that start with a specific prefix // env: "PUBLIC_*", // Or explicitly provide specific environment variables // These will replace both process.env.FOO and import.meta.env.FOO // env: { // API_URL: "https://api.example.com", // DEBUG: "false", // }, }); ``` -------------------------------- ### Configure Source Base Directory (CLI & Config) Source: https://bunup.dev/docs/guide/options Define the base directory for entry points to control the output file structure. This option helps preserve the source directory hierarchy in the build output, especially with multiple entry points. ```bash bunup --source-base ./src ``` ```typescript export default defineConfig({ sourceBase: "./src", }); ``` -------------------------------- ### Define Post-build Operations in Bunup Source: https://bunup.dev/docs/guide/options Configure actions to run after a successful build using the 'onSuccess' option. Supports function callbacks for custom logic or simple shell commands. ```typescript export default defineConfig({ onSuccess: (options) => { console.log("Build completed!"); const server = startDevServer(); // Optional: return a cleanup function for watch mode return () => server.close(); }, }); ``` ```sh bunup --on-success "bun run ./scripts/server.ts" ``` ```typescript export default defineConfig({ onSuccess: "bun run ./scripts/server.ts", }); ``` -------------------------------- ### Basic Bunup Workspace with Two Packages Source: https://bunup.dev/docs/guide/workspaces A minimal Bunup workspace configuration defining two packages, 'core' and 'utils'. The 'core' package specifies custom build formats, while 'utils' utilizes default settings for its build. ```typescript import { defineWorkspace } from "bunup"; export default defineWorkspace([ { name: "core", root: "packages/core", config: { // Bunup finds 'src/index.ts' by default // Or specify exactly which files to build // entry: ["src/index.ts", "src/plugins.ts"], format: ["esm", "cjs"], }, }, { name: "utils", root: "packages/utils", // Uses default entry points // Uses default format: esm // Generates .d.ts declaration files }, ]); ``` -------------------------------- ### Generated TypeScript Definitions for CSS Modules Source: https://bunup.dev/docs/guide/css Illustrates the TypeScript definition file automatically generated by Bunup for CSS Modules. This provides type safety and autocompletion for CSS class names. ```ts declare const classes: { readonly primary: string; readonly secondary: string; }; export default classes; ``` -------------------------------- ### Configure Loader Options (CLI & Config) Source: https://bunup.dev/docs/guide/options Customize how different file types are loaded and processed by bunup. You can map file extensions to built-in loader names. This is useful for handling non-JavaScript assets. ```bash bunup --loader.".css"=text --loader.".txt"=file ``` ```typescript export default defineConfig({ loader: { ".css": "text", ".txt": "file", }, }); ``` -------------------------------- ### Custom Bunup Configuration Path via CLI Source: https://bunup.dev/docs/guide/config-file Specifies a custom path for the Bunup configuration file using the `--config` or `-c` CLI option. This allows for non-standard file names or locations. ```bash bunup --config ./configs/custom.bunup.config.ts # or using alias bunup -c ./configs/custom.bunup.config.ts ``` -------------------------------- ### Import and Use React Component Library Source: https://bunup.dev/docs/recipes/react Example of how a consumer application would import and use components from a published React component library. It shows importing styles and the Button component. ```tsx import "my-component-library/styles.css"; import { Button } from "my-component-library"; function App() { return ; } ``` -------------------------------- ### Filtering Bunup Configurations via CLI Source: https://bunup.dev/docs/guide/config-file Uses the `--filter` CLI option to build only specific configurations by name when multiple configurations are defined in `bunup.config.ts`. This is useful for testing individual builds. ```bash # Single bunup --filter main # Multiple bunup --filter main,browser ``` -------------------------------- ### Bundle All Dependencies (CLI & Config) Source: https://bunup.dev/docs/guide/options Force all project dependencies to be included directly within the bundle, overriding the default behavior. Configure this using the --packages bundle flag on the CLI or the packages option in bunup.config.ts. ```sh bunup --packages bundle ``` ```typescript export default defineConfig({ packages: "bundle", }); ``` -------------------------------- ### Composing Styles from Another CSS Module Source: https://bunup.dev/docs/guide/css Shows how to compose styles from a different CSS module file using the `composes` property, specifying the relative path to the source file. This enables sharing styles across modules. ```css .primary { composes: base from "../shared.module.css"; background-color: #007bff; color: white; } ``` -------------------------------- ### Configure Separate CSS Entry Points Source: https://bunup.dev/docs/guide/css Configures Bunup to treat specific CSS files as entry points, generating separate CSS files in the build output instead of a single bundled file. This is useful for managing distinct style sets. ```typescript import { defineConfig } from 'bunup'; export default defineConfig({ entry: [ 'src/index.ts', 'src/components/button.css' 'src/components/alert.css' ], }); ``` -------------------------------- ### Define Global Build-Time Constants Source: https://bunup.dev/docs/guide/options Allows defining global constants that are replaced during the build process. Useful for feature flags, version numbers, or environment-specific configurations. Values are treated as strings and should be quoted if necessary. ```sh bunup --define.PACKAGE_VERSION='"1.0.0"' --define.DEBUG='false' ``` ```typescript export default defineConfig({ define: { PACKAGE_VERSION: '"1.0.0"', DEBUG: "false", }, }); ``` -------------------------------- ### Define Workspace Configuration with Bunup Source: https://bunup.dev/docs/guide/workspaces This snippet demonstrates how to initialize a Bunup workspace configuration using the `defineWorkspace` function. It serves as the entry point for defining your monorepo's package structure and build settings. ```typescript import { defineWorkspace } from "bunup"; export default defineWorkspace([ // Package configurations go here ]); ``` -------------------------------- ### Bunup Development Workflow Commands Source: https://bunup.dev/docs/scaffold-with-bunup A set of commands to manage the development lifecycle of a Bunup-scaffolded project. These include starting the development server, running tests, linting, fixing code style issues, type checking, and building the production bundle. ```sh bun run dev bun run test bun run lint bun run lint:fix bun run type-check bun run build ``` -------------------------------- ### Initial Publish for Single Package Source: https://bunup.dev/docs/scaffold-with-bunup For single-package projects, this command is used to perform the initial publish. It should be run from the root directory of the project with the '--access public' flag. ```sh bun publish --access public ``` -------------------------------- ### Configuring Copy Plugin Options: followSymlinks Source: https://bunup.dev/docs/builtin-plugins/copy Shows how to configure the `followSymlinks` option for the copy plugin. Setting this to `true` ensures that symbolic links are followed when copying files and directories. ```typescript copy("assets/**/*").with({ followSymlinks: true, }); ``` -------------------------------- ### Using Glob Patterns for File Selection Source: https://bunup.dev/docs/builtin-plugins/copy Shows how to leverage glob patterns with the copy plugin to select files and directories based on flexible matching rules. This includes recursive copying and combining multiple patterns. ```typescript // Copy all markdown files recursively copy("**/*.md"); // Copy all files in assets directory copy("assets/**/*"); // Copy with multiple patterns copy([ "assets/**/*", // All files in assets "docs/**/*.md", // Markdown files in docs "src/**/*.css", // CSS files in src ]); ``` -------------------------------- ### Copying Files and Directories Source: https://bunup.dev/docs/builtin-plugins/copy Illustrates various ways to use the copy plugin for handling files and directories. This includes copying single files, multiple files, entire directories, and renaming them during the copy process. ```typescript // Copy single file copy("README.md"); // Copy multiple specific files copy(["README.md", "LICENSE", "CHANGELOG.md"]); // Copy and rename a file copy("README.md").to("documentation.md"); // Copy entire directory as is (preserves structure) copy("assets"); // → dist/assets/ // Copy and rename directory copy("assets").to("static"); // → dist/static/ // Copy multiple directories copy(["assets", "public", "docs"]); ``` -------------------------------- ### Disable CSS Module Type Generation (CLI) Source: https://bunup.dev/docs/guide/css Disables the automatic generation of TypeScript definition files for CSS Modules using the Bunup CLI command. ```sh bunup --no-css.typed-modules ``` -------------------------------- ### Composing Styles within a CSS Module Source: https://bunup.dev/docs/guide/css Demonstrates how to reuse styles within the same CSS module using the `composes` property. This promotes DRY principles in CSS. ```css .base { padding: 8px 16px; border: none; border-radius: 4px; cursor: pointer; } .primary { composes: base; background-color: #007bff; color: white; } .secondary { composes: base; background-color: transparent; color: #007bff; border: 1px solid #007bff; } ``` -------------------------------- ### Disable CSS Module Type Generation (Config) Source: https://bunup.dev/docs/guide/css Disables the automatic generation of TypeScript definition files for CSS Modules by setting `typedModules: false` in the `bunup.config.ts` file. ```typescript import { defineConfig } from "bunup"; export default defineConfig({ css: { typedModules: false, }, }); ``` -------------------------------- ### Consumer Importing Components with Injected CSS Source: https://bunup.dev/docs/builtin-plugins/tailwindcss Demonstrates how consumers import components when the CSS is injected at runtime. No explicit CSS import is required. ```javascript import { Button } from "your-package";