deno_graph icon indicating copy to clipboard operation
deno_graph copied to clipboard

I have a module, now what?

Open justinmchase opened this issue 3 years ago • 5 comments

Ok suppose I've got parseModule/createGraph resolving modules correctly. How can I now execute the module?

const m = await denoGraph.parseModule("http://example.ts", "export default { foo: true }")

const { foo } = // ??
console.log(foo)

justinmchase avatar Mar 29 '22 11:03 justinmchase

deno_graph is used for static analysis of the code, it doesn't have functionality to execute code. If you want to execute modules you need to use deno_core crate with ModuleLoader

bartlomieju avatar Mar 29 '22 11:03 bartlomieju

Does deno_core not have a typescript module? https://deno.land/x/deno_core

Or, how would one use deno_core.load?

I am looking for something such as:

const specifier = "http://example.foo"
const graph = await denoGraph.createGraph(specifier, {
  load: // custom graph loading, emits js...
})

const { foo } = Deno.load(graph)

justinmchase avatar Mar 29 '22 12:03 justinmchase

What I'm trying to do is to essentially import a file which is not typescript or javascript and transpile it on the fly. It seems like that ModuleLoader.load function could handle that but that I'm going to have to make a rust executable and essentially embed deno_core to do it...

I'm wondering are there plans to make this load api available in typescript? Are you open to a PR for the above? It looks like, without really digging into it, that implementing the above Deno.load to be a function which just imlements a ModuleLoader for a createGraph result wouldn't be too hard...

justinmchase avatar Mar 30 '22 02:03 justinmchase

What I'm trying to do is to essentially import a file which is not typescript or javascript and transpile it on the fly. It seems like that ModuleLoader.load function could handle that but that I'm going to have to make a rust executable and essentially embed deno_core to do it...

I see, there were proposals for JavaScript APIs for loader in deno but they didn't go anywhere: https://github.com/denoland/deno/issues/1739

I'm wondering are there plans to make this load api available in typescript?

Currently not, it's considered low priority at the moment.

Are you open to a PR for the above? It looks like, without really digging into it, that implementing the above Deno.load to be a function which just imlements a ModuleLoader for a createGraph result wouldn't be too hard...

I don't agree, this is going to be very hard to implement, but PRs are always welcome; maybe it will spark another conversation about custom loaders.

bartlomieju avatar Mar 30 '22 11:03 bartlomieju

Don't forget, there is a built in module loading functionality that loads code in its own context and is part of the web platform called web workers.

kitsonk avatar Mar 30 '22 19:03 kitsonk