5
votes

I want to generate a template with Html Webpack Plugin. I don't want to generate an .html file from a template, I want to generate an .ejs template from another .ejs template.

I want to do this so that when rendering the template from the backend, I can insert different api keys depending on the environment. For example, I want to generate a template that looks something like this:

...
<script>
   API.setKey('<%= APIKey %>');
</script>
...

so that when staging, a test key can be used.

Is it possible to generate templates with Html Webpack Plugin?

2
I don't know what you mean when you say you want to render a .ejs template using another .ejs template, but you might want to check out ejs-loader to generate .ejs templates. https://github.com/okonet/ejs-loader - user5483398
I want to generate a template that still has <%= %>. Maybe I'm barking up the wrong tree. - oorst
@cynicaldevil It was actually easy. I just put the '<%= %>' into another template tag. <%= '<%= APIKey %>' %> - oorst
@oorst thank you, it worked for me. The only thing to mention is you should escape the template characters in the string. Ex: <%= '\<\%\= user_data \%\>' %> - tszekely

2 Answers

0
votes

See here

You can override the loader:

new HtmlWebpackPlugin({

  template: '!!html-loader!src/index.ejs'
})

it should be possible just to use an "identity-loader"

-1
votes

Yes, you can generate any text based file with this plugin. You will have to provide template and filename properties to the plugin as follows

new HtmlWebpackPlugin({
  template: 'template.ejs',
  filename: 'endresult.ejs'
})