|
1 | 1 | import { IApi } from 'umi';
|
2 |
| -import { dirname, join } from 'path'; |
3 |
| -import { existsSync, readFileSync } from 'fs'; |
| 2 | +import { join } from 'path'; |
| 3 | +import { readFileSync } from 'fs'; |
4 | 4 | import { EOL } from 'os';
|
5 |
| -import { assertExists } from '../../utils'; |
| 5 | +import { assertExists, getUserLib } from '../../utils'; |
6 | 6 |
|
7 | 7 | export default (api: IApi) => {
|
8 | 8 | const {
|
9 |
| - utils: { resolve, lodash, winPath }, |
10 |
| - paths: { absNodeModulesPath = '', absSrcPath = '' }, |
| 9 | + utils: { lodash, winPath }, |
| 10 | + paths: { absSrcPath = '' }, |
11 | 11 | } = api;
|
12 | 12 |
|
13 |
| - /** |
14 |
| - * 优先读取用户目录下依赖的绝对路径 |
15 |
| - * @param library 比如:'react-native'(目录) 或者 'react-router/esm/index.js'(文件) |
16 |
| - * @param defaults library找不到时的缺省值 |
17 |
| - * @param dir true-返回目录绝对路径,false-返回文件绝对路径 |
18 |
| - * @param basedir 用户目录查找起始路径 |
19 |
| - */ |
20 |
| - function getUserLibDir(library: string, defaults: string, dir: boolean = false, basedir = absSrcPath): string { |
21 |
| - try { |
22 |
| - const path = resolve.sync(library, { |
23 |
| - basedir, |
24 |
| - }); |
25 |
| - if (dir) { |
26 |
| - return dirname(path); |
27 |
| - } else { |
28 |
| - return path; |
29 |
| - } |
30 |
| - } catch (ignored) {} |
31 |
| - return defaults; |
32 |
| - } |
| 13 | + const REACT_NATIVE_PATH = getUserLib({ |
| 14 | + api, |
| 15 | + target: 'react-native/package.json', |
| 16 | + dir: true, |
| 17 | + }); |
33 | 18 |
|
34 |
| - const REACT_NATIVE_PATH = getUserLibDir( |
35 |
| - join('react-native', 'package.json'), |
36 |
| - join(absNodeModulesPath, 'react-native'), |
37 |
| - true, |
38 |
| - ); |
39 |
| - const EXPO_PATH = getUserLibDir(join('expo', 'package.json'), join(absNodeModulesPath, 'expo')); |
40 |
| - const HAUL_CLI_PATH = getUserLibDir(join('@haul-bundler', 'cli', 'package.json'), join(absNodeModulesPath, 'expo')); |
41 | 19 | assertExists(REACT_NATIVE_PATH);
|
42 | 20 |
|
43 |
| - const isExpo = existsSync(EXPO_PATH); |
44 |
| - const isHaul = existsSync(HAUL_CLI_PATH); |
45 |
| - |
46 | 21 | const { version } = require(join(REACT_NATIVE_PATH, 'package.json'));
|
47 | 22 |
|
48 | 23 | let appKey;
|
49 | 24 | try {
|
50 |
| - if (!isExpo) { |
51 |
| - const appJson = JSON.parse( |
52 |
| - readFileSync(getUserLibDir('app.json', join(absSrcPath, 'app.json'), false, absSrcPath), 'utf8'), |
53 |
| - ); |
54 |
| - appKey = appJson.name; |
55 |
| - } |
| 25 | + const appJson = JSON.parse(readFileSync(join(absSrcPath, 'app.json'), 'utf8')); |
| 26 | + appKey = appJson.name; |
56 | 27 | } catch (ignored) {}
|
57 | 28 |
|
58 | 29 | // umi-preset-react-native 扩展配置
|
59 | 30 | api.describe({
|
60 | 31 | key: 'reactNative',
|
61 | 32 | config: {
|
62 |
| - default: { appKey, version, bundler: isExpo ? 'expo' : isHaul ? 'haul' : 'react-native-cli' }, |
| 33 | + default: { appKey, version }, |
63 | 34 | schema(joi) {
|
64 | 35 | return joi
|
65 | 36 | .object({
|
66 | 37 | appKey: joi.string(), // moduleName app.json#name
|
67 | 38 | version: joi.string(), // RN 版本号
|
68 |
| - bundler: joi.allow('expo', 'react-native-cli', 'haul'), |
69 | 39 | })
|
70 | 40 | .optional();
|
71 | 41 | },
|
@@ -97,16 +67,13 @@ export default (api: IApi) => {
|
97 | 67 | },
|
98 | 68 | {
|
99 | 69 | name: 'react-router-native',
|
100 |
| - path: getUserLibDir( |
101 |
| - 'react-router-native', |
102 |
| - winPath(dirname(require.resolve('react-router-native/package.json'))), |
103 |
| - true, |
104 |
| - ), |
| 70 | + path: winPath(getUserLib({ api, target: 'react-router-native/package.json', dir: true })), |
105 | 71 | },
|
106 | 72 | ]);
|
107 | 73 |
|
108 | 74 | // 启动时检查
|
109 | 75 | api.onStart(() => {
|
| 76 | + const isExpo = Boolean(api.config?.expo); |
110 | 77 | if (!isExpo) {
|
111 | 78 | // 使用 haul 和 react-native-cli 时,appKey 一定不能为空,否则 RN 引用没法注册/部署/启动。
|
112 | 79 | if (!api.config?.reactNative?.appKey) {
|
@@ -136,11 +103,15 @@ export default (api: IApi) => {
|
136 | 103 | throw new TypeError('"history.type" 配置错误');
|
137 | 104 | }
|
138 | 105 |
|
139 |
| - if (api.config.dynamicImport && !api.config.dynamicImport.loading) { |
140 |
| - api.logger.error( |
141 |
| - `在 RN 环境中启用"dynamicImport"功能时,必须实现自定义的"loading"!${EOL}因为 umi 默认 loading 使用了 HTML 标签,在 RN 中运行会报错!${EOL}查看如何配置自定义 loading:https://umijs.org/config#dynamicimport`, |
142 |
| - ); |
143 |
| - throw new TypeError('"dynamicImport.loading" 未配置'); |
| 106 | + if (api.config.dynamicImport) { |
| 107 | + api.logger.error('在 RN 环境中暂不支持:"dynamicImport"功能。'); |
| 108 | + throw new TypeError('在 RN 环境中暂不支持:"dynamicImport"功能。'); |
144 | 109 | }
|
| 110 | + // if (api.config.dynamicImport && !api.config.dynamicImport.loading) { |
| 111 | + // api.logger.error( |
| 112 | + // `在 RN 环境中启用"dynamicImport"功能时,必须实现自定义的"loading"!${EOL}因为 umi 默认 loading 使用了 HTML 标签,在 RN 中运行会报错!${EOL}查看如何配置自定义 loading:https://umijs.org/config#dynamicimport`, |
| 113 | + // ); |
| 114 | + // throw new TypeError('"dynamicImport.loading" 未配置'); |
| 115 | + // } |
145 | 116 | });
|
146 | 117 | };
|
0 commit comments