Skip to content

Import IIFE Scripts #11145

Discussion options

You must be logged in to vote

The key was to wrap the source file with Function(return (+ ... +);)() to get a reference to the module constructor function, which can then be constructed. I'm open to better ideas, but this works for my case.

function createModule(pthORurl, ...args) {
    try {
        let mod = Function("return ("+Deno.readTextFileSync(pthORurl)+");")();
        if(typeof(mod)!=="function") { throw Error("Loaded module is type '"+typeof(mod)+"' not type 'function'"); }
        mod = new mod(...args);
        if(typeof(mod)!=="object") { throw Error("Loaded module is type '"+typeof(mod)+"' not type 'object'"); }
        return mod;
        }
    catch(err) { err.message += " (loading "+pthORurl+")"; thr…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by l-cornelius-dol
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant
Converted from issue

This discussion was converted from issue #11143 on June 26, 2021 23:21.