Dynamic import at compile time
I am trying to import files dynamically based on a glob pattern. These files are not imported correctly when I compile the application. It only works with a static string. Is there any way to make it work with the glob pattern? or is there a workaround?
Or do I just have to import the files using static strings
12 Replies
Can you show some example code you're trying to run?
This is the code I am using to import the files. The importer function gets called in the entrypoint the project
That looks correct and the way to do it. What's the exact problem you're running into?
If you want to use it with
deno compile
you can use --include
flag to include files into the compiled binary to be later dynamically importedWow I completely forgot to include the
--include
flag. That should do it, will test it when I'm back on my pc. Thank you!
hm, I've tried the following --include ./src/modules/**/*.ts
but it still isn't included. Any other suggestions?Try wrapping this in a quotes - otherwise it will be your shell that expands the glob
If it doesn't work I'd appreciate an issue - maybe you will need to specify
--include
flag multiple times once for each file
Which seems bad and we should fix itI have tried specifying each file separately but it still doesn't work.
I have opened an issue. If it needs more clarification please let me know
GitHub
Issues ยท denoland/deno
A modern runtime for JavaScript and TypeScript. Contribute to denoland/deno development by creating an account on GitHub.
Thanks!
The issue is closed as wont fix as it is basically impossible to implement which is fair. but then I might need some help with the include flag.
I have used it like this
--include "./src/modules/core/baseEvents.ts"
is this the correct way to do this? And how will this work code wise?
I'm just trying to get all the information needed to make it work ๐
Yeah that's correct way to do it
Eg if you are in
./src/main.ts
file you can import that included file as await import(import.meta.resolve("./modules/core/baseEvents.ts")
it works! thank you for your help ๐
@RogueMan Xmas came early this year: https://deno.com/blog/v2.1#embed-assets-files-in-deno-compile
Deno Blog
Deno 2.1: Wasm Imports and other enhancements
Deno 2.1 introduces first-class Wasm imports, built-in tracing with OpenTelemetry, faster
deno compile
, and improved package management.This looks very promising! Will definitely check it out.
I finished my compile script yesterday :cookie_deno:
Thank you (again ๐)