webpack.prod.conf.js 6.2 KB

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