webpack.prod.conf.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 env = config.build.env
  14. const webpackConfig = merge(baseWebpackConfig, {
  15. module: {
  16. rules: utils.styleLoaders({
  17. sourceMap: config.build.productionSourceMap,
  18. extract: false,
  19. usePostCSS: true
  20. })
  21. },
  22. devtool: config.build.productionSourceMap ? config.build.devtool : false,
  23. output: {
  24. path: config.build.assetsRoot,
  25. filename: utils.assetsPath('js/[name].[chunkhash].js'),
  26. chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
  27. },
  28. plugins: [
  29. // http://vuejs.github.io/vue-loader/en/workflow/production.html
  30. new webpack.DefinePlugin({
  31. 'process.env': env
  32. }),
  33. new UglifyJsPlugin({
  34. // 使用外部引入的新版本的js压缩工具
  35. parallel: true,
  36. uglifyOptions: {
  37. ie8: false,
  38. ecma: 5,
  39. warnings: false,
  40. mangle: true, // debug false
  41. output: {
  42. comments: false,
  43. beautify: false, // debug true
  44. },
  45. compress: {
  46. // 在UglifyJs删除没有用到的代码时不输出警告
  47. warnings: false,
  48. // 删除所有的 `console` 语句
  49. // 还可以兼容ie浏览器
  50. drop_console: true,
  51. // 内嵌定义了但是只用到一次的变量
  52. collapse_vars: true,
  53. // 提取出出现多次但是没有定义成变量去引用的静态值
  54. reduce_vars: true,
  55. }
  56. }
  57. }),
  58. // UglifyJs do not support ES6+, you can also use babel-minify for better treeshaking: https://github.com/babel/minify
  59. // new webpack.optimize.UglifyJsPlugin({
  60. // compress: {
  61. // warnings: false
  62. // },
  63. // sourceMap: config.build.productionSourceMap,
  64. // parallel: true
  65. // }),
  66. // extract css into its own file
  67. new ExtractTextPlugin({
  68. filename: utils.assetsPath('css/[name].[contenthash].css'),
  69. // set the following option to `true` if you want to extract CSS from
  70. // codesplit chunks into this main css file as well.
  71. // This will result in *all* of your app's CSS being loaded upfront.
  72. allChunks: false,
  73. }),
  74. // generate dist index.html with correct asset hash for caching.
  75. // you can customize output by editing /index.html
  76. // see https://github.com/ampedandwired/html-webpack-plugin
  77. new HtmlWebpackPlugin({
  78. filename: config.build.index,
  79. template: 'index.html',
  80. inject: true,
  81. minify: {
  82. removeComments: true,
  83. collapseWhitespace: true,
  84. removeAttributeQuotes: true
  85. // more options:
  86. // https://github.com/kangax/html-minifier#options-quick-reference
  87. },
  88. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  89. chunksSortMode: 'dependency'
  90. }),
  91. // keep module.id stable when vender modules does not change
  92. new webpack.HashedModuleIdsPlugin(),
  93. // enable scope hoisting
  94. new webpack.optimize.ModuleConcatenationPlugin(),
  95. // split vendor js into its own file
  96. new webpack.optimize.CommonsChunkPlugin({
  97. name: 'vendor',
  98. minChunks: function (module) {
  99. // any required modules inside node_modules are extracted to vendor
  100. return (
  101. module.resource &&
  102. /\.js$/.test(module.resource) &&
  103. module.resource.indexOf(
  104. path.join(__dirname, '../node_modules')
  105. ) === 0
  106. )
  107. }
  108. }),
  109. // extract webpack runtime and module manifest to its own file in order to
  110. // prevent vendor hash from being updated whenever app bundle is updated
  111. new webpack.optimize.CommonsChunkPlugin({
  112. name: 'manifest',
  113. minChunks: Infinity
  114. }),
  115. // This instance extracts shared chunks from code splitted chunks and bundles them
  116. // in a separate chunk, similar to the vendor chunk
  117. // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
  118. new webpack.optimize.CommonsChunkPlugin({
  119. name: 'app',
  120. async: 'vendor-async',
  121. children: true,
  122. minChunks: 3
  123. }),
  124. // copy custom static assets
  125. new CopyWebpackPlugin([
  126. {
  127. from: path.resolve(__dirname, '../static'),
  128. to: config.build.assetsSubDirectory,
  129. ignore: ['.*']
  130. }
  131. ])
  132. ]
  133. })
  134. if (config.build.bundleAnalyzerReport) {
  135. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  136. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  137. }
  138. module.exports = webpackConfig