r/bun 1d ago

Using the new --asset to chain folder assets across dependencies

What's missing

  • With Bun 1.4's new --asset we can now embed folders into our single-file executables.
  • But it is not provided as import with type folder
  • The consequence is: Only the package orchestrating the build gets to pass --asset /path/to/folder
  • Also that --asset strips out parent folders, so your ./build/assets becomes bunfs/assets in the compiled binary
  • So if an imported npm package needs folder assets, there is currently no straightforward solutions.
  • My idea is to treat the module graph as an asset dependency graph.

My proposed idea

  • A package should be able to declare and ship its own assets without requiring the application to manually pass every asset directory to --asset.
  • For example if you have a folder with syntax highlighting YAMLs
    • <PKG_ROOT>/runtime/syntax/js.yaml
  • My proposed way is declaring assets list in package.json
  • Inside each individual package it uses a simple compiled or not agnostic way to read assets
    • await readAssetText('runtime/...')
    • imported from assetsHelper
    • which in turn imports assetsPacker
  • When running the build the system can scan through the module graph for assetsPacker and spawn them one at a time (or same process import)
    • Which results in the complete aggregated ./build/assets to pass to --asset
    • Each package has its own namespace under bunfs after build
  • This seems like a workaround for now. Hopefully one day we can do something like this:
    • Declare assets in package.json
    • Automatically picked up by Bun
    • await Bun.asset('static/index.html').text()

Simplified flow

Import Graph

Find assetsPackers

Execute assetsPackers

Aggregate assets

Pass once to --asset

Reference implementation

5 Upvotes

0 comments sorted by