webpack.prod.conf.js 3.7 KB

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