webpack.prod.conf.js 3.2 KB

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