webpack.config.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. const path=require('path');
  2. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  3. //const { HtmlWebpackPlugin } = require("html-webpack-plugin");
  4. const HtmlWebpackPlugin = require('html-webpack-plugin');
  5. //const { VueLoaderPlugin } = require('vue-loader/dist/index');
  6. const { VueLoaderPlugin } = require('vue-loader');
  7. module.exports={
  8. entry:{
  9. index:"./index.js",
  10. },
  11. output:{
  12. filename:"[name].js",
  13. path:path.resolve(__dirname,"./dist")
  14. },
  15. resolve: {
  16. alias: {
  17. vue: 'vue/dist/vue.esm-bundler.js' // 关键配置
  18. }
  19. },
  20. module:{
  21. rules:[
  22. {
  23. test: /\.html \)/,
  24. use: [
  25. {
  26. loader: 'html-loader',
  27. options: { // [!code focus]
  28. // 配置 HTML 中资源引用的处理规则
  29. sources: { // [!code focus]
  30. list: [ // [!code focus]
  31. { tag: 'img', attribute: 'src', type: 'src' }, // 处理 <img src>
  32. { tag: 'a', attribute: 'href', type: 'src' } // 处理 <a href> // [!code focus]
  33. ] // [!code focus]
  34. } // [!code focus]
  35. } // [!code focus]
  36. }
  37. ]
  38. },
  39. {
  40. test:/\.css$/i,
  41. use:["style-loader","css-loader"],
  42. },
  43. {
  44. test:/\.vue$/,
  45. use:[
  46. { loader:"vue-loader" }
  47. ]
  48. },
  49. // 处理 .js 文件
  50. {
  51. test: /\.js$/,
  52. exclude: /node_modules/,
  53. use: {
  54. loader: 'babel-loader',
  55. options: {
  56. presets: ['@babel/preset-env']
  57. }
  58. }
  59. },
  60. {
  61. test:/\.(png|jpe?g|gif|svg)$/i,
  62. // use:[{
  63. // loader:"file-loader",
  64. // options:{
  65. // limit: 512 * 1024,
  66. // name: "[name].[ext]",
  67. // outputPath:"media",
  68. // },
  69. // }]
  70. type: 'asset/resource',
  71. generator:{
  72. filename: "[name].[ext]",
  73. }
  74. },
  75. {
  76. test:/\.(woff2?|eof|ttf)$/i,
  77. // use:[{
  78. // loader:"file-loader",
  79. // options:{
  80. // limit: 512 * 1024,
  81. // name: "[name].[ext]",
  82. // outputPath:"media",
  83. // },
  84. // }]
  85. type: 'asset/resource',
  86. generator:{
  87. filename: "./font/[name].[ext]",
  88. }
  89. },]
  90. },
  91. plugins:[
  92. new VueLoaderPlugin(),
  93. new CleanWebpackPlugin(),
  94. new HtmlWebpackPlugin({
  95. inject:'head',
  96. title:"index page ",
  97. template:"./index.html",
  98. filename:"index.html",
  99. chunks:['index'],
  100. }),
  101. ],
  102. // 优化配置(如代码拆分)
  103. optimization: {
  104. splitChunks: {
  105. chunks: 'all',
  106. minSize: 20000,
  107. cacheGroups: {
  108. vendors: {
  109. test: /[\\/]node_modules[\\/]/,
  110. priority: -10,
  111. reuseExistingChunk: true,
  112. name: 'vendors'
  113. }
  114. }
  115. }
  116. }
  117. }