aquakiloA
Denoβ€’2y agoβ€’
10 replies
aquakilo

How can I execute DOM-reliant javascript in deno_core?

I just want to execute some javascript in the background of my rust app, but I need a virtual dom to use some modules. I'm brand new to deno/rust, so apologies if I'm missing something obvious. But I don't understand if what I'm trying to do is even possible lol.

I'm trying to get linkedom to work with deno_core, but I get the error
error: ReferenceError: window is not defined
    at https://esm.sh/v135/node_perf_hooks.js:1:109

when I run the JsRuntime eventloop that imports linkedom.

This is my javascript file, following the example from https://docs.deno.com/runtime/manual/advanced/jsx_dom/linkedom/
import { parseHTML } from "https://esm.sh/linkedom@0.18.4";

function JSDOM(html) {
  return parseHTML(html);
}

const { document, window } = new JSDOM("<h1>hi</h1>");


this is my rust function to execute the js file
async fn run_js(file_path: &str, current_dir: &Path) -> Result<(), AnyError> {
    let mut js_runtime = deno_core::JsRuntime::new(deno_core::RuntimeOptions {
        module_loader: Some(Rc::new(SimpleModuleLoader)),
        ..Default::default()
    });

    let main_mod_specifier = deno_core::resolve_path(file_path, current_dir)?;
    let main_mod_id = js_runtime.load_main_es_module(&main_mod_specifier).await?;
    let main_result = js_runtime.mod_evaluate(main_mod_id);

    js_runtime
        .run_event_loop(PollEventLoopOptions {
            ..Default::default()
        })
        .await?;

    main_result.await
}
Was this page helpful?