webpack.prod.conf.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. 'use strict'
  2. const path = require('path')
  3. const utils = require('./utils')
  4. const webpack = require('webpack')
  5. const config = require('../config')
  6. const merge = require('webpack-merge')
  7. const baseWebpackConfig = require('./webpack.base.conf')
  8. const CopyWebpackPlugin = require('copy-webpack-plugin')
  9. const HtmlWebpackPlugin = require('html-webpack-plugin')
  10. const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin')
  11. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  12. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
  13. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  14. function resolve(dir) {
  15. return path.join(__dirname, '..', dir)
  16. }
  17. const env = require('../config/' + process.env.env_config + '.env')
  18. // For NamedChunksPlugin
  19. const seen = new Set()
  20. const nameLength = 4
  21. const webpackConfig = merge(baseWebpackConfig, {
  22. mode: 'production',
  23. module: {
  24. rules: utils.styleLoaders({
  25. sourceMap: config.build.productionSourceMap,
  26. extract: true,
  27. usePostCSS: true
  28. })
  29. },
  30. devtool: config.build.productionSourceMap ? config.build.devtool : false,
  31. output: {
  32. path: config.build.assetsRoot,
  33. filename: utils.assetsPath('js/[name].[chunkhash:12].js'),
  34. chunkFilename: utils.assetsPath('js/[name].[chunkhash:12].js')
  35. },
  36. plugins: [
  37. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  38. new webpack.DefinePlugin({
  39. 'process.env': env
  40. }),
  41. new webpack.ProvidePlugin({
  42. introJs: ['intro.js', 'introJs']
  43. })
  44. ,
  45. // extract css into its own file
  46. new MiniCssExtractPlugin({
  47. filename: utils.assetsPath('css/[name].[contenthash:12].css'),
  48. chunkFilename: utils.assetsPath('css/[name].[contenthash:12].css')
  49. }),
  50. // generate dist index.html with correct asset hash for caching.
  51. // you can customize output by editing /index.html
  52. // see https://github.com/ampedandwired/html-webpack-plugin
  53. new HtmlWebpackPlugin({
  54. filename: config.build.index,
  55. template: 'index.html',
  56. inject: true,
  57. favicon: resolve('favicon.ico'),
  58. title: '积分系统',
  59. path: config.build.assetsPublicPath + config.build.assetsSubDirectory,
  60. minify: {
  61. removeComments: true,
  62. collapseWhitespace: true,
  63. removeAttributeQuotes: true
  64. // more options:
  65. // https://github.com/kangax/html-minifier#options-quick-reference
  66. }
  67. // default sort mode uses toposort which cannot handle cyclic deps
  68. // in certain cases, and in webpack 4, chunk order in HTML doesn't
  69. // matter anyway
  70. }),
  71. new ScriptExtHtmlWebpackPlugin({
  72. //`runtime` must same as runtimeChunk name. default is `runtime`
  73. inline: /runtime\..*\.js$/
  74. }),
  75. // keep chunk.id stable when chunk has no name
  76. new webpack.NamedChunksPlugin(chunk => {
  77. if (chunk.name) {
  78. return chunk.name
  79. }
  80. const modules = Array.from(chunk.modulesIterable)
  81. if (modules.length > 1) {
  82. const hash = require('hash-sum')
  83. const joinedHash = hash(modules.map(m => m.id).join('_'))
  84. let len = nameLength
  85. while (seen.has(joinedHash.substr(0, len))) len++
  86. seen.add(joinedHash.substr(0, len))
  87. return `chunk-${joinedHash.substr(0, len)}`
  88. } else {
  89. return modules[0].id
  90. }
  91. }),
  92. // keep module.id stable when vender modules does not change
  93. new webpack.HashedModuleIdsPlugin(),
  94. // copy custom static assets
  95. new CopyWebpackPlugin([
  96. {
  97. from: path.resolve(__dirname, '../static'),
  98. to: config.build.assetsSubDirectory,
  99. ignore: ['.*']
  100. }
  101. ])
  102. ],
  103. optimization: {
  104. splitChunks: {
  105. chunks: 'all',
  106. cacheGroups: {
  107. libs: {
  108. name: 'chunk-libs',
  109. test: /[\\/]node_modules[\\/]/,
  110. priority: 10,
  111. chunks: 'initial' // 只打包初始时依赖的第三方
  112. },
  113. elementUI: {
  114. name: 'chunk-elementUI', // 单独将 elementUI 拆包
  115. priority: 20, // 权重要大于 libs 和 app 不然会被打包进 libs 或者 app
  116. test: /[\\/]node_modules[\\/]element-ui[\\/]/
  117. },
  118. commons: {
  119. name: 'chunk-comomns',
  120. test: resolve('src/components'), // 可自定义拓展你的规则
  121. minChunks: 3, // 最小公用次数
  122. priority: 5,
  123. reuseExistingChunk: true
  124. }
  125. }
  126. },
  127. runtimeChunk: 'single',
  128. minimizer: [
  129. new UglifyJsPlugin({
  130. uglifyOptions: {
  131. mangle: {
  132. safari10: true
  133. }
  134. },
  135. sourceMap: config.build.productionSourceMap,
  136. cache: true,
  137. parallel: true
  138. }),
  139. // Compress extracted CSS. We are using this plugin so that possible
  140. // duplicated CSS from different components can be deduped.
  141. new OptimizeCSSAssetsPlugin()
  142. ]
  143. }
  144. })
  145. if (config.build.productionGzip) {
  146. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  147. // webpackConfig.plugins.push(
  148. // new CompressionWebpackPlugin({
  149. // asset: '[path].gz[query]',
  150. // algorithm: 'gzip',
  151. // test: new RegExp(
  152. // '\\.(' + config.build.productionGzipExtensions.join('|') + ')$'
  153. // ),
  154. // threshold: 10240,
  155. // minRatio: 0.8
  156. // })
  157. // )
  158. webpackConfig.plugins.push(
  159. new CompressionWebpackPlugin({
  160. // asset: '[path].gz[query]',
  161. filename: '[path].gz[query]',
  162. algorithm: 'gzip',
  163. test: new RegExp(
  164. '\\.(' +
  165. config.build.productionGzipExtensions.join('|') +
  166. ')$'
  167. ),
  168. threshold: 10240,
  169. // deleteOriginalAssets:true, //删除源文件,不建议
  170. minRatio: 0.8
  171. })
  172. )
  173. }
  174. if (config.build.generateAnalyzerReport || config.build.bundleAnalyzerReport) {
  175. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer')
  176. .BundleAnalyzerPlugin
  177. if (config.build.bundleAnalyzerReport) {
  178. webpackConfig.plugins.push(
  179. new BundleAnalyzerPlugin({
  180. analyzerPort: 8080,
  181. generateStatsFile: false
  182. })
  183. )
  184. }
  185. if (config.build.generateAnalyzerReport) {
  186. webpackConfig.plugins.push(
  187. new BundleAnalyzerPlugin({
  188. analyzerMode: 'static',
  189. reportFilename: 'bundle-report.html',
  190. openAnalyzer: false
  191. })
  192. )
  193. }
  194. }
  195. module.exports = webpackConfig