Skip to content

Commit c955a13

Browse files
fix(antd): add missing configprovider prefixCls (#746)
* fix: add missing configprovider prefixCls * better * better Co-authored-by: chenshuai2144 <[email protected]>
1 parent 4dea6e4 commit c955a13

File tree

7 files changed

+70
-13
lines changed

7 files changed

+70
-13
lines changed

example/.umirc.ts

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ export default defineConfig({
6464
layout: {
6565
name: 'UMI 3',
6666
},
67+
antd: {
68+
config: {},
69+
},
6770
qiankun: {
6871
master: {
6972
apps: [

example/typing.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
declare module '*.css';
22
declare module '*.less';
3+
declare module '*.semver';

packages/plugin-antd/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"dependencies": {
3131
"antd": "^4.1.3",
32-
"antd-mobile": "^2.3.1"
32+
"antd-mobile": "^2.3.1",
33+
"semver": "^7.3.5"
3334
}
3435
}

packages/plugin-antd/src/index.ts

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { dirname, join } from 'path';
22
import { readFileSync } from 'fs';
33
import { IApi, utils } from 'umi';
44
import { ConfigProviderProps } from 'antd/es/config-provider';
5+
import semver from 'semver';
56

67
const { Mustache } = utils;
78

@@ -71,6 +72,14 @@ export default (api: IApi) => {
7172
});
7273
}
7374

75+
api.modifyDefaultConfig((config) => {
76+
config.theme = {
77+
'root-entry-name': 'default',
78+
...config.theme,
79+
};
80+
return config;
81+
});
82+
7483
api.addProjectFirstLibraries(() => {
7584
const imps = [
7685
{
@@ -94,10 +103,20 @@ export default (api: IApi) => {
94103
join(__dirname, 'runtime.tpl'),
95104
'utf-8',
96105
);
106+
107+
// 获取 antd 的版本号来判断应该是用什么api
108+
let version = '4.0.0';
109+
try {
110+
version = require(require.resolve('antd/package.json')).version;
111+
} catch (error) {}
112+
97113
api.writeTmpFile({
98114
path: 'plugin-antd/runtime.tsx',
99115
content: Mustache.render(runtimeTpl, {
100116
config: JSON.stringify(opts?.config),
117+
// @ts-ignore
118+
newAntd: semver.lt('4.13.0', version) || version === '4.13.0',
119+
oldAntd: semver.gt('4.13.0', version),
101120
}),
102121
});
103122
},

packages/plugin-antd/src/runtime.tpl

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
1-
import React from 'react';
2-
import { ConfigProvider, message } from 'antd';
3-
import { ApplyPluginsType } from 'umi';
4-
import { plugin } from '../core/umiExports';
1+
import React from "react";
2+
{{#newAntd}}
3+
import { ConfigProvider, message } from "antd";
4+
{{/newAntd}}
5+
6+
{{#oldAntd}}
7+
import { ConfigProvider, Modal, notification, message } from "antd";
8+
{{/oldAntd}}
9+
10+
import { ApplyPluginsType } from "umi";
11+
import { plugin } from "../core/umiExports";
512

613
export function rootContainer(container) {
714
const runtimeAntd = plugin.applyPlugins({
8-
key: 'antd',
15+
key: "antd",
916
type: ApplyPluginsType.modify,
1017
initialValue: {},
1118
});
1219

1320
const finalConfig = {...{{{ config }}},...runtimeAntd}
1421

1522
if (finalConfig.prefixCls) {
23+
{{#newAntd}}
24+
// 新版本的写法
25+
ConfigProvider.config({
26+
prefixCls: finalConfig.prefixCls,
27+
});
28+
{{/newAntd}}
29+
30+
{{#oldAntd}}
31+
// 老版本的antd需要这些写
32+
Modal.config({
33+
rootPrefixCls: finalConfig.prefixCls,
34+
});
35+
notification.config({
36+
prefixCls: `${finalConfig.prefixCls}-notification`,
37+
});
38+
{{/oldAntd}}
1639
message.config({
1740
prefixCls: `${finalConfig.prefixCls}-message`,
1841
});

typing.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
declare module 'semver';

yarn.lock

+16-7
Original file line numberDiff line numberDiff line change
@@ -3826,16 +3826,15 @@
38263826
through2 "^3.0.1"
38273827
vinyl "^2.2.0"
38283828

3829-
"@umijs/openapi@^1.1.6":
3830-
version "1.1.19"
3831-
resolved "https://registry.yarnpkg.com/@umijs/openapi/-/openapi-1.1.19.tgz#a40c9978d5a3b11139771320a34c7baa99a722e1"
3832-
integrity sha512-zXDrdaS//N4PAwrSHDP4D5s0n75dQQOzLYuAJXg9UAUScIuUwHorkzWZ0PpAfpXb+CBSG7gOjDHotncmpAC4Cw==
3829+
"@umijs/openapi@^1.2.0":
3830+
version "1.3.6"
3831+
resolved "https://registry.npmmirror.com/@umijs/openapi/download/@umijs/openapi-1.3.6.tgz?cache=0&sync_timestamp=1636949302168&other_urls=https%3A%2F%2Fregistry.npmmirror.com%2F%40umijs%2Fopenapi%2Fdownload%2F%40umijs%2Fopenapi-1.3.6.tgz#78af4d4ee4b05ed401832e7731e518d54551ca61"
3832+
integrity sha512-JPhJc9n1i0B4KqgCK6/tgvaGh+Zk5hvwV3NakRAGE7R4emfewAa+phVArSVbEPOjYt4Jzz7Wpzt6J31Q+Kttsg==
38333833
dependencies:
38343834
"@umijs/fabric" "^2.5.6"
38353835
dayjs "^1.10.3"
38363836
eslint "^7.18.0"
38373837
glob "^7.1.6"
3838-
lodash "^4.17.20"
38393838
memoizee "^0.4.15"
38403839
mock.js "^0.2.0"
38413840
mockjs "^1.1.0"
@@ -3906,7 +3905,7 @@
39063905
"@umijs/runtime" "3.5.13"
39073906
react-router-config "5.1.1"
39083907

3909-
"@umijs/route-utils@^1.0.35", "@umijs/route-utils@^1.0.7":
3908+
"@umijs/route-utils@^1.0.35":
39103909
version "1.0.37"
39113910
resolved "https://registry.yarnpkg.com/@umijs/route-utils/-/route-utils-1.0.37.tgz#1ceb516980d4754ce2d5959878fe72259091a990"
39123911
integrity sha512-RQYMYd0w3mKWlCvhUqYN2/dcCD8LxQWsr46DXpaLtqe2Y/8662qXA3E6moaFpyfokKJK6UHt2VxNGhkKPNJIgQ==
@@ -3916,6 +3915,16 @@
39163915
lodash.isequal "^4.5.0"
39173916
memoize-one "^5.1.1"
39183917

3918+
"@umijs/route-utils@^2.0.0":
3919+
version "2.0.4"
3920+
resolved "https://registry.npmmirror.com/@umijs/route-utils/download/@umijs/route-utils-2.0.4.tgz?cache=0&sync_timestamp=1637048932696&other_urls=https%3A%2F%2Fregistry.npmmirror.com%2F%40umijs%2Froute-utils%2Fdownload%2F%40umijs%2Froute-utils-2.0.4.tgz#951bf55c826d670c3acfff0566ceac63b7b74fea"
3921+
integrity sha512-YWxGHZhIMLrsiydu8P/v2SNuuqRfRbLaZ5mc1K9snTf6yQ+TEGFrpU8uLxp2iRBijMBD0CFyrWNuhlB/bvluUQ==
3922+
dependencies:
3923+
"@qixian.cs/path-to-regexp" "^6.1.0"
3924+
fast-deep-equal "^3.1.3"
3925+
lodash.isequal "^4.5.0"
3926+
memoize-one "^5.1.1"
3927+
39193928
39203929
version "3.5.13"
39213930
resolved "https://registry.yarnpkg.com/@umijs/runtime/-/runtime-3.5.13.tgz#fd37279cad39e0f953bd982d3bb3ffb221b701e3"
@@ -11635,7 +11644,7 @@ lodash.zip@^4.2.0:
1163511644
resolved "https://registry.yarnpkg.com/lodash.zip/-/lodash.zip-4.2.0.tgz#ec6662e4896408ed4ab6c542a3990b72cc080020"
1163611645
integrity sha1-7GZi5IlkCO1KtsVCo5kLcswIACA=
1163711646

11638-
[email protected], lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.20, lodash@^4.17.21, lodash@^4.17.4, lodash@^4.2.1, lodash@^4.7.0:
11647+
[email protected], lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.19, lodash@^4.17.21, lodash@^4.17.4, lodash@^4.2.1, lodash@^4.7.0:
1163911648
version "4.17.21"
1164011649
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
1164111650
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

0 commit comments

Comments
 (0)