webpack.prod.conf.js 3.0 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 MiniCssExtractPlugin = require('mini-css-extract-plugin')
  8. const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
  9. var HtmlWebpackPlugin = require('html-webpack-plugin')
  10. var env = process.env.NODE_ENV === 'testing'
  11. ? require('../config/test.env')
  12. : config.build.env
  13. let commitHash = require('child_process')
  14. .execSync('git rev-parse --short HEAD')
  15. .toString();
  16. var webpackConfig = merge(baseWebpackConfig, {
  17. mode: 'production',
  18. module: {
  19. rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, extract: true })
  20. },
  21. devtool: config.build.productionSourceMap ? 'source-map' : false,
  22. optimization: {
  23. minimize: true,
  24. splitChunks: {
  25. chunks: 'all'
  26. },
  27. minimizer: [
  28. `...`,
  29. new CssMinimizerPlugin()
  30. ]
  31. },
  32. output: {
  33. path: config.build.assetsRoot,
  34. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  35. chunkFilename: utils.assetsPath('js/[name].[chunkhash].js')
  36. },
  37. plugins: [
  38. // http://vuejs.github.io/vue-loader/workflow/production.html
  39. new webpack.DefinePlugin({
  40. 'process.env': env,
  41. 'COMMIT_HASH': JSON.stringify(commitHash),
  42. 'DEV_OVERRIDES': JSON.stringify(undefined),
  43. '__VUE_OPTIONS_API__': true,
  44. '__VUE_PROD_DEVTOOLS__': false
  45. }),
  46. // extract css into its own file
  47. new MiniCssExtractPlugin({
  48. filename: utils.assetsPath('css/[name].[contenthash].css')
  49. }),
  50. // generate dist index.html with correct asset hash for caching.
  51. // you can customize output by editing /index.html
  52. // see https://github.com/ampedandwired/html-webpack-plugin
  53. new HtmlWebpackPlugin({
  54. filename: process.env.NODE_ENV === 'testing'
  55. ? 'index.html'
  56. : config.build.index,
  57. template: 'index.html',
  58. inject: true,
  59. minify: {
  60. removeComments: true,
  61. collapseWhitespace: true,
  62. removeAttributeQuotes: true,
  63. ignoreCustomComments: [/server-generated-meta/]
  64. // more options:
  65. // https://github.com/kangax/html-minifier#options-quick-reference
  66. }
  67. }),
  68. // split vendor js into its own file
  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.SplitChunksPlugin({
  72. // name: ['app', 'vendor']
  73. // }),
  74. ]
  75. })
  76. if (config.build.productionGzip) {
  77. var CompressionWebpackPlugin = require('compression-webpack-plugin')
  78. webpackConfig.plugins.push(
  79. new CompressionWebpackPlugin({
  80. asset: '[path].gz[query]',
  81. algorithm: 'gzip',
  82. test: new RegExp(
  83. '\\.(' +
  84. config.build.productionGzipExtensions.join('|') +
  85. ')$'
  86. ),
  87. threshold: 10240,
  88. minRatio: 0.8
  89. })
  90. )
  91. }
  92. module.exports = webpackConfig