webpack.prod.conf.js 3.5 KB

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