Skip to content

Commit 563e40f

Browse files
author
徐远翔
committed
fix: 在开发模式下添加polyfill,为react-router-native的Link组件修正propTypes定义错误的问题。
1 parent 5f6049b commit 563e40f

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

packages/umi-preset-react-native/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default function () {
55
require.resolve('./plugins/features/haul'),
66
require.resolve('./plugins/features/reactNative'),
77
require.resolve('./plugins/generateFiles/react-native/exports'),
8+
require.resolve('./plugins/generateFiles/react-native/globals'),
89
require.resolve('./plugins/generateFiles/react-native/loading'),
910
require.resolve('./plugins/generateFiles/react-native/polyfill'),
1011
require.resolve('./plugins/generateFiles/react-native/routes'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { IApi } from 'umi';
2+
3+
const TPL = `{{{ globals }}}
4+
5+
`;
6+
7+
export default (api: IApi) => {
8+
const {
9+
utils: { Mustache, lodash },
10+
} = api;
11+
api.addPolyfillImports(() => {
12+
if (lodash.isPlainObject(api.config.define) && !lodash.isEmpty(api.config.define)) {
13+
return [
14+
{
15+
source: './react-native/globals',
16+
},
17+
];
18+
}
19+
return [];
20+
});
21+
api.onGenerateFiles(() => {
22+
if (lodash.isPlainObject(api.config.define) && !lodash.isEmpty(api.config.define)) {
23+
api.writeTmpFile({
24+
path: 'react-native/globals.ts',
25+
content: Mustache.render(TPL, {
26+
globals: lodash
27+
.keysIn(api.config.define)
28+
.map(
29+
(key) =>
30+
`// @ts-ignore\nglobal['${key}'] = ${JSON.stringify(api.config.define && api.config.define[key])};`,
31+
)
32+
.join('\n'),
33+
}),
34+
});
35+
}
36+
});
37+
};

packages/umi-preset-react-native/src/plugins/generateFiles/react-native/polyfill.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ export default (api: IApi) => {
2222
});
2323

2424
api.addEntryCodeAhead(() => {
25-
if (api.config.define && typeof api.config.define === 'object') {
26-
const keys = Object.keys(api.config.define);
27-
return keys
28-
.map((key) => `global['${key}'] = ${JSON.stringify(api.config.define && api.config.define[key])};`)
29-
.join('\n');
25+
if (process.env.NODE_ENV === 'development') {
26+
return `if (__DEV__) {
27+
const PropsTypes = require('prop-types');
28+
require('@umijs/runtime').Link.propTypes.component = PropsTypes.oneOfType([PropsTypes.func, PropsTypes.elementType]);
29+
}`;
3030
}
3131
return '';
3232
});

0 commit comments

Comments
 (0)