webpack.prod.conf.js 5.3 KB

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