Skip to content

Commit 0c0c366

Browse files
author
xuyuanxiang@home
committed
feat: support multiply bundles
1 parent f9b50e2 commit 0c0c366

File tree

20 files changed

+112
-54
lines changed

20 files changed

+112
-54
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"clean": "rimraf packages/*/lib",
77
"lint": "eslint packages/*/src/**/*.{ts,tsx}",
88
"build": "cross-env NODE_ENV=production lerna run build",
9-
"watch": "cross-env NODE_ENV=development lerna run watch --parallel --concurrency 4",
9+
"watch": "cross-env NODE_ENV=development lerna run watch --parallel --concurrency 5",
1010
"changelog": "rimraf CHANGELOG.md && node scripts/changelog",
1111
"prerelease": "yarn clean && yarn build",
1212
"release": "lerna publish",

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

+2-5
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,14 @@
1717
"engines": {
1818
"node": ">=10.x"
1919
},
20-
"main": "lib",
20+
"main": "lib/index.js",
2121
"typings": "lib/index.d.ts",
2222
"types": "lib/index.d.ts",
2323
"directories": {
2424
"lib": "lib"
2525
},
2626
"files": [
27-
"lib",
28-
"android",
29-
"ios",
30-
"umi-preset-react-native.podspec"
27+
"lib"
3128
],
3229
"publishConfig": {
3330
"registry": "https://registry.npmjs.org/"

packages/umi-preset-react-native/src/plugins/features/reactNative.ts

+17-17
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,23 @@ export default (api: IApi) => {
112112
throw new TypeError('"history.type" 配置错误');
113113
}
114114

115-
if (api.config.dynamicImport) {
116-
api.logger.error('在 RN 环境中暂不支持:"dynamicImport"功能。');
117-
throw new TypeError('在 RN 环境中暂不支持:"dynamicImport"功能。');
118-
}
119-
120-
// if (api.config.haul) {
121-
// if (api.config.dynamicImport && !api.config.dynamicImport.loading) {
122-
// api.logger.error(
123-
// `在 RN 环境中启用"dynamicImport"功能时,必须实现自定义的"loading"!${EOL}因为 umi 默认 loading 使用了 HTML 标签,在 RN 中运行会报错!${EOL}查看如何配置自定义 loading:https://umijs.org/config#dynamicimport`,
124-
// );
125-
// throw new TypeError('"dynamicImport.loading" 未配置');
126-
// }
127-
// } else {
128-
// if (api.config.dynamicImport) {
129-
// api.logger.error('在 RN 环境中暂不支持:"dynamicImport"功能。');
130-
// throw new TypeError('在 RN 环境中暂不支持:"dynamicImport"功能。');
131-
// }
115+
// if (api.config.dynamicImport) {
116+
// api.logger.error('在 RN 环境中暂不支持:"dynamicImport"功能。');
117+
// throw new TypeError('在 RN 环境中暂不支持:"dynamicImport"功能。');
132118
// }
119+
120+
if (api.config.haul) {
121+
if (api.config.dynamicImport && !api.config.dynamicImport.loading) {
122+
api.logger.error(
123+
`在 RN 环境中启用"dynamicImport"功能时,必须实现自定义的"loading"!${EOL}因为 umi 默认 loading 使用了 HTML 标签,在 RN 中运行会报错!${EOL}查看如何配置自定义 loading:https://umijs.org/config#dynamicimport`,
124+
);
125+
throw new TypeError('"dynamicImport.loading" 未配置');
126+
}
127+
} else {
128+
if (api.config.dynamicImport) {
129+
api.logger.error('在 RN 环境中暂不支持:"dynamicImport"功能。');
130+
throw new TypeError('在 RN 环境中暂不支持:"dynamicImport"功能。');
131+
}
132+
}
133133
});
134134
};

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { IApi } from 'umi';
22
import { createFormatter, routesToJSON } from '../../../utils';
33

