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