webpack.prod.conf.js 5.3 KB

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