Skip to content

Commit 2cbf82a

Browse files
author
徐远翔
committed
fix: 替换@umijs/renderer-react,解决RN Fast Refresh 触发时 umi runtime plugin: rootContainer 未执行的问题
1 parent 76a66a2 commit 2cbf82a

File tree

9 files changed

+83
-38
lines changed

9 files changed

+83
-38
lines changed

CHANGELOG.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
## <a target="_blank" href="https://github.com/xuyuanxiang/umi-react-native/compare/v0.2.0-alpha.2...v"></a> (2020-05-18)
1+
## <a target="_blank" href="https://github.com/xuyuanxiang/umi-react-native/compare/v0.2.0-alpha.3...v"></a> (2020-05-18)
2+
3+
## <a target="_blank" href="https://github.com/xuyuanxiang/umi-react-native/compare/v0.2.0-alpha.2...v0.2.0-alpha.3">0.2.0-alpha.3</a> (2020-05-18)
4+
5+
6+
### 新功能
7+
8+
* React Naitve CLI hooks supporting (<a target="_blank" href="https://github.com/xuyuanxiang/umi-react-native/commit/3065d12">3065d12</a>)
9+
10+
11+
212

313
## <a target="_blank" href="https://github.com/xuyuanxiang/umi-react-native/compare/v0.2.0-alpha.1...v0.2.0-alpha.2">0.2.0-alpha.2</a> (2020-05-18)
414

packages/umi-preset-react-native/hooks/prebundle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const { execFileSync } = require('child_process');
66
const cwd = process.cwd();
77
const umi = join(cwd, 'node_modules', 'umi', 'bin', 'umi.js');
88

9-
const child = execFileSync(umi, ['g', 'rn', '--dev'], { cwd, stdio: 'inherit' });
9+
const child = execFileSync(umi, ['g', 'rn'], { cwd, stdio: 'inherit' });
1010

1111
child.on('close', (code) => {
1212
process.exit(code);

packages/umi-preset-react-native/package.json

-6
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,5 @@
4747
"babel-plugin-module-resolver": "^4.0.0",
4848
"memoizerific": "^1.11.3",
4949
"react-router-native": "5.1.2"
50-
},
51-
"rnpm": {
52-
"hooks": {
53-
"prestart": "./hooks/prestart.js",
54-
"prebundle": "./hooks/prebundle.js"
55-
}
5650
}
5751
}

