config.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // https://umijs.org/config/
  2. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  3. const AntdDayjsWebpackPlugin = require('antd-dayjs-webpack-plugin')
  4. const prodGzipList = ['js', 'css']
  5. import { defineConfig } from 'umi'
  6. import defaultSettings from './defaultSettings'
  7. import proxy from './proxy'
  8. import routes from './routes'
  9. import windicss from 'windicss-webpack-plugin/dist/index'
  10. const { REACT_APP_ENV } = process.env
  11. export default defineConfig({
  12. hash: true,
  13. antd: {},
  14. dva: {
  15. hmr: true
  16. },
  17. history: {
  18. type: 'browser'
  19. },
  20. locale: {
  21. // default zh-CN
  22. default: 'zh-CN',
  23. antd: false,
  24. // default true, when it is true, will use `navigator.language` overwrite default
  25. baseNavigator: false
  26. },
  27. dynamicImport: {
  28. loading: '@/components/PageLoading/index'
  29. },
  30. targets: {
  31. ie: 11
  32. },
  33. terserOptions: {
  34. compress: {
  35. drop_console: REACT_APP_ENV === 'prod' ? true : false,
  36. drop_debugger: REACT_APP_ENV === 'prod' ? true : false
  37. }
  38. },
  39. // umi routes: https://umijs.org/docs/routing
  40. routes,
  41. // Theme for antd: https://ant.design/docs/react/customize-theme-cn
  42. theme: {
  43. 'primary-color': defaultSettings.primaryColor
  44. },
  45. title: false,
  46. ignoreMomentLocale: true,
  47. proxy: proxy[REACT_APP_ENV || 'dev'],
  48. manifest: {
  49. basePath: '/'
  50. },
  51. exportStatic: {},
  52. esbuild: {},
  53. fastRefresh: {},
  54. nodeModulesTransform: {
  55. type: 'none' // 提升编译速度加速,加速
  56. },
  57. extraBabelPlugins: [
  58. '@babel/plugin-proposal-nullish-coalescing-operator',
  59. '@babel/plugin-proposal-optional-chaining',
  60. [
  61. 'import',
  62. {
  63. libraryName: '@icon-park/react',
  64. libraryDirectory: 'es/icons',
  65. camel2DashComponentName: false
  66. }
  67. ]
  68. ],
  69. chainWebpack(config) {
  70. config.plugin('windicss').use(windicss)
  71. config.plugin('antd-dayjs-webpack-plugin').use(AntdDayjsWebpackPlugin)
  72. if (REACT_APP_ENV === 'prod') {
  73. config.merge({
  74. optimization: {
  75. minimize: true,
  76. splitChunks: {
  77. chunks: 'async',
  78. minSize: 30000,
  79. minChunks: 2,
  80. automaticNameDelimiter: '.',
  81. cacheGroups: {
  82. vendor: {
  83. name: 'vendors',
  84. test: /^.*node_modules[\\/](!lodash|antd).*$/,
  85. chunks: 'all',
  86. priority: 10
  87. },
  88. antd: {
  89. name: 'antd',
  90. test: /[\\/]node_modules[\\/]antd[\\/]/,
  91. chunks: 'all',
  92. priority: 9
  93. },
  94. lodash: {
  95. name: 'lodash',
  96. test: /[\\/]node_modules[\\/]lodash[\\/]/,
  97. chunks: 'all',
  98. priority: -2
  99. }
  100. }
  101. }
  102. }
  103. })
  104. config.plugin('compression-webpack-plugin').use(
  105. new CompressionWebpackPlugin({
  106. // filename: 文件名称,这里我们不设置,让它保持和未压缩的文件同一个名称
  107. algorithm: 'gzip', // 指定生成gzip格式
  108. test: new RegExp('\\.(' + prodGzipList.join('|') + ')$'), // 匹配哪些格式文件需要压缩
  109. threshold: 5000, //对超过10k的数据进行压缩
  110. minRatio: 0.6 // 压缩比例,值为0 ~ 1
  111. })
  112. )
  113. }
  114. //过滤掉momnet的那些不使用的国际化文件
  115. config
  116. .plugin('replace')
  117. .use(require('webpack').ContextReplacementPlugin)
  118. .tap(() => {
  119. return [/moment[/\\]locale$/, /zh-cn/]
  120. })
  121. }
  122. })