webpack.config.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const {resolve} = require('path')
  2. const HtmlWebpackPlugin = require('html-webpack-plugin')
  3. module.exports = {
  4. entry: './src/index.js',
  5. output:{
  6. filename: 'built.js',
  7. path: resolve(__dirname,'build')
  8. },
  9. module:{
  10. rules:[
  11. {
  12. test: /\.less$/,
  13. use: ['style-loader','css-loader','less-loader']
  14. },
  15. {
  16. //处理图片资源
  17. test:/\.(jpeg|png|gif|jpg)$/,
  18. // 需要下载url-loader和file-loader webpack5已弃用,添加type:'javascript/auto'可解决问题。
  19. loader: 'url-loader',
  20. options:{
  21. // 图片大小小于8kb 就会被base64处理
  22. // 减少请求数量 但图片体积会变大
  23. limit: 8 * 1024,
  24. esModule: false, // 解决[object%20Module]问题 原因:url-loader默认使用es6模块化解析,html-loader引入图片commonjs
  25. // 取图片的hash前10位 + 扩展名
  26. name: '[hash:10].[ext]'
  27. },
  28. type: 'javascript/auto'
  29. },
  30. {
  31. test: /\.html$/,
  32. loader: 'html-loader'
  33. }
  34. ]
  35. },
  36. plugins:[
  37. new HtmlWebpackPlugin({
  38. template: './src/index.html'
  39. })
  40. ],
  41. mode: 'development'
  42. }