webpack.prod.conf.js 3.9 KB

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