The use of the html-webpack-plugin of Webpack

Source: Internet
Author: User

Webpack the actual use of the process has a common plug-in html-webpack-plugin. Html-webpack-plugin will help us automatically generate an HTML page, and can dynamically write page title and linked bundle.js files on the page.

Reasons for use

When Webpack is packaged, it starts with the entry JS file in the entry, looks for the dependencies of the component, and ultimately generates the packaged bundle.js file in the corresponding directory based on the configuration in output. If we define a index.html file ourselves, we may face some problems: one is the actual production of the Bundle.js file is followed by a hash value, before packaging this value is not known, directly in the page given bundle file path is not very good; the other reason is that the bundle.js file Naming is based on the Webpack configuration of the output.filename dynamic generation, in the Index.html page directly introduced is also not convenient; for one reason, webpack files generated after packaging will generally be in a dist directory, run, and then manually copy index.html to dis The T directory is not a good idea after all.
These problems, html-webpack-plugin can be very good for me to solve. The detailed use of the plugin can go to this address.

Here is a simple example to illustrate the use of this plugin

Directory structure and description of the project


The project source is stacked in the SRC directory, and the documents are described below:
template.html--entry HTML file for the project
index.js--Project Package Entry file
Hello.js--index.js-Dependent files

Installation and use

Html-webpack-plugin is not a webpack built-in plugin and needs to be installed.
NPM Install Html-webpack-plugin--save-dev

After installation, plug-ins need to be introduced into the Webpack

After you build using the Webpack command, the following directories and files are generated

Look at the HTML file we generated:

Use in Devserver

In the actual development process, the code will be changed frequently, after each change to republish to the Dist directory, and then refresh the browser, this is not very convenient. Using Devserver can simplify this process and see the configuration using Devserver.

Start with the command Webpack-dev-server--hot
Then modify the address in the browser to http://localhost:3000/admin.html

The use of the html-webpack-plugin of Webpack

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.