I have TypeScript which asynchronously downloads another TypeScript/javascript module:
(function (exports) {
"use strict";
var path = require('path');
exports.convertData = function (data) {
return "converted " + data;
}
})(typeof exports === 'undefined' ? this['converter.someConverter'] = {} : exports);
During execution of my main app I receives this module as string and I have to use function convertData from there.
So, I'm trying the following:
eval(rendererScript);
console.log(exports.convertData("some data"));
It works well only in case if var path = require('path'); will be removed. Otherwise, the following error: Uncaught (in promise) Error: Module name "path" has not been loaded yet for context: _. Use require([])
Questions:
- Is it OK to use eval() in this case? (as we know eval is evil)
- How to be with require('path')? I'm trying to use RequireJS (http://requirejs.org/docs/node.html#2) but receiving the following error: Uncaught Error: Script error for "path"
Edited
So, in order to avoid eval() the following solution was found:
const scriptTag = document.createElement("script");
scriptTag.innerHTML = rendererScript;
document.body.appendChild(scriptTag);
this.m_rendererPlugin = (window as any)[`converter.someConverter`];