Webpack Htmlwebpackplugin using a custom template

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.