Htmlwebpackplugin This plugin can be used to generate static HTML files. The default internal is to generate an HTML file through the background. Of course, you can also use your own files to generate templates. can support. html files. You can also use other types of templates. such as Ejs. And Ejs is simple and convenient. It's a good place to build the static files we need.
The basic usage of Htmlwebpackplugin is as follows:
First, you need to require in the Webpack configuration or import with ES6:
Const Htmlwebpackplugin = require (' Html-webpack-plugin ');
The plug-in is then referenced in the configured plugins option:
New Htmlwebpackplugin ({}),
The supported options can be viewed here , and he supports multiple index generation. You only need to introduce it multiple times in plugins:
Note: This introduces more than just the default index.html. So when you need multiple different HTML. Each introduction is a separate configuration, and the introduction of HTML as a template is straightforward. You just need to provide the appropriate properties.
New Htmlwebpackplugin ({ title:' My first Webpack ', Template:' index.html ' })
Then you need to use EJS to generate the necessary HTML files, with ejs words need the corresponding ejs-loader, otherwise you write in Ejs <%=htmlwebpack.options.xx%> will error. Because he can't handle the Ejs type file. Add the Ejs type file to the module:
module: { rules: [ {test:/\.ejs$/,use:[' Ejs-loader ']}, /\.css$/, use: [' Style-loader ', ' Css-loader ']}, /\. (gif|png|svg|jpg)/, use: [' File-loader ']} ] }
Then add the following configuration to the Htmlwebpackplugin configuration:
New Htmlwebpackplugin ({
//title title:' My first Webpack ', //Where the template is located
Template:' Index.ejs ',
//Any other data name:' 123124 ', data :' 454564457 ' })
Inside the Index.ejs are as follows:
<! DOCTYPE html>
Use Webpack to compile the generated file as follows:
<! DOCTYPE html>
Webpack Htmlwebpackplugin using a custom template