RogueMan
RogueManโ€ข3w ago

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
bartlomieju
bartlomiejuโ€ข3w ago
Can you show some example code you're trying to run?
RogueMan
RogueManOPโ€ข3w ago
import { existsSync, expandGlob } from '@std/fs';

/**
* Dynamically import all files matching the glob pattern
* @param globPath The glob pattern to match files
*/
export async function importer(globPath: string) {
for await (const file of expandGlob(globPath)) {
if (file.isFile) {
await import(`file://${file.path}`);
}
}
}
import { existsSync, expandGlob } from '@std/fs';

/**
* Dynamically import all files matching the glob pattern
* @param globPath The glob pattern to match files
*/
export async function importer(globPath: string) {
for await (const file of expandGlob(globPath)) {
if (file.isFile) {
await import(`file://${file.path}`);
}
}
}
This is the code I am using to import the files. The importer function gets called in the entrypoint the project
bartlomieju
bartlomiejuโ€ข3w ago
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 imported
RogueMan
RogueManOPโ€ข3w ago
Wow 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?
bartlomieju
bartlomiejuโ€ข3w ago
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 it
RogueMan
RogueManOPโ€ข3w ago
I 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.
bartlomieju
bartlomiejuโ€ข3w ago
Thanks!
RogueMan
RogueManOPโ€ข3w ago
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 ๐Ÿ˜…
bartlomieju
bartlomiejuโ€ข3w ago
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")
RogueMan
RogueManOPโ€ข2w ago
it works! thank you for your help ๐Ÿ˜„
bartlomieju
bartlomiejuโ€ข2w ago
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.
RogueMan
RogueManOPโ€ข2w ago
This looks very promising! Will definitely check it out. I finished my compile script yesterday :cookie_deno: Thank you (again ๐Ÿ˜‰)