config.js 3.7 KB

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