webpack.prod.conf.js 5.5 KB

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