Skip to content

插件(plugins)

https://www.webpackjs.com/plugins/

html-webpack-plugin

https://www.webpackjs.com/plugins/html-webpack-plugin/

html-webpack-plugin会在打包结束后,自动生成一个html文件,并把打包生成的 js 自动引入到这个 html 文件中

src/index.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>html模板</title>
</head>
<body>
  <div id="root"></div>
</body>
</html>
1
2
3
plugins: [new HtmlWebpackPlugin({
  template: 'src/index.html'
})],

plugin 可以在 webpack 运行到某个时刻的时候,帮你做一些事情

clean-webpack-plugin

https://github.com/johnagan/clean-webpack-plugin

npm install clean-webpack-plugin -D

1
2
3
4
5
6
7
8
const { CleanWebpackPlugin } = require('clean-webpack-plugin'); 

plugins: [
  new CleanWebpackPlugin(),
  new HtmlWebpackPlugin({
    template: 'src/index.html'
  })
],