webpack.prod.conf.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. 'BASE_URL' : '"/api"'
  30. }),
  31. new webpack.optimize.UglifyJsPlugin({
  32. compress: {
  33. warnings: false
  34. },
  35. sourceMap: true
  36. }),
  37. // extract css into its own file
  38. new ExtractTextPlugin({
  39. filename: utils.assetsPath('css/[name].[contenthash].css')
  40. }),
  41. // Compress extracted CSS. We are using this plugin so that possible
  42. // duplicated CSS from different components can be deduped.
  43. new OptimizeCSSPlugin({
  44. cssProcessorOptions: {
  45. safe: true
  46. }
  47. }),
  48. // generate dist index.html with correct asset hash for caching.
  49. // you can customize output by editing /index.html
  50. // see https://github.com/ampedandwired/html-webpack-plugin
  51. new HtmlWebpackPlugin({
  52. filename: config.build.index,
  53. template: 'index.html',
  54. inject: true,
  55. minify: {
  56. removeComments: true,
  57. collapseWhitespace: true,
  58. removeAttributeQuotes: true
  59. // more options:
  60. // https://github.com/kangax/html-minifier#options-quick-reference
  61. },
  62. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  63. chunksSortMode: 'dependency'
  64. }),
  65. // split vendor js into its own file
  66. new webpack.optimize.CommonsChunkPlugin({
  67. name: 'vendor',
  68. minChunks: function (module, count) {
  69. // any required modules inside node_modules are extracted to vendor
  70. return (
  71. module.resource &&
  72. /\.js$/.test(module.resource) &&
  73. module.resource.indexOf(
  74. path.join(__dirname, '../node_modules')
  75. ) === 0
  76. )
  77. }
  78. }),
  79. // extract webpack runtime and module manifest to its own file in order to
  80. // prevent vendor hash from being updated whenever app bundle is updated
  81. new webpack.optimize.CommonsChunkPlugin({
  82. name: 'manifest',
  83. chunks: ['vendor']
  84. }),
  85. // copy custom static assets
  86. new CopyWebpackPlugin([
  87. {
  88. from: path.resolve(__dirname, '../static'),
  89. to: config.build.assetsSubDirectory,
  90. ignore: ['.*']
  91. }
  92. ])
  93. ]
  94. })
  95. if (config.build.productionGzip) {
  96. var CompressionWebpackPlugin = require('compression-webpack-plugin')
  97. webpackConfig.plugins.push(
  98. new CompressionWebpackPlugin({
  99. asset: '[path].gz[query]',
  100. algorithm: 'gzip',
  101. test: new RegExp(
  102. '\\.(' +
  103. config.build.productionGzipExtensions.join('|') +
  104. ')$'
  105. ),
  106. threshold: 10240,
  107. minRatio: 0.8
  108. })
  109. )
  110. }
  111. if (config.build.bundleAnalyzerReport) {
  112. var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  113. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  114. }
  115. module.exports = webpackConfig