Module loading
Hey,
I am just testing the deno_core. I know, that the deno runtime with the command line supports native executing of ts files and also loading of dependencies by an url and also npm depependcies out of the box. As I evaluated it, the deno_core does not support this by default, so how to easily implement executing ts files and loading of depencies by url? I already found, that I can set a custom
module_loader
and a custom_module_evaluation_cb
for the JsRuntime
, but I think there is an easier way to do this instead of integrating it by myself.
Thanks for you help2 Replies
Hello! You're correct that
deno_core
doesn't support TypeScript execution and dependency loading by URL out of the box. It provides a lower-level API that allows you to build these features on top of it. To execute TypeScript files and load dependencies by URL, you would typically need to implement a custom ModuleLoader
that can handle these tasks.
The documentation provides an example of how to create a custom ModuleLoader
that can resolve and load TypeScript modules. Here's a simplified version of the process:
1. Implement the ModuleLoader
trait for your custom loader, which includes the resolve
and load
functions.
2. Use deno_core::resolve_import
to resolve module specifiers in the resolve
function.
3. In the load
function, determine the file's media type and whether it needs transpiling.
4. Read the file into a string and transpile it if necessary using deno_ast
.
5. Return the transpiled code as a ModuleSource
.
Here's a snippet from the documentation that outlines the structure of a custom ModuleLoader
:
And the load
function would look something like this:
In the load
function, you would use deno_ast
toparse and transpile TypeScript to JavaScript. The
deno_ast
crate is part of the Deno ecosystem and can be added to your dependencies to handle the parsing and transpiling of TypeScript.
While this does require some manual setup, it allows you to have fine-grained control over how modules are loaded and processed. If you're looking for a more out-of-the-box solution, you might consider using the full Deno runtime for executing TypeScript and managing dependencies, as it already has these capabilities built-in.
For more detailed guidance and code examples, you can refer to the Deno blog post titled "Roll your own JavaScript runtime, pt. 2" which is available in the documentation. 🦕