webpack中使用plugin让打包更加便捷!
plugin插件的作用
plugin 可以在webpack运行到某个时刻的时候,帮你做一些事情
打包过后并没有自动生成index.html ?
1.安装插件
命令行输入:npm install html-webpack-plugin -D
2.使用步骤
(1)在webpack.config.js中引入
const HtmlWebpackPlugin = require('html-webpack-plugin');
(2)使用plugin
与output等同级
plugins:[new HtmlWebpackPlugin( )],
(3)重新打包,发现dist文件夹下自动生成一个html文件,并把打包生成的js自动引入到这个html文件中
(4)在src 文件夹下创建一个html的模板
(5)在webpack.config.js文件中修改
plugins:[new HtmlWebpackPlugin({
template:'src/index.html'
})],
(6)再次打包,发现dist文件下的html文件多了你刚刚写的html模板的内容
在重新打包时自动先删除dist文件夹
安装插件
命令行输入:npm install clean-webpack-plugin -D
引入
//打开webpack.config.js文件
const CleanWebpackPlugin = require('clean-webpack-plugin')
//在plugins中调用
plugins:[new HtmlWebpackPlugin({
template:'src/index.html'
}),new CleanWebpackPlugin(['dist'])],//删除dist文件夹
官网推荐使用的plugin:webpack.js.org/plugins/