NfNitLoop / deno-embedder

Dev tools for embedding static files into Deno as code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support async `import()`s

NfNitLoop opened this issue · comments

The current implementation of Deno Embedder creates a dir.ts file which imports all files for an embed directory.

This means that if you have a large set of embeds, you pay the cost of loading all of those embeds every time you start your application, even if you're not going to use them all.

It would be great if we could create a files list like:

const files = {
  "Smiley.svg": () => import("./Smiley.svg_.ts"),
  "index.html": () => import("./index.html_.ts"),
} as const

That way, we'd only need to import one of the embedded files if it's requested.

Blockers

I thought this new feature would allow using dynamic imports:
https://deno.com/blog/v1.32#deno-compile-support-for-web-workers-and-dynamic-import

However, the current implementation seems limited to binaries built with deno compile.

  • deno compile has access to all dynamic imports without requiring additional permissions.
  • deno install does not install dynamic imports, and they must be imported from network/disk, by granting extra permissions to do so.
  • deno run also requires extra permissions to load dynamic imports. (This impacts "dev mode".)

My goal for Deno Embedder is for it to "just work" for all these cases, so I'm not going to bother supporting dynamic imports until Deno supports the above cases.

Upstream tracking issues:

Woo, upstream blockers have been cleared. One day I may get around to implementing dynamic imports. 😆

Just wanted to mention that I'm interested in this feature.

My use-case is embedding static websites in val.town (and more globally distributing static websites through jsr).

Val.town use a serverless architecture, and loading all embeds each time a request might be a blocker for non-trivial websites.

ex: https://www.val.town/v/pomdtr/heynote

@pomdtr Thanks for letting me know you're interested! That definitely bumps this up in priority for me. 😊

loading all embeds each time a request might be a blocker for non-trivial websites

FYI: if your JS process is long-running, it doesn't load all embeds with every request. Once an embed is loaded, it stays in-memory. (It's just an import.) The main thing this fixes is that, currently, it does an import/load for all embeds at process/server startup.

If you want to chat about your use case, feel free to ping me on the Deno Discord. My username there is the same as here.