Bez popisu

webpack.prod.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. const path = require('path')
  2. const webpack = require('webpack')
  3. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  4. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
  5. const HtmlWebpackPlugin = require("html-webpack-plugin")
  6. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  7. const glob = require('glob')
  8. const setMPA = () => {
  9. const entry = {}
  10. const htmklWebpackPlugins = [];
  11. const entryFiles = glob.sync(path.join(__dirname ,"./src/*/index.js")) // 匹配入口文件
  12. console.log('entryFiles',entryFiles)
  13. Object.keys(entryFiles).map((index) => {
  14. const entryFile = entryFiles[index]
  15. const match = entryFile.match(/src\/(.*)\/index\.js/)
  16. const pageName = match && match[1]
  17. entry[pageName] = entryFile; // 设置entry
  18. htmklWebpackPlugins.push( // 设置html模版
  19. new HtmlWebpackPlugin({
  20. template: path.join(__dirname, `src/${pageName}/index.html`),
  21. filename: `${pageName}.html`,
  22. chunks: ['vendors', pageName],
  23. inject: true, // 注入css/js
  24. minify: { // 模板格式设置
  25. html5:true,
  26. collapseWhitespace: true ,
  27. preserveLicenseComments: false ,
  28. minifyJS: true ,
  29. minifyCSS: true,
  30. removeComments: true,
  31. }
  32. }),
  33. )
  34. })
  35. return {
  36. entry,
  37. htmklWebpackPlugins ,
  38. }
  39. }
  40. const {entry,htmklWebpackPlugins} = setMPA()
  41. module.exports = {
  42. entry,
  43. output: {
  44. path: path.join(__dirname, 'dist'),
  45. filename: '[name]_[chunkhash:8].js' // hash, chunkhash, cententhash 哈希
  46. },
  47. // mode: 'development',
  48. mode: 'production',
  49. watch: true ,
  50. module: {
  51. rules:[
  52. {
  53. test: /.js$/,
  54. use:'babel-loader'
  55. },
  56. {
  57. test: /.css$/,
  58. use:[
  59. // 'style-loader',// 将css插入到html style标签,如果要使用contenthash生成css文件,需要注释
  60. MiniCssExtractPlugin.loader ,
  61. 'css-loader'
  62. ]
  63. },
  64. {
  65. test: /.less$/,
  66. use:[
  67. // 'style-loader',// 将css插入到html style标签,如果要使用contenthash生成css文件,需要注释
  68. MiniCssExtractPlugin.loader ,
  69. 'css-loader',
  70. 'less-loader',
  71. {
  72. loader: 'postcss-loader',
  73. options: {
  74. plugins: () => [
  75. require('autoprefixer')({ // 自动补全
  76. overrideBrowserslist:["last 2 version", ">1%", 'ios 7']
  77. })
  78. ]
  79. }
  80. }, {
  81. loader: "px2rem-loader",
  82. options: {
  83. remUnit: 75, // 1rem = 75px
  84. remPrecesion: 8, // px转换rem到小数位
  85. }
  86. }
  87. ]
  88. },
  89. {
  90. test: /.(png|jpg|gif|jpeg)$/,
  91. use:[
  92. {
  93. loader: 'file-loader',
  94. options: {
  95. // limit: 10240
  96. name: '[name]_[hash:8].[ext]'
  97. }
  98. }
  99. ]
  100. },
  101. {
  102. test: /.(otf|ttf|woff|woff2|eot)$/,
  103. use:[
  104. {
  105. loader: 'file-loader',
  106. options: {
  107. // limit: 10240
  108. name: '[name]_[hash:8][ext]'
  109. }
  110. }
  111. ]
  112. }
  113. ]
  114. },
  115. plugins:[
  116. new MiniCssExtractPlugin({
  117. filename: '[name]_[contenthash:8].css'
  118. }),
  119. new OptimizeCSSAssetsPlugin({ // 压缩css
  120. assetNameRegExp: /\.css$/g,
  121. cssProcessor: require('cssnano')
  122. }),
  123. new CleanWebpackPlugin(), // 构建完成之前删除原来到构建目录
  124. ].concat(htmklWebpackPlugins),
  125. // optimization : {
  126. // splitChunks: {
  127. // cacheGroups: {
  128. // commons: {
  129. // test: /(react|react-dom)/,
  130. // name: "vendors",
  131. // chunks: 'all'
  132. // }
  133. // }
  134. // }
  135. // }
  136. optimization : {
  137. splitChunks: {
  138. minSize: 0, // 引用的模块大小,
  139. cacheGroups: {
  140. commons: {
  141. // test: /(react|react-dom)/,
  142. name: "commons",
  143. chunks: 'all',
  144. minChunks: 3, // 最少引用了两次
  145. }
  146. }
  147. }
  148. }
  149. }