Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

183 linhas
5.5 KiB

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