I have a function that lets me run a JavaScript function in the browser:
export async function executeFunction(data: LoadOutput, input: Record<string, any>): Promise<any> { let args = []; for (let param of data.functionTree[0].parameters) { let value = input[param.name]; if(!value) { return undefined; } args.push(value); } let func = new Function('return '+ data.jsCode)(); return await func.call(null, ...args);}
It all works, but now I want to add more functionality by being able to use external libraries.I tried adding imports in the string but this didn't work and resulted in errors
There was an error: Module name, 'image-conversion' does not resolve to a valid URL.
Is there a way I can import external libraries?Preferably by not having to pass them as a parameter (That's what ChatGPT suggested but would require a massive refactor of my code)
Is there maybe a library that does the same thing as Function but lets me add external libraries to the scope?
For additional information: I'm using SvelteKit