webpack.prod.conf.js 3.1 KB

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