Skip to content

Commit c2f1435

Browse files
author
xuyuanxiang@home
committed
refactor: 去除第三方haul打包器,换为 RN 官方的 metro。
原因:haul 不支持 Fast Refresh、Live Reloading功能。 BREAKING CHANGE: 去除`umi dev-rn`和`umi dev-build`命令,不再支持chainWebpack配置。新增`umi watch`命令。
1 parent 8ef86fc commit c2f1435

File tree

19 files changed

+269
-492
lines changed

19 files changed

+269
-492
lines changed

README.md

+21-152
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
| NPM 包 | 当前版本 | 简介 |
1111
| --- | --- | --- |
1212
| [umi-plugin-antd-react-native](packages/umi-plugin-antd-react-native) | [![npm version](https://img.shields.io/npm/v/umi-plugin-antd-react-native.svg?style=flat-square)](https://www.npmjs.com/package/umi-plugin-antd-react-native) | 针对[@ant-design/react-native](https://rn.mobile.ant.design/index-cn)[umi](https://umijs.org/)插件,为其提供按需加载,主题/皮肤定制、预设、切换,国际化等支持。 |
13-
| [umi-preset-react-native](packages/umi-preset-react-native) | [![npm version](https://img.shields.io/npm/v/umi-preset-react-native.svg?style=flat-square)](https://www.npmjs.com/package/umi-preset-react-native) | 针对 [react-native](https://reactnative.dev/) 应用的 [umi](https://umijs.org/) 插件集,为其提供 开发、编译、打包的支持。需要 [react-native](https://reactnative.dev/) **0.59.0 及以上版本(>=0.59.0)** |
14-
| [umi-preset-react-navigation](packages/umi-preset-react-navigation) | [![npm version](https://img.shields.io/npm/v/umi-preset-react-navigation.svg?style=flat-square)](https://www.npmjs.com/package/umi-preset-react-navigation) | 针对 [react-navigation](https://reactnavigation.org/) 的插件集,替换 [umi](https://umijs.org/) 内置的 [react-router](https://reacttraining.com/react-router/),开发地道的原生应用。需要 [react-native](https://reactnative.dev/) **0.60.0 及以上版本(>=0.60.0)** |
13+
| [umi-preset-react-native](packages/umi-preset-react-native) | [![npm version](https://img.shields.io/npm/v/umi-preset-react-native.svg?style=flat-square)](https://www.npmjs.com/package/umi-preset-react-native) | 基础包,让[umi](https://umijs.org/)具备开发 RN 的能力。**需要 [react-native](https://reactnative.dev/) 0.44.0 及以上版本(>=0.44.0)** |
14+
| [umi-preset-react-navigation](packages/umi-preset-react-navigation) | [![npm version](https://img.shields.io/npm/v/umi-preset-react-navigation.svg?style=flat-square)](https://www.npmjs.com/package/umi-preset-react-navigation) | 针对 [react-navigation](https://reactnavigation.org/) 的插件集,替换 [umi](https://umijs.org/) 内置的 [react-router](https://reacttraining.com/react-router/),开发地道的原生应用。**需要 [react-native](https://reactnative.dev/) 0.60.0 及以上版本(>=0.60.0)** |
1515

1616
[发布日志](/CHANGELOG.md)
1717

@@ -26,7 +26,6 @@
2626
- [集成 react-navigation(可选)](#%E9%9B%86%E6%88%90-react-navigation%E5%8F%AF%E9%80%89)
2727
- [配置](#%E9%85%8D%E7%BD%AE)
2828
- [目前支持的 umi 配置项](#%E7%9B%AE%E5%89%8D%E6%94%AF%E6%8C%81%E7%9A%84-umi-%E9%85%8D%E7%BD%AE%E9%A1%B9)
29-
- [umi-preset-react-native 扩展配置](#umi-preset-react-native-%E6%89%A9%E5%B1%95%E9%85%8D%E7%BD%AE)
3029
- [使用](#%E4%BD%BF%E7%94%A8)
3130
- [开发](#%E5%BC%80%E5%8F%91)
3231
- [打包](#%E6%89%93%E5%8C%85)
@@ -36,34 +35,15 @@
3635
- [没有`NavLink`组件](#%E6%B2%A1%E6%9C%89navlink%E7%BB%84%E4%BB%B6)
3736
- [新增`BackButton``AndroidBackButton`组件](#%E6%96%B0%E5%A2%9Ebackbutton%E5%92%8Candroidbackbutton%E7%BB%84%E4%BB%B6)
3837
- [使用 react-navigation](#%E4%BD%BF%E7%94%A8-react-navigation)
39-
- [深入](#%E6%B7%B1%E5%85%A5)
40-
- [切分多 bundle](#%E5%88%87%E5%88%86%E5%A4%9A-bundle)
4138
- [FAQ](#faq)
4239
- [hmrClient.send is not a function](#hmrclientsend-is-not-a-function)
43-
- [Live Reloading, Fast Refresh, Hot Replacement... 无法使用](#live-reloading-fast-refresh-hot-replacement-%E6%97%A0%E6%B3%95%E4%BD%BF%E7%94%A8)
4440
- [使用 @ant-design/react-native 组件时,报错:Unrecognized font family 'antoutline'](#%E4%BD%BF%E7%94%A8-ant-designreact-native-%E7%BB%84%E4%BB%B6%E6%97%B6%E6%8A%A5%E9%94%99unrecognized-font-family-antoutline)
4541

4642
## 必备
4743

4844
- RN 工程(已有,或使用`react-native init`新建);
4945
- 全局 或 RN 工程本地(内部)安装 umi 3.0 及以上版本。
5046

51-
Node、react、react-native、umi 版本要求:
52-
53-
```json
54-
{
55-
"name": "umi-preset-react-native",
56-
"engines": {
57-
"node": ">=10.x"
58-
},
59-
"peerDependencies": {
60-
"react": "^16.8.3",
61-
"react-native": ">=0.59.0 <1.0.x",
62-
"umi": "^3.0.0"
63-
}
64-
}
65-
```
66-
6747
## 安装
6848

6949
如果没有 RN 工程,则使用`react-native init`得到初始工程:
@@ -168,13 +148,11 @@ _与 DOM 无关的[umi](https://umijs.org/)插件都是可以使用的,或者
168148

169149
### 目前支持的 umi 配置项
170150

171-
**umi-preset-react-native**使用[haul](https://github.com/callstack/haul)打包器。
151+
**umi-preset-react-native**使用 RN 官方的[metro](https://github.com/facebook/metro)打包器。
172152

173153
目前支持的 umi 配置如下(已满足集成一些常用[umi 插件](https://github.com/umijs/plugins)的需要):
174154

175155
- [x] [alias](https://umijs.org/config#alias)
176-
- [x] [chainWebpack](https://umijs.org/config#chainwebpack):其中`createCSSRule`不生效,不支持添加 webpack 插件,使用`lodash.defaultsDeep(userConfig, haulWebapckConfig)`合并,这里注入的用户配置优先级高于[haul](https://github.com/callstack/haul)配置
177-
- [x] [dynamicImport](https://umijs.org/config#dynamicimport)
178156
- [x] [extraBabelPlugins](https://umijs.org/config#extrababelplugins)
179157
- [x] [extraBabelPresets](https://umijs.org/config#extrababelpresets)
180158
- [x] [history](https://umijs.org/config#history):只能使用:`{ type: 'memory' }`
@@ -189,59 +167,32 @@ _与 DOM 无关的[umi](https://umijs.org/)插件都是可以使用的,或者
189167

190168
_上文未列出的[umi 配置](https://umijs.org/config)**umi-preset-react-native** 不生效。_
191169

192-
[haul](https://github.com/callstack/haul)使用的 devServer 是[hapi](https://hapi.dev/),目前还不支持扩展额外的 hapi 插件(中间件),暂时无法支持[mock](https://umijs.org/config#mock)[proxy](https://umijs.org/config#proxy)功能。
170+
**注意**
193171

194-
### umi-preset-react-native 扩展配置
172+
使用**umi-preset-react-native**会在 RN 工程根目录下生成以下临时文件:
195173

196-
```javascript
197-
// .umirc.js
198-
export default {
199-
reactNative: {
200-
appKey: require('./app.json').name,
201-
version: require('react-native/package.json').version,
202-
},
203-
haul: {
204-
bundles: {
205-
index: {
206-
entry: './umi.ts',
207-
},
208-
},
209-
},
210-
};
211-
```
174+
- index.js
175+
- babel.config.js
176+
- metro.config.js
212177

213-
- `reactNative`:选填,默认值:上面代码示例中的值;
214-
- `haul`:选填,默认值:上面代码示例中的值,即[Project Configuration](https://github.com/callstack/haul/blob/master/docs/Configuration.md#project-configuration-reference)
178+
`react-native init`得到的这 3 个原文件将会被覆盖。
215179

216-
## 使用
180+
额外添加 Babel 配置只能在`umirc.js`中使用[extraBabelPlugins](https://umijs.org/config#extrababelplugins)[extraBabelPresets](https://umijs.org/config#extrababelpresets)配置项。
217181

218-
### 开发
182+
额外添加[Metro 配置](https://facebook.github.io/metro/docs/configuration)需要使用环境变量:[UMI_ENV](https://umijs.org/docs/env-variables#umi_env)指定要加载的配置文件:`metro.${UMI_ENV}.config.js`
219183

220-
修改`package.json`文件,使用`umi`取代`react-native`
184+
比如,执行`UMI_ENV=dev umi watch`时,会加载`metro.dev.config.js`文件中的配置,使用[mergeConfig](https://facebook.github.io/metro/docs/configuration#merging-configurations)`metro.config.js`中的配置进行合并。
221185

222-
```diff
223-
{
224-
"scripts": {
225-
"android": "react-native run-android",
226-
"ios": "react-native run-ios",
227-
- "start": "react-native start",
228-
+ "start": "umi dev-rn",
229-
},
230-
}
231-
```
186+
## 使用
232187

233-
启动 dev web server:
188+
### 开发
189+
190+
先执行 umi watch 监听源码文件变动,重新生成临时代码:
234191

235192
```npm
236-
yarn start
193+
yarn watch
237194
```
238195

239-
执行上面命令后,会看到:
240-
241-
![](https://cdn.xuyuanxiang.me/start_snapshot_332028d2.png)
242-
243-
_需要原生 Android、iOS 应用启动后,请求 bundle URL 时,进度条才会更新。_
244-
245196
接下来,另启一个终端,编译并启动 Android 应用:
246197

247198
```npm
@@ -256,36 +207,13 @@ yarn ios
256207

257208
### 打包
258209

259-
构建离线包(offline bundle):
260-
261-
```shell
262-
umi build-rn --platform <ios|android> [--bundle-output relative/to/output/outputPath/js.bundle] [--assets-dest relative/to/outputPath/assets]
263-
```
210+
先使用 umi 生成临时代码:
264211

265-
_`relative/to/output/outputPath`: 表示相对于[outputPath](https://umijs.org/config#outputpath)的路径。_
266-
267-
package.json:
268-
269-
```diff
270-
{
271-
"scripts": {
272-
"android": "react-native run-android",
273-
"ios": "react-native run-ios",
274-
"start": "umi dev-rn",
275-
+ "build:ios": "umi build-rn --platform ios",
276-
+ "build:android": "umi build-rn --platform android"
277-
},
278-
}
212+
```npm
213+
umi g tmp
279214
```
280215

281-
- 执行`yarn build:ios`
282-
1. 构建生成`main.jsbundle`文件到`dist/`目录;
283-
2. 拷贝静态资源到`dist/assets/`目录。
284-
- 执行`yarn build:android`
285-
1. 构建生成`index.android.bundle`文件到`dist/`目录;
286-
2. 拷贝静态资源到`dist/assets/`目录。
287-
288-
_`dist`[outputPath](https://umijs.org/config#outputpath)配置项的缺省(默认)值,可在`.umirc.js`中配置其他目录。_
216+
再使用[react-native bundle](https://github.com/react-native-community/cli/blob/master/docs/commands.md#bundle)构建离线包(offline bundle)。
289217

290218
## 路由
291219

@@ -371,61 +299,6 @@ export default Layout;
371299

372300
了解详情,请移步至:<a href="https://github.com/xuyuanxiang/umi-react-native/tree/master/packages/umi-preset-react-navigation#readme" target="_blank">umi-preset-react-navigation</a>。
373301

374-
## 深入
375-
376-
### 切分多 bundle
377-
378-
> TODO: 这一部分还是理论阶段,有待实践。(先画个饼)
379-
380-
参考[react-native-community/react-native-multibundle](https://github.com/react-native-community/react-native-multibundle)
381-
382-
使用 haul 切分多 bundle:
383-
384-
```javascript
385-
// .umirc.js
386-
export default {
387-
haul: {
388-
templates: {
389-
filename: {
390-
ios: '[bundleName].ios.bundle',
391-
},
392-
},
393-
features: {
394-
multiBundle: 2,
395-
},
396-
bundles: {
397-
index: {
398-
entry: ['./umi.ts', 'react', 'react-native'],
399-
dll: true,
400-
type: 'indexed-ram-bundle',
401-
},
402-
host: {
403-
entry: '@/app.js',
404-
dependsOn: ['index'],
405-
app: true,
406-
},
407-
login: {
408-
entry: '@/pages/login.js',
409-
type: 'indexed-ram-bundle',
410-
dependsOn: ['index'],
411-
app: true,
412-
},
413-
home: {
414-
entry: '@/pages/home.js',
415-
type: 'indexed-ram-bundle',
416-
dependsOn: ['index'],
417-
app: true,
418-
},
419-
},
420-
},
421-
};
422-
```
423-
424-
**注意**
425-
426-
- **不要**使用 haul 提供的工具:`makeConfig``withPolyfills`
427-
- ****保证主 bundle 中必须包含:`./umi.ts`
428-
429302
## FAQ
430303

431304
### hmrClient.send is not a function
@@ -444,10 +317,6 @@ _在 RN 工程`node_modules`目录中找到`metro`并查看版本:_
444317
cat ./node_modules/metro/package.json | grep version
445318
```
446319

447-
### Live Reloading, Fast Refresh, Hot Replacement... 无法使用
448-
449-
[haul](https://github.com/callstack/haul)不支持:[haul#issue-682](https://github.com/callstack/haul/issues/682)
450-
451320
### 使用 @ant-design/react-native 组件时,报错:Unrecognized font family 'antoutline'
452321

453322
[ant-design/ant-design-mobile-rn#issue-194](https://github.com/ant-design/ant-design-mobile-rn/issues/194)中有解决方案。

packages/umi-plugin-antd-react-native/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"url": "https://github.com/xuyuanxiang/umi-react-native/issues"
4040
},
4141
"peerDependencies": {
42+
"@ant-design/react-native": "*",
4243
"umi": "^3.0.0"
4344
}
4445
}

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

+2-10
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,12 @@
3737
"url": "https://github.com/xuyuanxiang/umi-react-native/issues"
3838
},
3939
"peerDependencies": {
40-
"metro": ">=0.56.0",
41-
"react-native": ">=0.59.0 <1.0.x",
40+
"react-native": ">=0.44",
4241
"umi": "^3.0.0"
4342
},
4443
"dependencies": {
45-
"@haul-bundler/babel-preset-react-native": "0.16.0",
46-
"@haul-bundler/cli": "0.22.1",
47-
"@haul-bundler/preset-0.59": "0.18.0",
48-
"@haul-bundler/preset-0.60": "0.18.0",
4944
"@types/react-router-native": "5.1.0",
50-
"@umijs/runtime": "^3.0.0",
45+
"babel-plugin-module-resolver": "^4.0.0",
5146
"react-router-native": "5.1.2"
52-
},
53-
"devDependencies": {
54-
"metro": ">=0.56.0"
5547
}
5648
}

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
export default function () {
22
return {
33
plugins: [
4-
require.resolve('./plugins/commands/buildRn'),
5-
require.resolve('./plugins/commands/devRn'),
4+
require.resolve('./plugins/commands/watch'),
65
require.resolve('./plugins/generateFiles/react-native/exports'),
76
require.resolve('./plugins/generateFiles/react-native/polyfill'),
87
require.resolve('./plugins/generateFiles/react-native/runtime'),
98
require.resolve('./plugins/generateFiles/babelConfig'),
10-
require.resolve('./plugins/generateFiles/haulConfig'),
11-
require.resolve('./plugins/features/haul'),
9+
require.resolve('./plugins/generateFiles/metroConfig'),
10+
require.resolve('./plugins/generateFiles/index'),
1211
require.resolve('./plugins/features/reactNative'),
1312
],
1413
};

packages/umi-preset-react-native/src/plugins/commands/buildRn.ts

-69
This file was deleted.

packages/umi-preset-react-native/src/plugins/commands/shared.ts

-18
This file was deleted.

0 commit comments

Comments
 (0)