ant-design-pro config

config

  1. // ref: https://umijs.org/config/
  2. import os from 'os';
  3. import pageRoutes from './router.config';
  4. import defaultSettings from '../src/defaultSettings';
  5. import slash from 'slash2';
  6. const { pwa, primaryColor } = defaultSettings;
  7. const { APP_TYPE, TEST } = process.env;
  8. const plugins = [
  9. [
  10. 'umi-plugin-react',
  11. {
  12. antd: true,
  13. dva: {
  14. hmr: true,
  15. },
  16. locale: {
  17. enable: true, // default false
  18. default: 'zh-CN', // default zh-CN
  19. baseNavigator: true, // default true, when it is true, will use `navigator.language` overwrite default
  20. },
  21. dynamicImport: {
  22. loadingComponent: './components/PageLoading/index',
  23. webpackChunkName: true,
  24. level: 3,
  25. },
  26. pwa: pwa
  27. ? {
  28. workboxPluginMode: 'InjectManifest',
  29. workboxOptions: {
  30. importWorkboxFrom: 'local',
  31. },
  32. }
  33. : false,
  34. ...(!TEST && os.platform() === 'darwin'
  35. ? {
  36. dll: {
  37. include: ['dva', 'dva/router', 'dva/saga', 'dva/fetch'],
  38. exclude: ['@babel/runtime', 'netlify-lambda'],
  39. },
  40. hardSource: false,
  41. }
  42. : {}),
  43. },
  44. ],
  45. ];
  46. // 针对 preview.pro.ant.design GA 统计代码
  47. // 业务上不需要这个
  48. if (APP_TYPE === 'site') {
  49. plugins.push([
  50. 'umi-plugin-ga',
  51. {
  52. code: 'UA-72788897-6',
  53. },
  54. ]);
  55. }
  56. export default {
  57. plugins: [
  58. [
  59. 'umi-plugin-react',
  60. {
  61. antd: true,
  62. dva: {
  63. hmr: true,
  64. },
  65. targets: {
  66. ie: 11,
  67. },
  68. locale: {
  69. enable: true, // default false
  70. default: 'zh-CN', // default zh-CN
  71. baseNavigator: true, // default true, when it is true, will use `navigator.language` overwrite default
  72. },
  73. dynamicImport: {
  74. loadingComponent: './components/PageLoading/index',
  75. },
  76. },
  77. ],
  78. [
  79. 'umi-plugin-pro-block',
  80. {
  81. moveMock: false,
  82. moveService: false,
  83. modifyRequest: true,
  84. autoAddMenu: true,
  85. },
  86. ],
  87. ],
  88. define: {
  89. APP_TYPE: process.env.APP_TYPE || '',
  90. },
  91. targets: {
  92. ie: 11,
  93. },
  94. /**
  95. * 路由相关配置
  96. */
  97. routes: pageRoutes,
  98. /**
  99. * webpack 相关配置
  100. */
  101. // Theme for antd
  102. // https://ant.design/docs/react/customize-theme-cn
  103. theme: {
  104. 'primary-color': primaryColor,
  105. },
  106. externals: {
  107. '@antv/data-set': 'DataSet',
  108. },
  109. ignoreMomentLocale: true,
  110. lessLoaderOptions: {
  111. javascriptEnabled: true,
  112. },
  113. disableRedirectHoist: true,
  114. cssLoaderOptions: {
  115. modules: true,
  116. getLocalIdent: (context, localIdentName, localName) => {
  117. if (
  118. context.resourcePath.includes('node_modules') ||
  119. context.resourcePath.includes('ant.design.pro.less') ||
  120. context.resourcePath.includes('global.less')
  121. ) {
  122. return localName;
  123. }
  124. const match = context.resourcePath.match(/src(.*)/);
  125. if (match && match[1]) {
  126. const antdProPath = match[1].replace('.less', '');
  127. const arr = slash(antdProPath)
  128. .split('/')
  129. .map(a => a.replace(/([A-Z])/g, '-$1'))
  130. .map(a => a.toLowerCase());
  131. return `antd-pro${arr.join('-')}-${localName}`.replace(/--/g, '-');
  132. }
  133. return localName;
  134. },
  135. },
  136. manifest: {
  137. basePath: '/',
  138. },
  139. };

