webpack.config.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 glob = require('glob');
  8. // // 获取所有入口文件(假设入口文件均为目录下的 index.js)
  9. // const entries = glob.sync('./src/**/index.js').reduce((acc, filePath) => {
  10. // // 提取相对于 src 的路径作为入口名称(如 'features/dashboard/index')
  11. // const entryName = path.relative('src', filePath).replace(/\.js \)/, '');
  12. // acc[entryName] = filePath;
  13. // return acc;
  14. // }, {});
  15. module.exports={
  16. entry:{
  17. index:"./index.js",
  18. },
  19. output:{
  20. filename:"[name].js",
  21. path:path.resolve(__dirname,"./dist")
  22. },
  23. resolve: {
  24. alias: {
  25. vue: 'vue/dist/vue.esm-bundler.js' // 关键配置
  26. }
  27. },
  28. module:{
  29. rules:[
  30. {
  31. test:/\.css$/i,
  32. use:["style-loader","css-loader"],
  33. options: {
  34. outputPath: './style/' // [!code ++]
  35. }
  36. },
  37. {
  38. test:/\.vue$/,
  39. use:[
  40. { loader:"vue-loader" }
  41. ]
  42. },
  43. // 处理 .js 文件
  44. {
  45. test: /\.js$/,
  46. exclude: /node_modules/,
  47. use: {
  48. loader: 'babel-loader',
  49. options: {
  50. presets: ['@babel/preset-env']
  51. }
  52. }
  53. },
  54. {
  55. test:/\.(png|jpe?g|gif|svg)$/i,
  56. // use:[{
  57. // loader:"file-loader",
  58. // options:{
  59. // limit: 512 * 1024,
  60. // name: "[name].[ext]",
  61. // outputPath:"media",
  62. // },
  63. // }]
  64. type: 'asset/resource',
  65. generator:{
  66. filename: "[name].[ext]",
  67. }
  68. },
  69. {
  70. test:/\.(woff2?|eof|ttf)$/i,
  71. // use:[{
  72. // loader:"file-loader",
  73. // options:{
  74. // limit: 512 * 1024,
  75. // name: "[name].[ext]",
  76. // outputPath:"media",
  77. // },
  78. // }]
  79. type: 'asset/resource',
  80. // generator:{
  81. // filename: "./font/[name].[ext]",
  82. // }
  83. },]
  84. },
  85. plugins:[
  86. new VueLoaderPlugin(),
  87. new CleanWebpackPlugin(),
  88. new HtmlWebpackPlugin({
  89. inject:'head',
  90. title:"index page ",
  91. template:"./index.html",
  92. filename:"index.html",
  93. chunks:['index'],
  94. }),
  95. ],
  96. // 优化配置(如代码拆分)
  97. optimization: {
  98. splitChunks: {
  99. chunks: 'all',
  100. minSize: 20000,
  101. cacheGroups: {
  102. vendors: {
  103. test: /[\\/]node_modules[\\/]/,
  104. priority: -10,
  105. reuseExistingChunk: true,
  106. name: 'vendors'
  107. }
  108. }
  109. }
  110. }
  111. }