webpack.prod.conf.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 = config.build.env
  10. var webpackConfig = merge(baseWebpackConfig, {
  11. module: {
  12. loaders: 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. vue: {
  24. loaders: utils.cssLoaders({
  25. sourceMap: config.build.productionSourceMap,
  26. extract: true
  27. })
  28. },
  29. plugins: [
  30. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  31. new webpack.DefinePlugin({
  32. 'process.env': env
  33. }),
  34. new webpack.optimize.UglifyJsPlugin({
  35. compress: {
  36. warnings: false
  37. }
  38. }),
  39. new webpack.optimize.OccurrenceOrderPlugin(),
  40. // extract css into its own file
  41. new ExtractTextPlugin(utils.assetsPath('css/[name].[contenthash].css')),
  42. // generate dist index.html with correct asset hash for caching.
  43. // you can customize output by editing /index.html
  44. // see https://github.com/ampedandwired/html-webpack-plugin
  45. new HtmlWebpackPlugin({
  46. filename: 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. module.exports = webpackConfig