webpack.prod.conf.js 3.2 KB

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