webpack.prod.conf.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 'use strict'
  2. const path = require('path')
  3. const utils = require('./utils')
  4. const webpack = require('webpack')
  5. const config = require('../config')
  6. const merge = require('webpack-merge')
  7. const baseWebpackConfig = require('./webpack.base.conf')
  8. const CopyWebpackPlugin = require('copy-webpack-plugin')
  9. const HtmlWebpackPlugin = require('html-webpack-plugin')
  10. const ExtractTextPlugin = require('extract-text-webpack-plugin')
  11. const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
  12. const env = require('../config/prod.env')
  13. const webpackConfig = merge(baseWebpackConfig, {
  14. module: {
  15. rules: utils.styleLoaders({
  16. sourceMap: config.build.productionSourceMap,
  17. extract: true,
  18. usePostCSS: true
  19. })
  20. },
  21. devtool: config.build.productionSourceMap ? config.build.devtool : 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. }),
  32. // UglifyJs do not support ES6+, you can also use babel-minify for better treeshaking: https://github.com/babel/minify
  33. new webpack.optimize.UglifyJsPlugin({
  34. compress: {
  35. warnings: false
  36. },
  37. sourceMap: config.build.productionSourceMap,
  38. parallel: true
  39. }),
  40. // extract css into its own file
  41. new ExtractTextPlugin({
  42. filename: utils.assetsPath('css/[name].[contenthash].css'),
  43. // set the following option to `true` if you want to extract CSS from
  44. // codesplit chunks into this main css file as well.
  45. // This will result in *all* of your app's CSS being loaded upfront.
  46. allChunks: false,
  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. // keep module.id stable when vender modules does not change
  66. new webpack.HashedModuleIdsPlugin(),
  67. // enable scope hoisting
  68. new webpack.optimize.ModuleConcatenationPlugin(),
  69. // split vendor js into its own file
  70. // new webpack.optimize.CommonsChunkPlugin({
  71. // name: 'vendor',
  72. // minChunks: function (module) {
  73. // // any required modules inside node_modules are extracted to vendor
  74. // return (
  75. // module.resource &&
  76. // /\.js$/.test(module.resource) &&
  77. // module.resource.indexOf(
  78. // path.join(__dirname, '../node_modules')
  79. // ) === 0
  80. // )
  81. // }
  82. // }),
  83. // extract webpack runtime and module manifest to its own file in order to
  84. // prevent vendor hash from being updated whenever app bundle is updated
  85. // new webpack.optimize.CommonsChunkPlugin({
  86. // name: 'manifest',
  87. // minChunks: Infinity
  88. // }),
  89. // This instance extracts shared chunks from code splitted chunks and bundles them
  90. // in a separate chunk, similar to the vendor chunk
  91. // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
  92. // new webpack.optimize.CommonsChunkPlugin({
  93. // name: 'app',
  94. // async: 'vendor-async',
  95. // children: true,
  96. // minChunks: 3
  97. // }),
  98. // copy custom static assets
  99. new CopyWebpackPlugin([
  100. {
  101. from: path.resolve(__dirname, '../static'),
  102. to: config.build.assetsSubDirectory,
  103. ignore: ['.*']
  104. }
  105. ])
  106. ]
  107. })
  108. if (config.test.productionGzip) {
  109. var CompressionWebpackPlugin = require('compression-webpack-plugin')
  110. webpackConfig.plugins.push(
  111. new CompressionWebpackPlugin({
  112. asset: '[path].gz[query]',
  113. algorithm: 'gzip',
  114. test: new RegExp(
  115. '\\.(' +
  116. config.test.productionGzipExtensions.join('|') +
  117. ')$'
  118. ),
  119. threshold: 10240,
  120. minRatio: 0.8
  121. })
  122. )
  123. }
  124. if (config.build.bundleAnalyzerReport) {
  125. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  126. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  127. }
  128. module.exports = webpackConfig