Suppose I have an ever growing set of different templates to be used with react. Each template extends React.Component and renders fancy border around its children.
I expect to generate such templates (with caching etc.) on the fly from some data known only at run-time.
At run-time I can compute the name of the module containing the template. I can map an URL to a server code to provide JavaScript source and expect the browser to be able to run it.
I imagine that I would isolate this piece of code in a simple method similar loading a *.DLL by name and calling its exported symbol. My guess below does not work.
private async templateLoader():Promise<React.ComponentType>{
const templateModuleName:string = this.props.api.getTemplateName(this.props.user);
return (await import(templateModuleName)) as React.ComponentType;
}
I can imagine JavaScript using require.js like so
var propsMappedFromStore=this.props;
//...
require(["api","user"],function(api,user){
var templateModuleName = api.getTemplateName(user);
require([templateModuleName],function(tt){
propsMappedFromStore.updateTemplate(tt);
})
})
But is it possible with Typescript and Webpack?
How would I require/import the module identified by expression?
