webpack.prod.conf.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // 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: config.build.index,
  48. template: 'index.html',
  49. inject: true,
  50. minify: {
  51. removeComments: true,
  52. collapseWhitespace: true,
  53. removeAttributeQuotes: true
  54. // more options:
  55. // https://github.com/kangax/html-minifier#options-quick-reference
  56. },
  57. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  58. chunksSortMode: 'dependency'
  59. }),
  60. // split vendor js into its own file
  61. new webpack.optimize.CommonsChunkPlugin({
  62. name: 'vendor',
  63. minChunks: function (module, count) {
  64. // any required modules inside node_modules are extracted to vendor
  65. return (
  66. module.resource &&
  67. /\.js$/.test(module.resource) &&
  68. module.resource.indexOf(
  69. path.join(__dirname, '../node_modules')
  70. ) === 0
  71. )
  72. }
  73. }),
  74. // extract webpack runtime and module manifest to its own file in order to
  75. // prevent vendor hash from being updated whenever app bundle is updated
  76. new webpack.optimize.CommonsChunkPlugin({
  77. name: 'manifest',
  78. chunks: ['vendor']
  79. }),
  80. // copy custom static assets
  81. new CopyWebpackPlugin([
  82. {
  83. from: path.resolve(__dirname, '../static'),
  84. to: config.build.assetsSubDirectory,
  85. ignore: ['.*']
  86. }
  87. ])
  88. ]
  89. })
  90. if (config.build.productionGzip) {
  91. var CompressionWebpackPlugin = require('compression-webpack-plugin')
  92. webpackConfig.plugins.push(
  93. new CompressionWebpackPlugin({
  94. asset: '[path].gz[query]',
  95. algorithm: 'gzip',
  96. test: new RegExp(
  97. '\\.(' +
  98. config.build.productionGzipExtensions.join('|') +
  99. ')$'
  100. ),
  101. threshold: 10240,
  102. minRatio: 0.8
  103. })
  104. )
  105. }
  106. if (config.build.bundleAnalyzerReport) {
  107. var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  108. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  109. }
  110. module.exports = webpackConfig