webpack.prod.conf.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. var path = require('path')
  2. var utils = require('./utils')
  3. var webpack = require('webpack')
  4. var config = require('../config')
  5. var merge = require('webpack-merge')
  6. var baseWebpackConfig = require('./webpack.base.conf')
  7. var HtmlWebpackPlugin = require('html-webpack-plugin')
  8. var ExtractTextPlugin = require('extract-text-webpack-plugin')
  9. var env = process.env.NODE_ENV === 'testing'
  10. ? require('../config/test.env')
  11. : config.build.env
  12. var webpackConfig = merge(baseWebpackConfig, {
  13. module: {
  14. rules: utils.styleLoaders({
  15. sourceMap: config.build.productionSourceMap,
  16. extract: true
  17. })
  18. },
  19. devtool: config.build.productionSourceMap ? '#source-map' : false,
  20. output: {
  21. path: config.build.assetsRoot,
  22. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  23. chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  24. },
  25. plugins: [
  26. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  27. new webpack.DefinePlugin({
  28. 'process.env': env
  29. }),
  30. new webpack.optimize.UglifyJsPlugin({
  31. compress: {
  32. warnings: false
  33. },
  34. sourceMap: true
  35. }),
  36. // extract css into its own file
  37. new ExtractTextPlugin({
  38. filename: utils.assetsPath('css/[name].[contenthash].css')
  39. }),
  40. // generate dist index.html with correct asset hash for caching.
  41. // you can customize output by editing /index.html
  42. // see https://github.com/ampedandwired/html-webpack-plugin
  43. new HtmlWebpackPlugin({
  44. filename: process.env.NODE_ENV === 'testing'
  45. ? 'index.html'
  46. : config.build.index,
  47. template: 'index.html',
  48. inject: true,
  49. minify: {
  50. removeComments: true,
  51. collapseWhitespace: true,
  52. removeAttributeQuotes: true
  53. // more options:
  54. // https://github.com/kangax/html-minifier#options-quick-reference
  55. },
  56. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  57. chunksSortMode: 'dependency'
  58. }),
  59. // split vendor js into its own file
  60. new webpack.optimize.CommonsChunkPlugin({
  61. name: 'vendor',
  62. minChunks: function (module, count) {
  63. // any required modules inside node_modules are extracted to vendor
  64. return (
  65. module.resource &&
  66. /\.js$/.test(module.resource) &&
  67. module.resource.indexOf(
  68. path.join(__dirname, '../node_modules')
  69. ) === 0
  70. )
  71. }
  72. }),
  73. // extract webpack runtime and module manifest to its own file in order to
  74. // prevent vendor hash from being updated whenever app bundle is updated
  75. new webpack.optimize.CommonsChunkPlugin({
  76. name: 'manifest',
  77. chunks: ['vendor']
  78. })
  79. ]
  80. })
  81. if (config.build.productionGzip) {
  82. var CompressionWebpackPlugin = require('compression-webpack-plugin')
  83. webpackConfig.plugins.push(
  84. new CompressionWebpackPlugin({
  85. asset: '[path].gz[query]',
  86. algorithm: 'gzip',
  87. test: new RegExp(
  88. '\\.(' +
  89. config.build.productionGzipExtensions.join('|') +
  90. ')$'
  91. ),
  92. threshold: 10240,
  93. minRatio: 0.8
  94. })
  95. )
  96. }
  97. if (config.build.bundleAnalyzerReport) {
  98. var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  99. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  100. }
  101. module.exports = webpackConfig