webpack.prod.conf.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use strict'
  2. const path = require('path')
  3. const utils = require('./utils')
  4. const webpack = require('webpack')
  5. const config = require('../config')
  6. const merge = require('webpack-merge')
  7. const baseWebpackConfig = require('./webpack.base.conf')
  8. const CopyWebpackPlugin = require('copy-webpack-plugin')
  9. const HtmlWebpackPlugin = require('html-webpack-plugin')
  10. const ExtractTextPlugin = require('extract-text-webpack-plugin')
  11. const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
  12. const env = config.build.env
  13. const webpackConfig = merge(baseWebpackConfig, {
  14. module: {
  15. rules: utils.styleLoaders({
  16. sourceMap: config.build.productionSourceMap,
  17. extract: true
  18. })
  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. plugins: [
  27. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  28. new webpack.DefinePlugin({
  29. 'process.env': env
  30. }),
  31. // UglifyJs do not support ES6+, you can also use babel-minify for better treeshaking: https://github.com/babel/minify
  32. new webpack.optimize.UglifyJsPlugin({
  33. compress: {
  34. warnings: false
  35. },
  36. sourceMap: true
  37. }),
  38. // extract css into its own file
  39. new ExtractTextPlugin({
  40. filename: utils.assetsPath('css/[name].[contenthash].css')
  41. }),
  42. // Compress extracted CSS. We are using this plugin so that possible
  43. // duplicated CSS from different components can be deduped.
  44. new OptimizeCSSPlugin({
  45. cssProcessorOptions: {
  46. safe: true
  47. }
  48. }),
  49. // generate dist index.html with correct asset hash for caching.
  50. // you can customize output by editing /index.html
  51. // see https://github.com/ampedandwired/html-webpack-plugin
  52. new HtmlWebpackPlugin({
  53. filename: config.build.index,
  54. template: 'index.html',
  55. inject: true,
  56. minify: {
  57. removeComments: true,
  58. collapseWhitespace: true,
  59. removeAttributeQuotes: true
  60. // more options:
  61. // https://github.com/kangax/html-minifier#options-quick-reference
  62. },
  63. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  64. chunksSortMode: 'dependency'
  65. }),
  66. // keep module.id stable when vender modules does not change
  67. new webpack.HashedModuleIdsPlugin(),
  68. // split vendor js into its own file
  69. new webpack.optimize.CommonsChunkPlugin({
  70. name: 'vendor',
  71. minChunks: function (module) {
  72. // any required modules inside node_modules are extracted to vendor
  73. return (
  74. module.resource &&
  75. /\.js$/.test(module.resource) &&
  76. module.resource.indexOf(
  77. path.join(__dirname, '../node_modules')
  78. ) === 0
  79. )
  80. }
  81. }),
  82. // extract webpack runtime and module manifest to its own file in order to
  83. // prevent vendor hash from being updated whenever app bundle is updated
  84. new webpack.optimize.CommonsChunkPlugin({
  85. name: 'manifest',
  86. chunks: ['vendor']
  87. }),
  88. // copy custom static assets
  89. new CopyWebpackPlugin([
  90. {
  91. from: path.resolve(__dirname, '../static'),
  92. to: config.build.assetsSubDirectory,
  93. ignore: ['.*']
  94. }
  95. ])
  96. ]
  97. })
  98. if (config.build.productionGzip) {
  99. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  100. webpackConfig.plugins.push(
  101. new CompressionWebpackPlugin({
  102. asset: '[path].gz[query]',
  103. algorithm: 'gzip',
  104. test: new RegExp(
  105. '\\.(' +
  106. config.build.productionGzipExtensions.join('|') +
  107. ')$'
  108. ),
  109. threshold: 10240,
  110. minRatio: 0.8
  111. })
  112. )
  113. }
  114. if (config.build.bundleAnalyzerReport) {
  115. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  116. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  117. }
  118. module.exports = webpackConfig