webpack.prod.conf.js 5.5 KB

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