Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundling assets from dependencies #710

Open
azriel91 opened this issue Feb 10, 2024 · 8 comments
Open

Bundling assets from dependencies #710

azriel91 opened this issue Feb 10, 2024 · 8 comments
Labels
enhancement New feature or request help wanted Extra attention is needed needs design This item needs design work

Comments

@azriel91
Copy link

Heya, is it possible to bundle assets from dependencies?

e.g. web_app depends on web_components, and web_components defines its own assets.

My use case is shipping stylesheets in a components library crate, so that consumers who are building their own application don't have to make a copy of the stylesheet and maintain it.

@ctron
Copy link
Collaborator

ctron commented Feb 12, 2024

No that I am aware of. But yes, that would be a nice addition.

@ealmloff
Copy link

ealmloff commented Mar 21, 2024

This is an issue we were facing with dioxus. We are working on developing a library called Manganis that supports collecting and optimizing assets across dependancies. We are using it in the dioxus-cli, but it is built to work with any CLI. It could be used for cross dependency assets in trunk in the future

We implement a similar approach to what is described in #9

@ctron
Copy link
Collaborator

ctron commented Mar 21, 2024

We implement a similar approach to what is described in #9

I think that describes a bunch of ideas :) … What exactly do you propose?

@ealmloff
Copy link

I think that describes a bunch of ideas :) … What exactly do you propose?

It implements something similar to option 0 in the original comment. You can link to a local asset with a macro which expands to the server URL (with a configurable base path).

// RUSTACEAN_URL is a url in the form `/base_path/asset_name_then_asset_hash`
const RUSTACEAN_URL: &str = manganis::mg!(file("https://rustacean.net/assets/rustacean-flat-happy.png"));

You can also use a builder syntax (with autocomplete) to modify the asset at compile time:

// ImageAsset implements Display to get the URL with additional information like a low quality image preview if that feature is enabled
pub const AVIF_ASSET: manganis::ImageAsset = manganis::mg!(image("./rustacean-flat-gesture.png")
    .size(52, 52)
    .format(ImageType::Avif));

Any CLIs that support Manganis (currently just the dioxus-cli) can use the manganis-cli-support library to collect, automatically optimize and copy those assets before the application is served.

@ctron
Copy link
Collaborator

ctron commented Mar 22, 2024

so that would be part of the frontend code and compile when trunk would call cargo build, right? I assume that will create a tree of assets at some location, which then would need to be scooped up the bundler, e.g. trunk?

If that's the case, then I guess using copy-dir, would already work today: https://trunkrs.dev/assets/#copy-dir

I agree that it would be cool to auto-detect the use of manganis. Just trying to understand what would be required to do that.

@ctron ctron added enhancement New feature or request help wanted Extra attention is needed needs design This item needs design work labels Mar 22, 2024
@ealmloff
Copy link

so that would be part of the frontend code and compile when trunk would call cargo build, right?

Yes, manganis could be in the main package you are building or in any dependency that needs assets like a component library

I assume that will create a tree of assets at some location, which then would need to be scooped up the bundler, e.g. trunk?

The macro will just create a link to an asset. It doesn't copy or optimize the assets directly because macros are built in debug mode (by default) and a library doesn't know the location of the binary it is being used for

If that's the case, then I guess using copy-dir, would already work today: https://trunkrs.dev/assets/#copy-dir

I agree that it would be cool to auto-detect the use of manganis. Just trying to understand what would be required to do that.

Any CLI that supports manganis needs to:

  1. save a config
  2. Hold a guard when you build the project. This guard sets some environment variables that the macro reads
  3. Load the asset manifest and copy the assets to the final location

Note that manganis is still in alpha and some of these APIs will change in the future (also open to any suggestions). I just found this issue and thought it might be worth reaching out to see if manganis would be useful for trunk.

@ctron
Copy link
Collaborator

ctron commented Apr 4, 2024

I think it would be worth exploring this idea. I also think there could be multiple levels of integration. Maybe it makes sense forking off an issue working towards this. Just to be clear, I am happy to help, but I won't have time to do all the work.

@ctron
Copy link
Collaborator

ctron commented Apr 4, 2024

Created a new issue: #759

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed needs design This item needs design work
Projects
None yet
Development

No branches or pull requests

3 participants