Description
In ParcelJS, the JS import system is overloaded to allow users to "import" images, css, sass and the like into their JS, and then Parcel will intercept those non-JS imports and build/bundle/hash them. It would be awesome to do something similar for trunk.
option 0
Create a compile-time macro which will take a path to a resource, maybe a few config options as well. Something like trunk::include!("assets/my-image.png")
, or trunk::include!("my-component.css", Opt::Hash, Opt::Compress)
.
- first argument will be a path to the asset, verified at compile time.
- any values passed after the path parameter will be treated as pipeline configuration options for the asset.
- generate a manifest of all assets included during the cargo build of the Rust WASM app. Will use a std::sync::Once var to ensure the manifest is cleared at the beginning of each build (probably).
- after the cargo build is finished, trunk will spawn asset pipelines for each entry in the manifest and add them to the bundle.
- we could also do glob processing on these, something like
trunk::include!("src/**/*.css")
, to spawn pipelines for, and include, all css files under thesrc
dir. A similar pattern is already planned as part of Support additional asset types #3. Will probably support both.
A variant of the macro will be exported, say trunk::include_ref!(...)
, which will return a String value which will be the public url of the asset, hashed and all, once the trunk pipeline is complete. This will allow applications to include an image directly in their Rust source code and have a correct ref to the asset after hashing and all.
considerations
- the macros will need to work the same way even when
cargo build
is executed outside of the context of trunk. - however, when applications ship, and are being served over the network, they will need to be able to reference assets via their public URL (typically something like
/<asset>
or/static/<asset>
&c). - as such, the macros should default to
/
as the base path of the asset, but users will be able to set an env var to overwrite the default. When cargo is invoked by trunk during atrunk build
(which is how all of this is intended to be used), trunk will be able to set the env var based on the value of--public-url
to coordinate the values. - trunk should look for a manifest generated in the cargo output dir matching the debug/release mode.
option n
Definitely open to other design options here. Please comment and let me know if you've got ideas you would like to share.