run icon indicating copy to clipboard operation
run copied to clipboard

Assets from Server-Only Marko Components Are Dropped

Open AndresBarreto-code opened this issue 10 months ago • 1 comments

Problem

When importing assets (e.g. images via vite-imagetools) inside Marko components used only in the server build, those assets are not emitted to dist/public/assets.

Even though emitFile() is called (confirmed via logging), the assets are silently dropped unless the component explicity become a client-component.

Debugging

Reproduced on: https://github.com/AndresBarreto-code/marko-with-vite-imagetools

npm install
npm run build

OR

npm install
npm run preview

You will see no serve-image is being built in dist/public/assets.

Why It Matters

  • Assets used in SSR components vanish from production builds
  • Devs are forced to restructure components or fake client usage
  • It breaks expected behavior when using Vite plugins like vite-imagetools

Proposed Fix

Update @marko/run to retain assets from the SSR build:

  • During SSR build, track emitted assets via emitFile()
  • In client build, re-emit those assets to ensure they're preserved

AndresBarreto-code avatar Apr 10 '25 02:04 AndresBarreto-code

@AndresBarreto-code thanks for the callout.

For some context, the way that assets like this are propagated to the client build is through (essentially) a special compiler option regexp

Anything that matches that regexp imported from a marko file will automatically be carried over to the client compilation. (The server compilation is typically configured to avoid emitting any assets to avoid duplicate work).

That said I think we could update the regexp to ignore the search params.

DylanPiercey avatar Apr 10 '25 03:04 DylanPiercey

Hey @DylanPiercey I was going to look back at this, but it seems it was already fixed. Could you please confirm with me? If so, thank you very much.

AndresBarreto-code avatar May 26 '25 06:05 AndresBarreto-code

@AndresBarreto-code yes this is fixed, but I forgot to close this issue!

The actual issue was that the client asset propagation regex was not accounting for assets with query params in them (like with vite imagetools).

DylanPiercey avatar May 26 '25 12:05 DylanPiercey

@DylanPiercey thank you very much! Curious to learn more about Marko, was this https://github.com/marko-js/run/pull/117 the fix? It does not sound like 🤔 otherwise, this one https://github.com/marko-js/run/pull/129?

AndresBarreto-code avatar May 26 '25 22:05 AndresBarreto-code

@AndresBarreto-code what I said was the fix was wrong 🤦. I think this is what was the fix https://github.com/marko-js/vite/pull/162

DylanPiercey avatar May 26 '25 22:05 DylanPiercey