安装依赖

  1. npm i html-webpack-plugin -D

webpack配置

  1. const {resolve}=require('path')
  2. const HtmlWebpackPlugin=require('html-webpack-plugin')//首先引入包
  3. module.exports={
  4. entry:'./src/index.js',
  5. output:{
  6. filename:'main.js',
  7. path:resolve(__dirname,'build')
  8. },
  9. // loader的配置
  10. module:{
  11. rules:[
  12. {
  13. test:/\.css$/,
  14. use:[
  15. 'style-loader',
  16. 'css-loader'
  17. ]
  18. }
  19. ]
  20. },
  21. plugins:[
  22. new HtmlWebpackPlugin( //在插件选项中配置,并指定模板是哪个
  23. {
  24. template:'./src/index.html'
  25. }
  26. )
  27. ],
  28. mode:'production'
  29. }

最后运行webpack就会在build中生成index.html,它会自动绑定js,css