I have a string variable with some code which is essentially ESModule:
let name = "test_snippet";
let injectedCode =
`const callback = (environment, ..._) => {
function ${name}() {
const global = (0,eval)("this");
global.works = true;
console.info("${name} works!");
}
const injectedSnippetsList = ["${name}"];
const injectedSnippetsCallbacks = [${name}];
const snippets = {};
for (let e = 0, { length: t} = injectedSnippetsList; e < t; e++)
snippets[injectedSnippetsList[e]] = injectedSnippetsCallbacks[e];
for (let [name, ...args] of _) {
if (snippets.hasOwnProperty(name)) {
try {
snippets[name](...args);
} catch (error) {
console.error(error);
}
}
}
};
callback.has = snippet => [
"${name}"
].includes(snippet);
export default callback;
`;
How can i "compile" it into ESModule to call it's has for example?
I have the following:
async function compileModule(code)
{
let moduleData = "data:text/javascript;base64," + btoa(code);
return await import(moduleData);
}
but it interprets moduleData as module path, not as a "content", resulting in "failed to load".
Any suggestions?