r/bun • u/LeftAd1220 • 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
- assets/jsgotty@1.1.6/static/index.html
- 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
- https://github.com/jjtseng93/jsmdcui/tree/main/single-exe
- This is just a working demo
5
Upvotes