esm_entry_point / extension macro; ReferenceError

How do I use esm_entry_point properly? I defined my extension as:
extension!(
ext_name,
ops = [op_register_entrypoint],
esm_entry_point = "ext:ext_name/js_playground.js",
esm = [ dir "src/ext", "ext_name.js" ],
);
extension!(
ext_name,
ops = [op_register_entrypoint],
esm_entry_point = "ext:ext_name/js_playground.js",
esm = [ dir "src/ext", "ext_name.js" ],
);
But I'm now getting a referenceerror about ext_name being undefined - which is odd since I can see it expands to define the struct
15 Replies
Deno AI Helper
Deno AI Helper11mo ago
I'm sorry, but the documentation does not provide a clear answer to your question about using esm_entry_point properly. It would be helpful to have more context or see more of your code to understand the issue better. However, based on the code snippet you provided, it seems like you're trying to define an extension with a JavaScript entry point. Please ensure that the JavaScript file specified in esm_entry_point exists at the correct path and is properly exported. Also, make sure that the ext_name you're using in your Rust code matches the name of the JavaScript module you're trying to import. If you're still having issues, it might be helpful to debug by checking the paths and names in your code, or by trying to isolate the problem in a smaller piece of code. If the problem persists, please provide more details about your setup and the error message you're seeing.
baphomet_the_traveller
Yeah that's not super helpful @mmastrac Hate to bother you again, but you seem to be very familiar with the extensions module
mmastrac
mmastrac11mo ago
Hmm, that's odd. Are you able post the code? This looks very similar to what we see in the CLI extension:
deno_core::extension!(cli,
deps = [runtime],
ops = [op_npm_process_state],
esm_entry_point = "ext:cli/99_main.js",
esm = [
dir "js",
"40_testing.js",
"99_main.js"
],
deno_core::extension!(cli,
deps = [runtime],
ops = [op_npm_process_state],
esm_entry_point = "ext:cli/99_main.js",
esm = [
dir "js",
"40_testing.js",
"99_main.js"
],
baphomet_the_traveller
That's the example I used haha Gimme a sec I'll drop it in a gist https://gist.github.com/rscarson/72aa99b910e9f1427d15ce6563d0b888 Here's the relevant file I declare the extension on line 30, then use it to build the runtime with vec![js_playground::init_ops_and_esm()]; on 44 The rest of it is here: https://github.com/rscarson/js-playground (it's part of a larger project to migrate a platform I maintain to use deno on the backend) A lot of the samples I see use customize to set the specifiers to that format... what's the default specifier? I'm guessing that's the problem Oh wait a second - I'm a moron It's not a rust error - its a JS error The extension entrypoint never runs so the globalThis.js_playground object is never set
mmastrac
mmastrac10mo ago
Ah yeah, it looks like something needs to call load 🙂
baphomet_the_traveller
js_playground::init_ops_and_esm() isn't sufficient? where do I call load Is that a rust or JS thing
mmastrac
mmastrac10mo ago
It's a JS thing. You could probably just remove the load function wrapper and run that JS directly
baphomet_the_traveller
Ah yeah I see the wrapper it generates - how do I turn that off?
mmastrac
mmastrac10mo ago
What if you change js_playground.js to this?
export function load() {
globalThis.js_playground = {
'register_entrypoint': (f) => Deno.core.ops.op_register_entrypoint(f)
};
}

load()
export function load() {
globalThis.js_playground = {
'register_entrypoint': (f) => Deno.core.ops.op_register_entrypoint(f)
};
}

load()
baphomet_the_traveller
Oh I'm dumb - load is my function It's aliiiiiiive! Thanks for all the help! If I can make a small request though - it'd be nice if the extension! macro could have an option to not add pub It doesn't document the functions it builds, which breaks my lints I wrapped it in a mod block but that's a little clunky
mmastrac
mmastrac10mo ago
We would totally take a PR if you'd like to PR a fix to make the doc lints work. Ideally we would be able to drop a doc comment on the extension block itself.
baphomet_the_traveller
Sure! I'll work on one this aft So init_js_only is for CJS, init_ops_and_esm is for use with ES modules, and init_ops only preps the ops, bypassing any sources attached to the extension? Alrighty just making sure the CI and fmt checks still pass then I'll put my PR through
mmastrac
mmastrac10mo ago
It's a bit complicated, but those methods are used for the various snapshotting states we have while building Deno. If you don't use snapshots, I believe you can use init_ops_and_esm() everywhere
baphomet_the_traveller
GitHub
Add documentation options for extension! by rscarson · Pull Request...
Added docs: comma separated list of toplevel #[doc=...] tags to be applied to the extension's resulting struct to extension! to allow users to set docblocks for it, since the struct is always m...
baphomet_the_traveller
Also included a small example showing op2/extension!() Since there's few places in the codebase that builds and uses extensions in a concise way, I can remove it if that's out of line