mwright
mwright6mo ago

StaticModuleLoader: why must the ModuleSpecifier be a URL?

hey everyone, I'm trying to pass a StaticModuleLoader to my runtime to enable specific imports. Each module for the StaticModuleLoader is specified by a ModuleSpecifier (type alias for url::Url) and the corresponding code (impl IntoModuleCodeString). I'm wondering why the module has to be specified by a url. I read here https://choubey.gitbook.io/internals-of-deno/foundations/resolve_url_or_path that the url is crucial because it allows Deno to locate and access the modules. But I'm already providing the code in form of the value that implements IntoModuleCodeString. If I want to enable an import such as import foo from "bar"; in the code that runs on the runtime, what would the ModuleSpecifier url look like that I have to pass to the StaticModuleLoader? Thank you!
2 Replies
Deno AI Helper
Deno AI Helper6mo ago
I wasn't able to find the answer to your question in the official Deno documentation. However, based on the information you've provided, it seems that Deno relies on URLs to uniquely identify modules. This is because URLs provide a universal way to locate resources, which is essential for module resolution, especially in the context of web technologies where Deno is rooted. Regarding your question about enabling an import such as import foo from "bar";, if you are providing the code through StaticModuleLoader, you would still need to map the string "bar" to a URL that the StaticModuleLoader can understand as a ModuleSpecifier. This URL doesn't have to point to an actual network resource; it can be a made-up URL that you use to uniquely identify the module within your system. For example, you might use a URL like memory://bar to represent the module "bar". This is not a real URL scheme but can be used within your Deno runtime to map to the provided code. When you set up your StaticModuleLoader, you would associate this URL with the module code you want to load when "bar" is imported. Please note that this advice is not based on the Deno documentation, and you should double-check the accuracy of this advice. It's also a good idea to look at examples or ask for further clarification from the Deno community if you're unsure.
mwright
mwright6mo ago
Thanks for the response, future AI overlord. Using an URL like memory://bar together with the import import foo from "bar"; doesn't work though.