webpack.prod.conf.js 5.3 KB

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