44
const ROUTES_TPL = `import {ApplyPluginsType, dynamic} from 'umi';
5-
import Multibundle from 'umi-preset-react-native';
5+
import Multibundle from 'umi-react-native-multibundle';
66
import { plugin } from '@@/core/plugin';
77
88
const routes = {{#routes}}{{{ routes }}}{{/routes}}{{^routes}}{}{{/routes}};

packages/umi-preset-react-native/src/templates/haulConfigTpl.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,7 @@ export default makeConfig({
2424
}
2525
),
2626
dll: true,
27-
type: 'indexed-ram-bundle',
28-
transform,
29-
},
30-
host: {
31-
entry: '@/index',
32-
dependsOn: ['index'],
33-
app: true,
27+
// type: 'indexed-ram-bundle',
3428
transform,
3529
},
3630
...{{{ bundles }}},

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

+24-19
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,29 @@ interface IBundle {
9090
transform?: string;
9191
}
9292

93-
export function transformRoutesToBundle(routes: IRoute[], parent?: IRoute): IBundle[] {
93+
export function getBundleNameFrom(component: string): string {
94+
const bundleName = component.replace(/^@\//, '').replace(/\.[tj]sx?$/, '');
95+
if (bundleName === 'pages/index') {
96+
return 'host';
97+
}
98+
return bundleName;
99+
}
100+
101+
export function transformRoutesToBundle(routes: IRoute[]): IBundle[] {
94102
const bundles: IBundle[] = [];
95103
for (const route of routes) {
96-
if (Array.isArray(route.routes)) {
97-
bundles.push(...transformRoutesToBundle(route.routes, route));
98-
} else {
99-
if (route.component) {
100-
bundles.push({
101-
name: route.component,
102-
entry:
103-
parent && parent.component
104-
? { entryFiles: [route.component], setupFiles: [parent.component] }
105-
: route.component,
106-
dependsOn: ['index'],
107-
app: true,
108-
type: 'indexed-ram-bundle',
109-
transform: 'transform',
110-
});
111-
}
104+
if (route.component) {
105+
bundles.push({
106+
name: getBundleNameFrom(route.component),
107+
entry: route.component,
108+
dependsOn: ['index'],
109+
app: true,
110+
// type: 'indexed-ram-bundle',
111+
transform: 'transform',
112+
});
113+
}
114+
if (Array.isArray(route.routes) && route.routes.length > 0) {
115+
bundles.push(...transformRoutesToBundle(route.routes));
112116
}
113117
}
114118
return bundles;
@@ -185,14 +189,15 @@ export async function routesToJSON(api: IApi) {
185189
if (isFunctionComponent(value)) return value;
186190
if (api.config.dynamicImport) {
187191
const [component] = value.split(SEPARATOR);
192+
const bundleName = getBundleNameFrom(component);
188193
let loading = '';
189194
if (api.config.dynamicImport.loading) {
190195
loading = `,loading: require('${api.config.dynamicImport.loading}').default`;
191196
}
192197
return `dynamic({
193198
loader: async () => {
194-
await Multibundle.loadBundle('${component}');
195-
return Multibundle.getBundleExport('${component}');
199+
await Multibundle.loadBundle('${bundleName}');
200+
return Multibundle.getBundleExport('${bundleName}');
196201
}${loading}})`;
197202
} else {
198203
return `require('${value}').default`;

packages/umi-plugin-react-native-multibundle/android/src/main/java/com/reactlibrary/MultibundleModule.java packages/umi-react-native-multibundle/android/src/main/java/com/reactlibrary/MultibundleModule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void loadBundle(String bundleName, int bundleId, Promise promise) {
2828
this.reactContext.getAssets(),
2929
"assets://" + bundleName + ".android.bundle",
3030
false);
31-
catalystInstance.registerSegment(bundleId, "assets://" + bundleName + ".android.bundle");
31+
// catalystInstance.registerSegment(bundleId, "assets://" + bundleName + ".android.bundle");
3232
promise.resolve(null);
3333
} catch(Exception e) {
3434
promise.reject(e);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "umi-react-native-multibundle",
3+
"version": "0.3.4",
4+
"description": "umi-react-native-multibundle",
5+
"keywords": [
6+
"umi",
7+
"umijs",
8+
"react-native",
9+
"multibundle"
10+
],
11+
"author": {
12+
"name": "xuyuanxiang",
13+
"email": "hi@xuyuanxiang.cn",
14+
"url": "https://xuyuanxiang.me/about/"
15+
},
16+
"homepage": "https://github.com/xuyuanxiang/umi-react-native#readme",
17+
"license": "MIT",
18+
"main": "lib",
19+
"typings": "lib/index.native.d.ts",
20+
"types": "lib/index.native.d.ts",
21+
"directories": {
22+
"lib": "lib"
23+
},
24+
"files": [
25+
"android",
26+
"ios",
27+
"lib",
28+
"src",
29+
"umi-react-native-multibundle.podspec"
30+
],
31+
"publishConfig": {
32+
"registry": "https://registry.npmjs.org/"
33+
},
34+
"repository": {
35+
"type": "git",
36+
"url": "git+https://github.com/xuyuanxiang/umi-react-native.git"
37+
},
38+
"scripts": {
39+
"build": "tsc",
40+
"watch": "tsc -w"
41+
},
42+
"bugs": {
43+
"url": "https://github.com/xuyuanxiang/umi-react-native/issues"
44+
},
45+
"peerDependencies": {
46+
"react-native": ">=0.60.0"
47+
}
48+
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Multibundle {
4040
if (!this.isBundleLoaded(bundleName)) {
4141
throw new Error(`Bundle ${bundleName} was not loaded`);
4242
}
43+
console.warn('getBundleExport: bundleName=', bundleName, 'default=', global[bundleName].default);
4344
return global[bundleName].default;
4445
}
4546

@@ -66,7 +67,12 @@ class Multibundle {
6667
throw new Error(`Cannot find bundle id for bundle name ${bundleName}`);
6768
}
6869

69-
await MultibundleNativeModule.loadBundle(bundleName, bundleId);
70+
console.warn('loadBundle:', bundleName, ' bundleId:', bundleId);
71+
try {
72+
await MultibundleNativeModule.loadBundle(bundleName, bundleId);
73+
} catch (e) {
74+
console.warn('loadBundle:', bundleName, ' failed:', e);
75+
}
7076

7177
return bundleId;
7278
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"extends": "../../tsconfig",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"outDir": "lib"
6+
},
7+
"include": ["src", "../../types"]
8+
}

packages/umi-plugin-react-native-multibundle/umi-plugin-react-native-multibundle.podspec packages/umi-react-native-multibundle/umi-react-native-multibundle.podspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ require "json"
33
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
44

55
Pod::Spec.new do |s|
6-
s.name = "umi-preset-react-native"
6+
s.name = "umi-react-native-multibundle"
77
s.version = package["version"]
88
s.summary = package["description"]
99
s.description = <<-DESC
10-
umi-preset-react-native
10+
umi-react-native-multibundle
1111
DESC
1212
s.homepage = "https://github.com/xuyuanxiang/umi-react-native#readme"
1313
s.license = "MIT"

0 commit comments

Comments
 (0)