Doing some research into non-eval methods to dynamically execute JavaScript code in Node.js:
-
Zach Leatherman :11ty:replied to David Bushell last edited by
@dbushell ooh this is extremely helpful thank you!
and yes to Blob: https://nodejs.org/api/all.html#all_buffer_class-blob
-
David Bushellreplied to Zach Leatherman :11ty: last edited by
@zachleat there is also the `Function` constructor that can execute code, Vento uses it here: https://github.com/ventojs/vento/blob/main/src/environment.ts#L142
-
Zach Leatherman :11ty:replied to David Bushell last edited by
@dbushell huh—I’m curious how common this approach is, I had kinda filed in alongside `eval` (perhaps unfairly). Seems similar to Module#_compile or `import()` that can affect global scope
-
Mayankreplied to Zach Leatherman :11ty: last edited by
@zachleat i have used this incredibly cursed approach via `node --eval`
https://github.com/mayank99/ecsstatic/blob/main/package%2Fvite.ts#L299-L326 -
Zach Leatherman :11ty:replied to Mayank last edited by
@mayank oh wow, yeah. I’ve seen something similar recommended—requires the output be stringify-able, right?
Is there a way to use eval() with esm? · Issue #43280 · nodejs/node
Version v18.2.0 Platform Darwin C02F54NDMD6T 21.4.0 Darwin Kernel Version 21.4.0: Mon Feb 21 20:34:37 PST 2022; root:xnu-8020.101.4~2/RELEASE_X86_64 x86_64 Subsystem No response What steps will reproduce the bug? Have 2 files as follows:...
GitHub (github.com)
-
@zachleat Build your own JS runtime with deno?
-
@jak2k when they said they wanted me to demo a Deno rapper at a conference I think I misunderstood the assignment
-
maxfentonreplied to Zach Leatherman :11ty: last edited by
@zachleat It's beyond my understanding, but AlpineJS does _something_ that's not actually eval()
-
Zach Leatherman :11ty:replied to maxfenton last edited by
@maxfenton ah yes! Thank you!
-
maxfentonreplied to Zach Leatherman :11ty: last edited by
@zachleat I mean, I read your readme but I still wasn't sure if I was saying “hey here's this thing you already described" :blob_laughing:
-
Zach Leatherman :11ty:replied to maxfenton last edited by
@maxfenton yeah, sorry about that—definitely helpful!
-
Mayankreplied to Zach Leatherman :11ty: last edited by
@zachleat yeah it worked for my use-case because i was feeding the output back into a template literal. you could do some serialization/deserialization for greater flexibility but will probably hit a wall relatively soon.
definitely wouldn't recommend this. i just want stable `vm` + ESM
-
Zach Leatherman :11ty:replied to Mayank last edited by
@mayank *SAME*