移动端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
5.8 KiB

  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 CompressionPlugin = require('compression-webpack-plugin');//引入gzip压缩插件
  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/[id].[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 UglifyJsPlugin({
  35. uglifyOptions: {
  36. compress: {
  37. warnings: false
  38. }
  39. },
  40. sourceMap: config.build.productionSourceMap,
  41. parallel: true
  42. }),
  43. new CompressionPlugin({
  44. filename: '[path].gz[query]', // 使得多个.gz文件合并成一个文件,这种方式压缩后的文件少,建议使用
  45. algorithm: 'gzip', // 官方默认压缩算法也是gzip
  46. test: /\.js$|\.css$|\.html$|\.ttf$|\.eot$|\.woff$/, // 使用正则给匹配到的文件做压缩,这里是给html、css、js以及字体(.ttf和.woff和.eot)做压缩
  47. threshold: 10240, //以字节为单位压缩超过此大小的文件,使用默认值10240吧
  48. minRatio: 0.8, // 最小压缩比率,官方默认0.8
  49. //是否删除原有静态资源文件,即只保留压缩后的.gz文件,建议这个置为false,还保留源文件。以防:
  50. deleteOriginalAssets: false
  51. }),
  52. // extract css into its own file
  53. new ExtractTextPlugin({
  54. filename: utils.assetsPath('css/[name].[contenthash].css'),
  55. // Setting the following option to `false` will not extract CSS from codesplit chunks.
  56. // Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
  57. // It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
  58. // increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
  59. allChunks: true,
  60. }),
  61. // Compress extracted CSS. We are using this plugin so that possible
  62. // duplicated CSS from different components can be deduped.
  63. new OptimizeCSSPlugin({
  64. cssProcessorOptions: config.build.productionSourceMap
  65. ? { safe: true, map: { inline: false } }
  66. : { safe: true }
  67. }),
  68. // generate dist index.html with correct asset hash for caching.
  69. // you can customize output by editing /index.html
  70. // see https://github.com/ampedandwired/html-webpack-plugin
  71. new HtmlWebpackPlugin({
  72. filename: config.build.index,
  73. template: 'index.html',
  74. inject: true,
  75. minify: {
  76. removeComments: true,
  77. collapseWhitespace: true,
  78. removeAttributeQuotes: true
  79. // more options:
  80. // https://github.com/kangax/html-minifier#options-quick-reference
  81. },
  82. // necessary to consistently work with multiple chunks via CommonsChunkPlugin
  83. chunksSortMode: 'dependency'
  84. }),
  85. // keep module.id stable when vendor modules does not change
  86. new webpack.HashedModuleIdsPlugin(),
  87. // enable scope hoisting
  88. new webpack.optimize.ModuleConcatenationPlugin(),
  89. // split vendor js into its own file
  90. new webpack.optimize.CommonsChunkPlugin({
  91. name: 'vendor',
  92. minChunks (module) {
  93. // any required modules inside node_modules are extracted to vendor
  94. return (
  95. module.resource &&
  96. /\.js$/.test(module.resource) &&
  97. module.resource.indexOf(
  98. path.join(__dirname, '../node_modules')
  99. ) === 0
  100. )
  101. }
  102. }),
  103. // extract webpack runtime and module manifest to its own file in order to
  104. // prevent vendor hash from being updated whenever app bundle is updated
  105. new webpack.optimize.CommonsChunkPlugin({
  106. name: 'manifest',
  107. minChunks: Infinity
  108. }),
  109. // This instance extracts shared chunks from code splitted chunks and bundles them
  110. // in a separate chunk, similar to the vendor chunk
  111. // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
  112. new webpack.optimize.CommonsChunkPlugin({
  113. name: 'app',
  114. async: 'vendor-async',
  115. children: true,
  116. minChunks: 3
  117. }),
  118. // copy custom static assets
  119. new CopyWebpackPlugin([
  120. {
  121. from: path.resolve(__dirname, '../static'),
  122. to: config.build.assetsSubDirectory,
  123. ignore: ['.*']
  124. }
  125. ])
  126. ]
  127. })
  128. if (config.build.productionGzip) {
  129. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  130. webpackConfig.plugins.push(
  131. new CompressionWebpackPlugin({
  132. asset: '[path].gz[query]',
  133. algorithm: 'gzip',
  134. test: new RegExp(
  135. '\\.(' +
  136. config.build.productionGzipExtensions.join('|') +
  137. ')$'
  138. ),
  139. threshold: 10240,
  140. minRatio: 0.8
  141. })
  142. )
  143. }
  144. if (config.build.bundleAnalyzerReport) {
  145. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  146. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  147. }
  148. module.exports = webpackConfig