webpack.prod.conf.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/en/workflow/production.html
  30. new webpack.DefinePlugin({
  31. 'process.env': env
  32. }),
  33. new webpack.optimize.OccurrenceOrderPlugin(),
  34. // extract css into its own file
  35. new ExtractTextPlugin('[name].[contenthash].css'),
  36. // generate dist index.html with correct asset hash for caching.
  37. // you can customize output by editing /index.html
  38. // see https://github.com/ampedandwired/html-webpack-plugin
  39. new HtmlWebpackPlugin({
  40. filename: process.env.NODE_ENV === 'testing'
  41. ? 'index.html'
  42. : 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. module.exports = webpackConfig