详解html-webpack-plugin插件(用法总结)

html-webpack-plugin 插件是用于编译 Webpack 项目中的 html 类型的文件,如果直接将 html 文件置于 ./src 目录中,用 Webpack 打包时是不会编译到生产环境中的。因为 Webpack 编译任何文件都需要基于配置文件先行配置的。

Webpack 插件使用三步曲:安装>引入>配置

npm 安装

rush:bash;"> npm install --save-dev html-webpack-plugin

yarn 安装

rush:bash;"> yarn add html-webpack-plugin --dev

html-webpack-plugin 入口未定义时

rush:js;"> //webpack.config.js const path = require('path'); const htmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
entry: {
home: path.resolve(dirname,'./src/app.js')
},output: {
path: path.resolve(
dirname,'dist'),filename: '[name].js'
},plugins: [
new htmlWebpackPlugin()
]
}

输出的 html 文件为:dist/index.html

rush:xhtml;"> <Meta charset="UTF-8"> Webpack App