webpack.prod.conf.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.ProvidePlugin({
  31. $: 'jquery',
  32. jquery: 'jquery',
  33. 'window.jQuery': 'jquery',
  34. jQuery: 'jquery'
  35. }),
  36. new webpack.optimize.UglifyJsPlugin({
  37. compress: {
  38. warnings: false
  39. },
  40. sourceMap: true
  41. }),
  42. // extract css into its own file
  43. new ExtractTextPlugin({
  44. filename: utils.assetsPath('css/[name].[contenthash].css')
  45. }),
  46. // Compress extracted CSS. We are using this plugin so that possible
  47. // duplicated CSS from different components can be deduped.
  48. new OptimizeCSSPlugin(),
  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. // split vendor js into its own file
  67. new webpack.optimize.CommonsChunkPlugin({
  68. name: 'vendor',
  69. minChunks: function (module, count) {
  70. // any required modules inside node_modules are extracted to vendor
  71. return (
  72. module.resource &&
  73. /\.js$/.test(module.resource) &&
  74. module.resource.indexOf(
  75. path.join(__dirname, '../node_modules')
  76. ) === 0
  77. )
  78. }
  79. }),
  80. // extract webpack runtime and module manifest to its own file in order to
  81. // prevent vendor hash from being updated whenever app bundle is updated
  82. new webpack.optimize.CommonsChunkPlugin({
  83. name: 'manifest',
  84. chunks: ['vendor']
  85. }),
  86. // copy custom static assets
  87. new CopyWebpackPlugin([
  88. {
  89. from: path.resolve(__dirname, '../static'),
  90. to: config.build.assetsSubDirectory,
  91. ignore: ['.*']
  92. }
  93. ])
  94. ]
  95. })
  96. if (config.build.productionGzip) {
  97. var CompressionWebpackPlugin = require('compression-webpack-plugin')
  98. webpackConfig.plugins.push(
  99. new CompressionWebpackPlugin({
  100. asset: '[path].gz[query]',
  101. algorithm: 'gzip',
  102. test: new RegExp(
  103. '\\.(' +
  104. config.build.productionGzipExtensions.join('|') +
  105. ')$'
  106. ),
  107. threshold: 10240,
  108. minRatio: 0.8
  109. })
  110. )
  111. }
  112. if (config.build.bundleAnalyzerReport) {
  113. var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  114. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  115. }
  116. module.exports = webpackConfig