webpack.prod.conf.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 = require('../config/prod.env')
  14. const webpackConfig = merge(baseWebpackConfig, {
  15. module: {
  16. rules: utils.styleLoaders({
  17. sourceMap: config.build.productionSourceMap,
  18. extract: true,
  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. new webpack.DefinePlugin({
  30. 'process.env': env
  31. }),
  32. new UglifyJsPlugin({
  33. uglifyOptions: {
  34. compress: {
  35. warnings: false
  36. }
  37. },
  38. sourceMap: config.build.productionSourceMap,
  39. parallel: true
  40. }),
  41. new ExtractTextPlugin({
  42. filename: utils.assetsPath('css/[name].[contenthash].css'),
  43. allChunks: true,
  44. }),
  45. new OptimizeCSSPlugin({
  46. cssProcessorOptions: config.build.productionSourceMap
  47. ? { safe: true, map: { inline: false } }
  48. : { safe: true }
  49. }),
  50. // 主应用 HTML
  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. },
  60. chunksSortMode: 'dependency',
  61. chunks: ['manifest', 'vendor', 'app']
  62. }),
  63. // workPage 独立页面
  64. new HtmlWebpackPlugin({
  65. filename: 'workPage.html',
  66. template: 'public/workPage.html',
  67. inject: true,
  68. minify: {
  69. removeComments: true,
  70. collapseWhitespace: true,
  71. removeAttributeQuotes: true
  72. },
  73. chunksSortMode: 'dependency',
  74. chunks: ['workPage-manifest', 'workPage-vendor', 'workPage']
  75. }),
  76. new webpack.HashedModuleIdsPlugin(),
  77. new webpack.optimize.ModuleConcatenationPlugin(),
  78. // 关键:正确的 CommonsChunkPlugin 顺序
  79. // 1. 先提取主应用的 vendor
  80. new webpack.optimize.CommonsChunkPlugin({
  81. name: 'vendor',
  82. chunks: ['app'],
  83. minChunks: function (module) {
  84. return (
  85. module.resource &&
  86. /\.js$/.test(module.resource) &&
  87. module.resource.indexOf(path.join(__dirname, '../node_modules')) === 0
  88. )
  89. }
  90. }),
  91. // 2. 再提取 workPage 的 vendor
  92. new webpack.optimize.CommonsChunkPlugin({
  93. name: 'workPage-vendor',
  94. chunks: ['workPage'],
  95. minChunks: function (module) {
  96. return (
  97. module.resource &&
  98. /\.js$/.test(module.resource) &&
  99. module.resource.indexOf(path.join(__dirname, '../node_modules')) === 0
  100. )
  101. }
  102. }),
  103. // 3. 主应用的 manifest(必须在 vendor 之后)
  104. new webpack.optimize.CommonsChunkPlugin({
  105. name: 'manifest',
  106. chunks: ['vendor'],
  107. minChunks: Infinity
  108. }),
  109. // 4. workPage 的 manifest(必须在 workPage-vendor 之后)
  110. new webpack.optimize.CommonsChunkPlugin({
  111. name: 'workPage-manifest',
  112. chunks: ['workPage-vendor'],
  113. minChunks: Infinity
  114. }),
  115. new CopyWebpackPlugin([
  116. {
  117. from: path.resolve(__dirname, '../static'),
  118. to: config.build.assetsSubDirectory,
  119. ignore: ['.*']
  120. }
  121. ])
  122. ]
  123. })
  124. if (config.build.productionGzip) {
  125. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  126. webpackConfig.plugins.push(
  127. new CompressionWebpackPlugin({
  128. asset: '[path].gz[query]',
  129. algorithm: 'gzip',
  130. test: new RegExp(
  131. '\\.(' +
  132. config.build.productionGzipExtensions.join('|') +
  133. ')$'
  134. ),
  135. threshold: 10240,
  136. minRatio: 0.8
  137. })
  138. )
  139. }
  140. if (config.build.bundleAnalyzerReport) {
  141. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  142. webpackConfig.plugins.push(new BundleAnalyzerPlugin())
  143. }
  144. module.exports = webpackConfig