webpack.prod.conf.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 ExtractTextPlugin = require('extract-text-webpack-plugin')
  11. const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
  12. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  13. let env
  14. switch (process.env.ENV_ID) {
  15. case '145':
  16. env = require('../config/145.env')
  17. break;
  18. case '520':
  19. env = require('../config/520.env')
  20. break;
  21. case '680':
  22. env = require('../config/prod.env')
  23. break;
  24. case 'test':
  25. env = require('../config/test.env')
  26. break;
  27. case 'zz':
  28. env = require('../config/zz.env')
  29. break;
  30. default:
  31. env = require('../config/prod.env')
  32. break;
  33. }
  34. const webpackConfig = merge(baseWebpackConfig, {
  35. module: {
  36. rules: utils.styleLoaders({
  37. sourceMap: config.build.productionSourceMap,
  38. extract: true,
  39. usePostCSS: true
  40. })
  41. },
  42. devtool: config.build.productionSourceMap ? config.build.devtool : false,
  43. output: {
  44. path: config.build.assetsRoot,
  45. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  46. chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  47. },
  48. plugins: [
  49. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  50. new webpack.DefinePlugin({
  51. 'process.env': env
  52. }),
  53. new UglifyJsPlugin({
  54. uglifyOptions: {
  55. compress: {
  56. warnings: false,
  57. drop_debugger: true,
  58. drop_console: true
  59. }
  60. },
  61. sourceMap: config.build.productionSourceMap,
  62. parallel: true
  63. }),
  64. // extract css into its own file
  65. new ExtractTextPlugin({
  66. filename: utils.assetsPath('css/[name].[contenthash].css'),
  67. // Setting the following option to `false` will not extract CSS from codesplit chunks.
  68. // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
  69. // It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
  70. // increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
  71. allChunks: true,
  72. }),
  73. // Compress extracted CSS. We are using this plugin so that possible
  74. // duplicated CSS from different components can be deduped.
  75. new OptimizeCSSPlugin({
  76. cssProcessorOptions: config.build.productionSourceMap
  77. ? { safe: true, map: { inline: false } }
  78. : { safe: true }
  79. }),
  80. // generate dist index.html with correct asset hash for caching.
  81. // you can customize output by editing /index.html
  82. // see https://github.com/ampedandwired/html-webpack-plugin
  83. new HtmlWebpackPlugin({
  84. filename: config.build.index,
  85. template: 'index.html',
  86. favicon: './src/assets/favicon.ico',
  87. inject: true,
  88. minify: {
  89. removeComments: true,
  90. collapseWhitespace: true,
  91. removeAttributeQuotes: true
  92. // more options:
  93. // https://github.com/kangax/html-minifier#options-quick-reference
  94. },
  95. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  96. chunksSortMode: 'dependency'
  97. }),
  98. // keep module.id stable when vendor modules does not change
  99. new webpack.HashedModuleIdsPlugin(),
  100. // enable scope hoisting
  101. new webpack.optimize.ModuleConcatenationPlugin(),
  102. // split vendor js into its own file
  103. new webpack.optimize.CommonsChunkPlugin({
  104. name: 'vendor',
  105. minChunks(module) {
  106. // any required modules inside node_modules are extracted to vendor
  107. return (
  108. module.resource &&
  109. /\.js$/.test(module.resource) &&
  110. module.resource.indexOf(
  111. path.join(__dirname, '../node_modules')
  112. ) === 0
  113. )
  114. }
  115. }),
  116. // extract webpack runtime and module manifest to its own file in order to
  117. // prevent vendor hash from being updated whenever app bundle is updated
  118. new webpack.optimize.CommonsChunkPlugin({
  119. name: 'manifest',
  120. minChunks: Infinity
  121. }),
  122. // This instance extracts shared chunks from code splitted chunks and bundles them
  123. // in a separate chunk, similar to the vendor chunk
  124. // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
  125. new webpack.optimize.CommonsChunkPlugin({
  126. name: 'app',
  127. async: 'vendor-async',
  128. children: true,
  129. minChunks: 3
  130. }),
  131. // copy custom static assets
  132. new CopyWebpackPlugin([
  133. {
  134. from: path.resolve(__dirname, '../static'),
  135. to: config.build.assetsSubDirectory,
  136. ignore: ['.*']
  137. }
  138. ])
  139. ]
  140. })
  141. if (config.build.productionGzip) {
  142. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  143. webpackConfig.plugins.push(
  144. new CompressionWebpackPlugin({
  145. asset: '[path].gz[query]',
  146. algorithm: 'gzip',
  147. test: new RegExp(
  148. '\\.(' +
  149. config.build.productionGzipExtensions.join('|') +
  150. ')$'
  151. ),
  152. threshold: 10240,
  153. minRatio: 0.8
  154. })
  155. )
  156. }
  157. if (config.build.bundleAnalyzerReport) {
  158. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  159. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  160. }
  161. module.exports = webpackConfig