I'd like to generate html contents using javascript template literals, and output static html files on Webpack compile time. I also need a development server with hot module replacement capabilities that works with html, css, and js files.
I require page1.js inside HtmlWebpackPlugin options:
new HtmlWebpackPlugin({
template: './html-template.ejs',
appMount: require('./src/views/page1')
})
Then I use a custom ejs template html-template.ejs to import the generated html contents:
<% if (htmlWebpackPlugin.options.appMount) { %>
<%= htmlWebpackPlugin.options.appMount %>
<% } %>
page1.js looks like this:
const h1 = (text) => `<h1>${text}</h1>`;
module.exports = h1('Hello World');
Note that page1.js is not set as entry point. The only entry point is src/index.js (which only have a console.log
statement inside).
When I run webpack
everything works fine, dist/index.html is generated along side with dist/main.js.
If I run webpack-dev-server
and try to edit page1.js, the browser reloads but html contents doesn't update.
Any ideas on how to make webpack-dev-server
and Hot Module Replacement works on views files? Or do you have any other better solution to create a simple static site generator?