webpack.config.js 3.2 KB

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