webpack.prod.conf.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  13. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  14. const env = require('../config/prod.env')
  15. const webpackConfig = merge(baseWebpackConfig, {
  16. module: {
  17. rules: utils.styleLoaders({
  18. sourceMap: config.build.productionSourceMap,
  19. extract: true,
  20. usePostCSS: true
  21. })
  22. },
  23. devtool: config.build.productionSourceMap ? config.build.devtool : false,
  24. output: {
  25. path: config.build.assetsRoot,
  26. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  27. chunkFilename: utils.assetsPath('js/[name].[chunkhash].js')
  28. },
  29. plugins: [
  30. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  31. new webpack.DefinePlugin({
  32. 'process.env': env
  33. }),
  34. new BundleAnalyzerPlugin({
  35. analyzerMode: 'server',
  36. analyzerHost: '127.0.0.1',
  37. analyzerPort: 8889,
  38. reportFilename: 'report.html',
  39. defaultSizes: 'parsed',
  40. openAnalyzer: true,
  41. generateStatsFile: false,
  42. statsFilename: 'stats.json',
  43. statsOptions: null,
  44. logLevel: 'info'
  45. }),
  46. new UglifyJsPlugin({
  47. uglifyOptions: {
  48. compress: {
  49. warnings: false
  50. }
  51. },
  52. sourceMap: config.build.productionSourceMap,
  53. parallel: true
  54. }),
  55. // extract css into its own file
  56. new ExtractTextPlugin({
  57. filename: utils.assetsPath('css/[name].[contenthash].css'),
  58. // Setting the following option to `false` will not extract CSS from codesplit chunks.
  59. // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
  60. // It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
  61. // increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
  62. allChunks: true,
  63. }),
  64. // Compress extracted CSS. We are using this plugin so that possible
  65. // duplicated CSS from different components can be deduped.
  66. new OptimizeCSSPlugin({
  67. cssProcessorOptions: config.build.productionSourceMap
  68. ? { safe: true, map: { inline: false } }
  69. : { safe: true }
  70. }),
  71. // generate dist index.html with correct asset hash for caching.
  72. // you can customize output by editing /index.html
  73. // see https://github.com/ampedandwired/html-webpack-plugin
  74. new HtmlWebpackPlugin({
  75. filename: config.build.index,
  76. template: 'index.html',
  77. inject: true,
  78. minify: {
  79. removeComments: true,
  80. collapseWhitespace: true,
  81. removeAttributeQuotes: true
  82. // more options:
  83. // https://github.com/kangax/html-minifier#options-quick-reference
  84. },
  85. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  86. chunksSortMode: 'dependency'
  87. }),
  88. // keep module.id stable when vendor modules does not change
  89. new webpack.HashedModuleIdsPlugin(),
  90. // enable scope hoisting
  91. new webpack.optimize.ModuleConcatenationPlugin(),
  92. // split vendor js into its own file
  93. new webpack.optimize.CommonsChunkPlugin({
  94. name: 'vendor',
  95. minChunks (module) {
  96. // any required modules inside node_modules are extracted to vendor
  97. return (
  98. module.resource &&
  99. /\.js$/.test(module.resource) &&
  100. module.resource.indexOf(
  101. path.join(__dirname, '../node_modules')
  102. ) === 0
  103. )
  104. }
  105. }),
  106. // extract webpack runtime and module manifest to its own file in order to
  107. // prevent vendor hash from being updated whenever app bundle is updated
  108. new webpack.optimize.CommonsChunkPlugin({
  109. name: 'manifest',
  110. minChunks: Infinity
  111. }),
  112. // This instance extracts shared chunks from code splitted chunks and bundles them
  113. // in a separate chunk, similar to the vendor chunk
  114. // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
  115. new webpack.optimize.CommonsChunkPlugin({
  116. name: 'app',
  117. async: 'vendor-async',
  118. children: true,
  119. minChunks: 3
  120. }),
  121. new CopyWebpackPlugin([
  122. {
  123. from: path.resolve(__dirname, '../static'),
  124. to: config.build.assetsSubDirectory,
  125. ignore: ['.*']
  126. }
  127. ])
  128. // copy custom static assets
  129. ]
  130. })
  131. if (config.build.productionGzip) {
  132. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  133. webpackConfig.plugins.push(
  134. new CompressionWebpackPlugin({
  135. filename: '[path].gz[query]',
  136. algorithm: 'gzip',
  137. test: new RegExp('\\.(' + config.build.productionGzipExtensions.join('|') +')$'),
  138. threshold: 10240,
  139. minRatio: 0.8
  140. })
  141. )
  142. }
  143. if (config.build.bundleAnalyzerReport) {
  144. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  145. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  146. }
  147. module.exports = webpackConfig