webpack.prod.conf.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 MiniCssExtractPlugin = require('mini-css-extract-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. var webpackConfig = merge(baseWebpackConfig, {
  16. mode: 'production',
  17. module: {
  18. rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, extract: true })
  19. },
  20. devtool: config.build.productionSourceMap ? '#source-map' : false,
  21. optimization: {
  22. minimize: true,
  23. splitChunks: {
  24. chunks: 'all'
  25. }
  26. },
  27. output: {
  28. path: config.build.assetsRoot,
  29. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  30. chunkFilename: utils.assetsPath('js/[name].[chunkhash].js')
  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. '__VUE_OPTIONS_API__': true,
  39. '__VUE_PROD_DEVTOOLS__': false
  40. }),
  41. // extract css into its own file
  42. new MiniCssExtractPlugin({
  43. filename: utils.assetsPath('css/[name].[contenthash].css')
  44. }),
  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: process.env.NODE_ENV === 'testing'
  50. ? 'index.html'
  51. : config.build.index,
  52. template: 'index.html',
  53. inject: true,
  54. minify: {
  55. removeComments: true,
  56. collapseWhitespace: true,
  57. removeAttributeQuotes: true,
  58. ignoreCustomComments: [/server-generated-meta/]
  59. // more options:
  60. // https://github.com/kangax/html-minifier#options-quick-reference
  61. },
  62. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  63. chunksSortMode: 'dependency'
  64. }),
  65. // split vendor js into its own file
  66. // extract webpack runtime and module manifest to its own file in order to
  67. // prevent vendor hash from being updated whenever app bundle is updated
  68. // new webpack.optimize.SplitChunksPlugin({
  69. // name: ['app', 'vendor']
  70. // }),
  71. ]
  72. })
  73. if (config.build.productionGzip) {
  74. var CompressionWebpackPlugin = require('compression-webpack-plugin')
  75. webpackConfig.plugins.push(
  76. new CompressionWebpackPlugin({
  77. asset: '[path].gz[query]',
  78. algorithm: 'gzip',
  79. test: new RegExp(
  80. '\\.(' +
  81. config.build.productionGzipExtensions.join('|') +
  82. ')$'
  83. ),
  84. threshold: 10240,
  85. minRatio: 0.8
  86. })
  87. )
  88. }
  89. module.exports = webpackConfig