trunk icon indicating copy to clipboard operation
trunk copied to clipboard

Adding the ability to deferre asset bundling

Open mskorkowski opened this issue 2 months ago • 3 comments

Current situation

Currently Trunk's assets bundling is being run in parallel to the building stage.

Trunk's build process

This is a brief overview of Trunk's build process for the purpose of describing when hooks are executed. Please note that the exact ordering may change in the future to add new features.

Step 1 — Read and parse the HTML file. Step 2 — Produce a plan of all assets to be built. Step 3 — Build all assets in parallel. Step 4 — Finalize and write assets to staging directory. Step 5 — Write HTML to staging directory. Step 6 - Replace dist directory contents with staging directory contents.

The hook stages correspond to this as follows:

pre_build: takes place before step 1. build: takes place at the same time as step 3, executing in parallel with asset builds. post_build: takes place after step 5 and before step 6.

My issue

I have the resources which are being generated by the build script of the upstream crate to my application, which should be included into the assets. This makes it impossible to run trunk build because it expects the files assets to be reachable before build script is finished.

My proposal

Adding early_post_build stage. If somebody would use the deferred resources the new execution schema would look like

Step 1 — Read and parse the HTML file. Step 2 — Produce a plan of all assets to be built. Step 3 — Build all undeferred assets in parallel. Step 3+ — Build all deferred assets in parallel. Step 4 — Finalize and write assets to staging directory. Step 5 — Write HTML to staging directory. Step 6 - Replace dist directory contents with staging directory contents.

The hook stages would correspond now

pre_build: takes place before step 1. build: takes place at the same time as step 3, executing in parallel with asset builds. early_post_build: starts at step 3+ - 5 post_build: takes place after step 5 and before step 6.

To mark the resource as deferred in index.html it would be required to add the deferred boolean attribute

<link data-trunk rel="{type}" href="{path}" deffered  ..other options here.. />

mskorkowski avatar Oct 08 '25 06:10 mskorkowski

I just found #701 - Delay asset pipeline until rust build finished (I don't know when my eyes were).

This ticket could be considered a proposal for a fix without the job dependency graph.

Regardless of what will be a final design, I'm willing to spend time to see it through after the plan will be laid down.

mskorkowski avatar Oct 09 '25 11:10 mskorkowski

Hm. I am not sure I get this. You basically want a second build phase? Like run all build steps (in parallel). Then run next phase of build in parallel? Then continue?

ctron avatar Oct 09 '25 11:10 ctron

More like split the build phase into two.

First one runs cargo build and resources in parallel the same as now. Second does whatever was marked as deferred.


Edit: I've updated a wording to make it easier to understand.

mskorkowski avatar Oct 09 '25 11:10 mskorkowski