webpack.config.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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: '[name]/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. {
  66. loader: 'babel-loader',
  67. options: {
  68. presets: ['@babel/preset-env']
  69. }
  70. }
  71. ]
  72. },
  73. {
  74. test:/\.(png|jpe?g|gif|svg|webp)$/i,
  75. // use:[{
  76. // loader:"file-loader",
  77. // options:{
  78. // limit: 512 * 1024,
  79. // name: "[name].[ext]",
  80. // outputPath:"media",
  81. // },
  82. // }]
  83. type: 'asset/resource',
  84. generator:{
  85. filename: "[name].[ext]",
  86. }
  87. },
  88. {
  89. test:/\.(woff2?|eof|ttf)$/i,
  90. // use:[{
  91. // loader:"file-loader",
  92. // options:{
  93. // limit: 512 * 1024,
  94. // name: "[name].[ext]",
  95. // outputPath:"media",
  96. // },
  97. // }]
  98. type: 'asset/resource',
  99. generator:{
  100. filename: "./font/[name].[ext]",
  101. }
  102. },]
  103. },
  104. plugins:[
  105. new VueLoaderPlugin(),
  106. new CleanWebpackPlugin(),
  107. new HtmlWebpackPlugin({
  108. inject:'head',
  109. title:"index page ",
  110. template:"./index.html",
  111. filename:"index.html",
  112. chunks:['index'],
  113. }),
  114. new MiniCssExtractPlugin({
  115. // CSS 输出到对应入口的 css 目录
  116. filename: '[name]/css/[name].[contenthash:8].css'
  117. }),
  118. ],
  119. // 优化配置(如代码拆分)
  120. optimization: {
  121. splitChunks: {
  122. chunks: 'all',
  123. minSize: 20000,
  124. cacheGroups: {
  125. vendors: {
  126. test: /[\\/]node_modules[\\/]/,
  127. priority: -10,
  128. reuseExistingChunk: true,
  129. name: 'vendors'
  130. }
  131. }
  132. }
  133. }
  134. }