packages/umi-preset-react-native/react-native.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
module.exports = {
22
dependency: {
3+
platforms: {
4+
ios: {},
5+
android: {},
6+
},
37
hooks: {
48
prestart: './hooks/prestart.js',
59
prebundle: './hooks/prebundle.js',

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

+23-11
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@ if (global.window === undefined) {
66
global.window = global;
77
}
88
9-
// hot module replacement
10-
// @ts-ignore
11-
if (module.hot) {
12-
// @ts-ignore
13-
const accept = module.hot.accept;
14-
// @ts-ignore
15-
module.hot.accept = (path, fn) => {
16-
accept(fn);
17-
};
18-
}
19-
209
`;
2110

2211
export default (api: IApi) => {
@@ -32,6 +21,29 @@ export default (api: IApi) => {
3221
return memo;
3322
});
3423

24+
api.addEntryCode(
25+
() => `// @ts-ignore
26+
let accept: unknown;
27+
// @ts-ignore
28+
if (module.hot) {
29+
// @ts-ignore
30+
if (!accept) {
31+
accept = module.hot.accept;
32+
}
33+
// @ts-ignore
34+
module.hot.accept = function (path, fn) {
35+
console.info('path=', path, 'fn=', fn);
36+
if (typeof path === 'function' && typeof accept === 'function') {
37+
accept(path);
38+
}
39+
if (typeof fn === 'function' && typeof accept === 'function') {
40+
accept(fn);
41+
}
42+
};
43+
}
44+
`,
45+
);
46+
3547
api.onGenerateFiles(() => {
3648
api.writeTmpFile({
3749
path: 'react-native/polyfill.ts',

packages/umi-preset-react-navigation/src/plugins/generateFiles/react-navigation/runtime.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,21 @@ export default (api: IApi) => {
5050

5151
api.addRuntimePlugin(() => [join(api.paths.absTmpPath || '', 'react-navigation', 'runtime')]);
5252

53+
api.modifyRendererPath(() =>
54+
winPath(
55+
getUserLibDir(
56+
'umi-renderer-react-navigation/package.json',
57+
dirname(require.resolve('umi-renderer-react-navigation/package.json')),
58+
true,
59+
),
60+
),
61+
);
62+
5363
api.onGenerateFiles(() => {
5464
const dynamicImport = api.config.dynamicImport;
5565
api.writeTmpFile({
5666
path: 'react-navigation/runtime.tsx',
5767
content: Mustache.render(runtimeTpl, {
58-
rendererPath: winPath(
59-
getUserLibDir(
60-
'umi-renderer-react-navigation/package.json',
61-
dirname(require.resolve('umi-renderer-react-navigation/package.json')),
62-
true,
63-
),
64-
),
6568
reactNavigationPath: winPath(
6669
getUserLibDir(
6770
'@react-navigation/native/package.json',
@@ -74,8 +77,6 @@ export default (api: IApi) => {
7477
? `require('${dynamicImport.loading}').default`
7578
: '',
7679
theme: JSON.stringify(api.config?.reactNavigation?.theme || {}, null, 2),
77-
enableTitle: api.config.title !== false,
78-
defaultTitle: api.config.title || '',
7980
}),
8081
});
8182
});

packages/umi-preset-react-navigation/src/plugins/generateFiles/react-navigation/runtimeTpl.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
export default `import React from 'react';
22
{{^loading}}import {View, Text} from 'react-native';{{/loading}}
33
import {ApplyPluginsType, dynamic} from 'umi';
4-
import {Navigation} from '{{{ rendererPath }}}';
54
import {NavigationContainer} from '{{{ reactNavigationPath }}}';
65
7-
export function rootContainer(container, {plugin, routes, history}) {
6+
export function rootContainer(container, {plugin}) {
87
return React.createElement(
98
dynamic({
109
loading: plugin.applyPlugins({
@@ -43,14 +42,7 @@ export function rootContainer(container, {plugin, routes, history}) {
4342
});
4443
},
4544
},
46-
React.createElement(Navigation, {
47-
routes,
48-
history,
49-
plugin,
50-
{{#enableTitle}}
51-
defaultTitle: '{{{ defaultTitle }}}',
52-
{{/enableTitle}}
53-
}),
45+
container,
5446
);
5547
},
5648
}),
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './createHistoryNavigator';
22
export * from './Navigation';
3+
export * from './renderClient';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react';
2+
import { ApplyPluginsType, Plugin } from 'umi';
3+
import { IRoute } from '@umijs/renderer-react';
4+
import { Navigation } from './Navigation';
5+
6+
interface IRouterComponentProps {
7+
routes: IRoute[];
8+
plugin: Plugin;
9+
history: any;
10+
ssrProps?: object;
11+
defaultTitle?: string;
12+
}
13+
14+
interface IOpts extends IRouterComponentProps {
15+
rootElement?: string | HTMLElement;
16+
}
17+
18+
export function renderClient(opts: IOpts) {
19+
return opts.plugin.applyPlugins({
20+
type: ApplyPluginsType.modify,
21+
key: 'rootContainer',
22+
initialValue: (
23+
<Navigation history={opts.history} routes={opts.routes} plugin={opts.plugin} defaultTitle={opts.defaultTitle} />
24+
),
25+
args: {
26+
history: opts.history,
27+
routes: opts.routes,
28+
plugin: opts.plugin,
29+
},
30+
});
31+
}

0 commit comments

Comments
 (0)