Skip to content
GitHubXDiscord

Bundle

The Bundle resource uses esbuild to bundle JavaScript and TypeScript files into optimized output.

Bundle a TypeScript file into ESM format:

import { Bundle } from "alchemy/esbuild";
const bundle = await Bundle("handler", {
entryPoint: "src/handler.ts",
format: "esm",
});

Specify an output directory and additional build options:

const bundle = await Bundle("api", {
entryPoint: "src/api.ts",
outdir: ".build",
format: "esm",
platform: "node",
target: "node18",
minify: true,
sourcemap: true,
});

Exclude packages from the bundle:

const bundle = await Bundle("app", {
entryPoint: "src/app.ts",
format: "esm",
external: ["aws-sdk", "lodash"],
platform: "node",
});

Create a browser-compatible IIFE bundle:

const bundle = await Bundle("web", {
entryPoint: "src/main.ts",
outfile: "dist/bundle.js",
format: "iife",
platform: "browser",
minify: true,
sourcemap: "external",
});