plugin.config

  1. // Change theme plugin
  2. import MergeLessPlugin from 'antd-pro-merge-less';
  3. import AntDesignThemePlugin from 'antd-theme-webpack-plugin';
  4. import path from 'path';
  5. function getModulePackageName(module) {
  6. if (!module.context) return null;
  7. const nodeModulesPath = path.join(__dirname, '../node_modules/');
  8. if (module.context.substring(0, nodeModulesPath.length) !== nodeModulesPath) {
  9. return null;
  10. }
  11. const moduleRelativePath = module.context.substring(nodeModulesPath.length);
  12. const [moduleDirName] = moduleRelativePath.split(path.sep);
  13. let packageName = moduleDirName;
  14. // handle tree shaking
  15. if (packageName.match('^_')) {
  16. // eslint-disable-next-line prefer-destructuring
  17. packageName = packageName.match(/^_(@?[^@]+)/)[1];
  18. }
  19. return packageName;
  20. }
  21. export default config => {
  22. // pro 开发环境再添加这个插件
  23. if (process.env.APP_TYPE === 'site' || process.env.NODE_ENV !== 'production') {
  24. // 将所有 less 合并为一个供 themePlugin使用
  25. const outFile = path.join(__dirname, '../.temp/ant-design-pro.less');
  26. const stylesDir = path.join(__dirname, '../src/');
  27. config.plugin('merge-less').use(MergeLessPlugin, [
  28. {
  29. stylesDir,
  30. outFile,
  31. },
  32. ]);
  33. config.plugin('ant-design-theme').use(AntDesignThemePlugin, [
  34. {
  35. antDir: path.join(__dirname, '../node_modules/antd'),
  36. stylesDir,
  37. varFile: path.join(__dirname, '../node_modules/antd/lib/style/themes/default.less'),
  38. mainLessFile: outFile, // themeVariables: ['@primary-color'],
  39. indexFileName: 'index.html',
  40. generateOne: true,
  41. lessUrl: 'https://gw.alipayobjects.com/os/lib/less.js/3.8.1/less.min.js',
  42. },
  43. ]);
  44. }
  45. // optimize chunks
  46. config.optimization
  47. .runtimeChunk(false) // share the same chunks across different modules
  48. .splitChunks({
  49. chunks: 'async',
  50. name: 'vendors',
  51. maxInitialRequests: Infinity,
  52. minSize: 0,
  53. cacheGroups: {
  54. vendors: {
  55. test: module => {
  56. const packageName = getModulePackageName(module);
  57. if (packageName) {
  58. return ['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0;
  59. }
  60. return false;
  61. },
  62. name(module) {
  63. const packageName = getModulePackageName(module);
  64. if (['bizcharts', '@antv_data-set'].indexOf(packageName) >= 0) {
  65. return 'viz'; // visualization package
  66. }
  67. return 'misc';
  68. },
  69. },
  70. },
  71. });
  72. };

router.config

  1. export default [
  2. //user
  3. {
  4. path: '/user',
  5. component: '../layouts/UserLayout',
  6. routes: [
  7. { path: '/user', redirect: '/user/login' },
  8. { path: '/user/login', name: 'login', component: './User/Login' },
  9. { path: '/user/register', name: 'register', component: './User/Register' },
  10. {
  11. path: '/user/register-result',
  12. name: 'register.result',
  13. component: './User/RegisterResult',
  14. },
  15. {
  16. component: '404',
  17. },
  18. ],
  19. },
  20. //app
  21. {
  22. path: '/',
  23. component: '../layouts/BasicLayout',
  24. Routes: ['src/pages/Authorized'],
  25. routes: [
  26. //dashboard
  27. { path: '/', redirect: '/dashboard/analysis', authority: ['admin', 'user'] },
  28. {
  29. path: '/dashboard',
  30. name: 'dashboard',
  31. icon: 'dashboard',
  32. routes: [
  33. {
  34. path: '/dashboard/analysis',
  35. name: 'analysis',
  36. component: './Dashboard/Analysis',
  37. },
  38. ],
  39. },
  40. ],
  41. },
  42. ]

pro-umi-config

config

  1. // ref: https://umijs.org/config/
  2. import os from 'os';
  3. import pageRoutes from './router.config';
  4. import defaultSettings from '../src/defaultSettings';
  5. const { pwa, primaryColor } = defaultSettings;
  6. const { APP_TYPE, TEST } = process.env;
  7. const plugins = [
  8. [
  9. 'umi-plugin-react',
  10. {
  11. antd: true,
  12. dva: {
  13. hmr: true,
  14. },
  15. locale: {
  16. enable: true, // default false
  17. default: 'zh-CN', // default zh-CN
  18. baseNavigator: true, // default true, when it is true, will use `navigator.language` overwrite default
  19. },
  20. dynamicImport: {
  21. loadingComponent: './components/PageLoading/index',
  22. webpackChunkName: true,
  23. level: 3,
  24. },
  25. pwa: pwa
  26. ? {
  27. workboxPluginMode: 'InjectManifest',
  28. workboxOptions: {
  29. importWorkboxFrom: 'local',
  30. },
  31. }
  32. : false,
  33. ...(!TEST && os.platform() === 'darwin'
  34. ? {
  35. dll: {
  36. include: ['dva', 'dva/router', 'dva/saga', 'dva/fetch'],
  37. exclude: ['@babel/runtime', 'netlify-lambda'],
  38. },
  39. hardSource: false,
  40. }
  41. : {}),
  42. },
  43. ],
  44. ];
  45. // 针对 preview.pro.ant.design 的 GA 统计代码
  46. // 业务上不需要这个
  47. if (APP_TYPE === 'site') {
  48. plugins.push([
  49. 'umi-plugin-ga',
  50. {
  51. code: 'UA-72788897-6',
  52. },
  53. ]);
  54. }
  55. export default {
  56. plugins: [
  57. [
  58. 'umi-plugin-react',
  59. {
  60. antd: true,
  61. dva: {
  62. hmr: true,
  63. },
  64. targets: {
  65. ie: 11,
  66. },
  67. locale: {
  68. enable: true, // default false
  69. default: 'zh-CN', // default zh-CN
  70. baseNavigator: true, // default true, when it is true, will use `navigator.language` overwrite default
  71. },
  72. dynamicImport: {
  73. loadingComponent: './components/PageLoading/index',
  74. },
  75. },
  76. ],
  77. [
  78. 'umi-plugin-pro-block',
  79. {
  80. moveMock: false,
  81. moveService: false,
  82. modifyRequest: true,
  83. autoAddMenu: true,
  84. },
  85. ],
  86. ], /**
  87. * webpack 相关配置
  88. */
  89. define: {
  90. APP_TYPE: process.env.APP_TYPE || '',
  91. },
  92. treeShaking: true,
  93. targets: {
  94. ie: 11,
  95. },
  96. // Theme for antd
  97. // https://ant.design/docs/react/customize-theme-cn
  98. //路由相关配置
  99. routes: pageRoutes,
  100. disableRedirectHoist: true,
  101. theme: {
  102. 'primary-color': primaryColor,
  103. },
  104. externals: {
  105. '@antv/data-set': 'DataSet',
  106. },
  107. ignoreMomentLocale: true,
  108. lessLoaderOptions: {
  109. javascriptEnabled: true,
  110. },
  111. };

pages/umi/routes

  1. import React from 'react';
  2. import { Router as DefaultRouter, Route, Switch } from 'react-router-dom';
  3. import dynamic from 'umi/dynamic';
  4. import renderRoutes from 'umi/lib/renderRoutes';
  5. import history from '@tmp/history';
  6. import RendererWrapper0 from '/Users/a1/github/prointeraction/pop3/src/pages/.umi/LocaleWrapper.jsx';
  7. import _dvaDynamic from 'dva/dynamic';
  8. let Router = require('dva/router').routerRedux.ConnectedRouter;
  9. let routes = [
  10. {
  11. path: '/user',
  12. component: __IS_BROWSER
  13. ? _dvaDynamic({
  14. component: () => import('../../layouts/UserLayout'),
  15. LoadingComponent: require('/Users/a1/github/prointeraction/pop3/src/components/PageLoading/index')
  16. .default,
  17. })
  18. : require('../../layouts/UserLayout').default,
  19. routes: [
  20. {
  21. path: '/user',
  22. redirect: '/user/login',
  23. exact: true,
  24. },
  25. {
  26. path: '/user/login',
  27. name: 'login',
  28. component: __IS_BROWSER
  29. ? _dvaDynamic({
  30. component: () => import('../User/Login'),
  31. LoadingComponent: require('/Users/a1/github/prointeraction/pop3/src/components/PageLoading/index')
  32. .default,
  33. })
  34. : require('../User/Login').default,
  35. exact: true,
  36. },
  37. {
  38. path: '/user/register',
  39. name: 'register',
  40. component: __IS_BROWSER
  41. ? _dvaDynamic({
  42. component: () => import('../User/Register'),
  43. LoadingComponent: require('/Users/a1/github/prointeraction/pop3/src/components/PageLoading/index')
  44. .default,
  45. })
  46. : require('../User/Register').default,
  47. exact: true,
  48. },
  49. {
  50. path: '/user/register-result',
  51. name: 'register.result',
  52. component: __IS_BROWSER
  53. ? _dvaDynamic({
  54. component: () => import('../User/RegisterResult'),
  55. LoadingComponent: require('/Users/a1/github/prointeraction/pop3/src/components/PageLoading/index')
  56. .default,
  57. })
  58. : require('../User/RegisterResult').default,
  59. exact: true,
  60. },
  61. {
  62. component: __IS_BROWSER
  63. ? _dvaDynamic({
  64. component: () => import('../404'),
  65. LoadingComponent: require('/Users/a1/github/prointeraction/pop3/src/components/PageLoading/index')
  66. .default,
  67. })
  68. : require('../404').default,
  69. exact: true,
  70. },
  71. {
  72. component: () =>
  73. React.createElement(
  74. require('/Users/a1/github/prointeraction/pop3/node_modules/umi-build-dev/lib/plugins/404/NotFound.js')
  75. .default,
  76. { pagesPath: 'src/pages', hasRoutesInConfig: true },
  77. ),
  78. },
  79. ],
  80. },
  81. {
  82. path: '/',
  83. component: __IS_BROWSER
  84. ? _dvaDynamic({
  85. component: () => import('../../layouts/BasicLayout'),
  86. LoadingComponent: require('/Users/a1/github/prointeraction/pop3/src/components/PageLoading/index')
  87. .default,
  88. })
  89. : require('../../layouts/BasicLayout').default,
  90. Routes: [require('../Authorized').default],
  91. routes: [
  92. {
  93. path: '/',
  94. redirect: '/dashboard/analysis',
  95. authority: ['admin', 'user'],
  96. exact: true,
  97. },
  98. {
  99. path: '/dashboard',
  100. name: 'dashboard',
  101. icon: 'dashboard',
  102. routes: [
  103. {
  104. path: '/dashboard/analysis',
  105. name: 'analysis',
  106. component: __IS_BROWSER
  107. ? _dvaDynamic({
  108. component: () => import('../Dashboard/Analysis'),
  109. LoadingComponent: require('/Users/a1/github/prointeraction/pop3/src/components/PageLoading/index')
  110. .default,
  111. })
  112. : require('../Dashboard/Analysis').default,
  113. exact: true,
  114. },
  115. {
  116. component: () =>
  117. React.createElement(
  118. require('/Users/a1/github/prointeraction/pop3/node_modules/umi-build-dev/lib/plugins/404/NotFound.js')
  119. .default,
  120. { pagesPath: 'src/pages', hasRoutesInConfig: true },
  121. ),
  122. },
  123. ],
  124. },
  125. {
  126. component: () =>
  127. React.createElement(
  128. require('/Users/a1/github/prointeraction/pop3/node_modules/umi-build-dev/lib/plugins/404/NotFound.js')
  129. .default,
  130. { pagesPath: 'src/pages', hasRoutesInConfig: true },
  131. ),
  132. },
  133. ],
  134. },
  135. {
  136. component: () =>
  137. React.createElement(
  138. require('/Users/a1/github/prointeraction/pop3/node_modules/umi-build-dev/lib/plugins/404/NotFound.js')
  139. .default,
  140. { pagesPath: 'src/pages', hasRoutesInConfig: true },
  141. ),
  142. },
  143. ];
  144. window.g_routes = routes;
  145. const plugins = require('umi/_runtimePlugin');
  146. plugins.applyForEach('patchRoutes', { initialValue: routes });
  147. export { routes };
  148. export default class RouterWrapper extends React.Component {
  149. unListen = () => {};
  150. constructor(props) {
  151. super(props);
  152. // route change handler
  153. function routeChangeHandler(location, action) {
  154. plugins.applyForEach('onRouteChange', {
  155. initialValue: {
  156. routes,
  157. location,
  158. action,
  159. },
  160. });
  161. }
  162. this.unListen = history.listen(routeChangeHandler);
  163. routeChangeHandler(history.location);
  164. }
  165. componentWillUnmount() {
  166. this.unListen();
  167. }
  168. render() {
  169. const props = this.props || {};
  170. return (
  171. <RendererWrapper0>
  172. <Router history={history}>{renderRoutes(routes, props)}</Router>
  173. </RendererWrapper0>
  174. );
  175. }
  176. }