webpack.prod.conf.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. }),
  39. // extract css into its own file
  40. new MiniCssExtractPlugin({
  41. filename: utils.assetsPath('css/[name].[contenthash].css')
  42. }),
  43. // generate dist index.html with correct asset hash for caching.
  44. // you can customize output by editing /index.html
  45. // see https://github.com/ampedandwired/html-webpack-plugin
  46. new HtmlWebpackPlugin({
  47. filename: process.env.NODE_ENV === 'testing'
  48. ? 'index.html'
  49. : config.build.index,
  50. template: 'index.html',
  51. inject: true,
  52. minify: {
  53. removeComments: true,
  54. collapseWhitespace: true,
  55. removeAttributeQuotes: true,
  56. ignoreCustomComments: [/server-generated-meta/]
  57. // more options:
  58. // https://github.com/kangax/html-minifier#options-quick-reference
  59. },
  60. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  61. chunksSortMode: 'dependency'
  62. }),
  63. // split vendor js into its own file
  64. // extract webpack runtime and module manifest to its own file in order to
  65. // prevent vendor hash from being updated whenever app bundle is updated
  66. // new webpack.optimize.SplitChunksPlugin({
  67. // name: ['app', 'vendor']
  68. // }),
  69. ]
  70. })
  71. if (config.build.productionGzip) {
  72. var CompressionWebpackPlugin = require('compression-webpack-plugin')
  73. webpackConfig.plugins.push(
  74. new CompressionWebpackPlugin({
  75. asset: '[path].gz[query]',
  76. algorithm: 'gzip',
  77. test: new RegExp(
  78. '\\.(' +
  79. config.build.productionGzipExtensions.join('|') +
  80. ')$'
  81. ),
  82. threshold: 10240,
  83. minRatio: 0.8
  84. })
  85. )
  86. }
  87. module.exports = webpackConfig