A lightweight module bundler wrapping Bun.build
Designed to bundle client-side code for the browser. Capable of:
- Building multiple entrypoints (including a service worker)
- Copying folders and files from the source directory to the output directory
- All the other native features of Bun.build
To use, install the bun-bundle
package, then import BunBundle
and call its build
function with the desired config options.
bun add -D bun-bundle
To see an example of bun-bundle in action, check out the fullstack-bun project template.
You can now specify an HTML file as an entrypoint. The build process will also build any scripts it references (including .jsx
, .ts
, and .tsx
files), and will rename them inline to match their respective build output filenames. Any CSS files it references will also be copied to the output directory.
import { BunBundle, BunBundleBuildConfig, BunBundleBuildOutput } from "bun-bundle";
const IS_PROD = process.env.NODE_ENV === "production";
const buildConfig: BunBundleBuildConfig = {
root: "./src/client",
outdir: "./public",
entrypoints: ["index.html"],
swEntrypoint: "sw.ts",
copyFolders: ["assets"],
copyFiles: ["browserconfig.xml", "favicon.ico", "manifest.json"],
define: { "Bun.env.IS_PROD": `"${IS_PROD}"` },
sourcemap: IS_PROD ? "none" : "linked",
naming: {
entry: "[dir]/[name]~[hash].[ext]",
asset: "[dir]/[name]~[hash].[ext]"
},
minify: IS_PROD,
suppressLog: true
};
const { results, buildTime }: BunBundleBuildOutput = await BunBundle.build(buildConfig);
console.log(`Build completed in ${IS_PROD ? "production" : "development"} mode in ${output.buildTime}ms`);
console.log("Build results:\n", results);
(required) The path of the source directory to build from, relative to the project root. This is typically where your client-side source code lives.
(required) The path of the output directory to build to, relative to the project root. This is typically a public directory that will be served by your web server.
(required) An array of strings representing the main entrypoint file(s) of the build process. Each file must be of type .html
, .js
, .ts
, .jsx
, or .tsx
, and its path must be relative to the root
.
If an HTML file is specified, the build process will also build any scripts it references (including .jsx
, .ts
, and .tsx
files), and will rename them inline to match their respective build output filenames. Any CSS files it references will also be copied to the outdir
.
For example, a script tag like this...
<script type="text/javascript" src="./main.tsx" defer></script>
...will have the value of its src
attribute automatically updated in the outdir
to match the build output filename:
<script type="text/javascript" src="./main~4rvgxggr.js" defer></script>
(optional) The service worker entrypoint file (.js
or .ts
). If specified, its path must be relative to the root
.
(optional) An array of folder names to copy recursively from the root
to the outdir
.
(optional) An array of file names to copy from the root
to the outdir
.
(optional) An array of Bun.build plugins to use. By default, only the bun-copy-plugin will be used to copy the folders and files specified via the copyFolders
and copyFiles
options.
(optional) A boolean used to suppress the log output of the build process. If unspecified, defaults to false
.
(optional) A boolean used to clear the output directory at the start of the build. If unspecified, defaults to true
.
The BunBundle.build
function accepts any of the native Bun.build config options. The select native options listed above are explicitly mentioned either because they are required, or because they are optional but are given special default values. Any native option that is not listed above will simply be passed through to the Bun.build
function with its normal native default value.
An object containing the Bun.build outputs.
A number indicating the amount of time in milliseconds the build process took to complete.