webpack.prod.babel.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Important modules this config uses
  2. const path = require('path')
  3. const HtmlWebpackPlugin = require('html-webpack-plugin')
  4. const { HashedModuleIdsPlugin } = require('webpack')
  5. const TerserPlugin = require('terser-webpack-plugin')
  6. const CompressionPlugin = require('compression-webpack-plugin')
  7. const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
  8. const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  9. module.exports = require('./webpack.base.babel')({
  10. mode: 'production',
  11. // In production, we skip all hot-reloading stuff
  12. entry: {
  13. app: [
  14. path.join(process.cwd(), 'app/app.tsx')
  15. ],
  16. share: [
  17. path.join(process.cwd(), 'share/app.tsx')
  18. ]
  19. },
  20. // Utilize long-term caching by adding content hashes (not compilation hashes) to compiled assets
  21. output: {
  22. filename: '[name].[chunkhash].js',
  23. chunkFilename: '[name].[chunkhash].chunk.js'
  24. },
  25. tsLoaders: [{
  26. loader: 'babel-loader'
  27. }],
  28. optimization: {
  29. minimize: true,
  30. minimizer: [
  31. new TerserPlugin({
  32. terserOptions: {
  33. warnings: false,
  34. compress: {
  35. comparisons: false
  36. },
  37. parse: {},
  38. mangle: true,
  39. output: {
  40. comments: false,
  41. ascii_only: true
  42. }
  43. },
  44. parallel: true,
  45. cache: true,
  46. sourceMap: true
  47. })
  48. ],
  49. nodeEnv: 'production',
  50. sideEffects: true,
  51. concatenateModules: true,
  52. splitChunks: {
  53. // chunks: 'all',
  54. chunks: 'async',
  55. minSize: 30000,
  56. minChunks: 1,
  57. maxAsyncRequests: 5,
  58. maxInitialRequests: 3,
  59. name: true,
  60. cacheGroups: {
  61. vendors: {
  62. test: /[\\/]node_modules[\\/](?!antd|jquery|three|bootstrap-datepicker)(.[a-zA-Z0-9.\-_]+)[\\/]/,
  63. // test: /[\\/]node_modules[\\/]/,
  64. name: 'vendor',
  65. chunks: 'all'
  66. }
  67. // main: {
  68. // chunks: 'all',
  69. // minChunks: 2,
  70. // reuseExistingChunk: true,
  71. // enforce: true
  72. // }
  73. }
  74. },
  75. runtimeChunk: true
  76. },
  77. plugins: [
  78. // Minify and optimize the index.html
  79. new HtmlWebpackPlugin({
  80. filename: 'index.html',
  81. chunks: ['app', 'runtime~app', 'app~share', 'vendor'],
  82. template: 'app/index.html',
  83. minify: {
  84. removeComments: true,
  85. collapseWhitespace: true,
  86. removeRedundantAttributes: true,
  87. useShortDoctype: true,
  88. removeEmptyAttributes: true,
  89. removeStyleLinkTypeAttributes: true,
  90. keepClosingSlash: true,
  91. minifyJS: true,
  92. minifyCSS: true,
  93. minifyURLs: true
  94. },
  95. inject: true
  96. }),
  97. new HtmlWebpackPlugin({
  98. filename: 'share.html',
  99. chunks: ['share', 'runtime~share', 'app~share', 'vendor'],
  100. template: 'app/index.html',
  101. minify: {
  102. removeComments: true,
  103. collapseWhitespace: true,
  104. removeRedundantAttributes: true,
  105. useShortDoctype: true,
  106. removeEmptyAttributes: true,
  107. removeStyleLinkTypeAttributes: true,
  108. keepClosingSlash: true,
  109. minifyJS: true,
  110. minifyCSS: true,
  111. minifyURLs: true
  112. },
  113. inject: true
  114. }),
  115. new CompressionPlugin({
  116. algorithm: 'gzip',
  117. test: /\.js$|\.css$|\.html$/,
  118. threshold: 10240,
  119. minRatio: 0.8
  120. }),
  121. new HashedModuleIdsPlugin({
  122. hashFunction: 'sha256',
  123. hashDigest: 'hex',
  124. hashDigestLength: 20
  125. }),
  126. new CaseSensitivePathsPlugin(),
  127. new BundleAnalyzerPlugin({
  128. analyzerMode: 'server',
  129. analyzerHost: 'localhost',
  130. analyzerPort: 5000,
  131. reportFilename: 'report.html',
  132. defaultSizes: 'parsed',
  133. openAnalyzer: true,
  134. generateStatsFile: true,
  135. statsFilename: '../stats.json',
  136. statsOptions: null,
  137. logLevel: 'info'
  138. })
  139. ],
  140. performance: {
  141. assetFilter: assetFilename =>
  142. !/(\.map$)|(^(main\.|favicon\.))/.test(assetFilename)
  143. },
  144. htmlWebpackPlugin: {
  145. files: {
  146. js: ['app.js', 'share.js'],
  147. chunks: {
  148. app: {
  149. entry: 'app.js'
  150. },
  151. share: {
  152. entry: 'share.js'
  153. }
  154. }
  155. }
  156. }
  157. })