如何在Webpack模块中捆绑ejs模板

I want to use default ejs templates, but be able to override them when reusing the module.

myModule.ts

这有效:

const data = {foo:123};   
const markup = require('../templates/calendar.ejs');    
markup(data); // => <p>123</p>

这不起作用:

const data = {foo:123};
const templateFile = '../templates/calendar.ejs'
const markup = require(templateFile);    
markup(data); // => Error: Cannot find module '../templates/calendar.ejs'

I'm not even sure if this it the correct way of loading ejs templates. I'm not using express oder any node stuff, I just want to be able to generate some HTML with my bundle.js. Is there a way to tell webpack to bundle some files or do I need to use the HtmlWebpackPlugin to generate separate output files. If so, how can I load them?

I do not rely on ejs, I would even use _.template from loadash as long as I can separate them to files. But never found a good way of loading string templates without node. Any advice?

谢谢!

评论