webpack.prod.babel.js 4.2 KB

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