Skip to content

Bun.build: Add an option to explicitly embed files in compiled executables #29456

@recrsn

Description

@recrsn

What is the problem this feature would solve?

Currently, bundling a full-stack app into a single-file executable is cumbersome when you need to include a large number of assets. bun build --compile entrypoint dir/**/* only embeds non-JS/TS assets. The documentation sadly also acknowledges this.

There's no way to programmatically embed an arbitrary file at build time and have it appear in bun.embeddedFiles, even if you explicitly want to embed a .ts file as raw data.

What is the feature you are proposing to solve the problem?

Add an embeddedFiles option to Bun.build that accepts a list of file paths. Each file is embedded as raw data in the standalone executable and appears in Bun.embeddedFiles regardless of file type.

await Bun.build({
  entrypoints: ["./src/index.ts"],
  compile: {
    embeddedFiles: [
      "./src/templates/email.html",
      "./src/schema.ts",
      "./config/defaults.json",
    ],
  },
  outfile: "./my-app",
});

At runtime:

for (const file of Bun.embeddedFiles) {
  console.log(file.name);   // "src/templates/email.html", "src/schema.ts", ...
  const text = await file.text();
}

What alternatives have you considered?

Currently, I have been using a workaround to generate a virtual file using the files option which reads the directory and generates import file from 'path' with { type: 'file'}, and build my own array for emulating embeddedFiles but I'll very much love to avoid that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions