@bm-fe/react-native-multi-bundle 1.0.0-beta.0 → 1.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/INTEGRATION.md CHANGED
@@ -55,113 +55,155 @@ npx react-native start
55
55
 
56
56
  ## Native 模块集成
57
57
 
58
- ### Android
58
+ ### Android(自动链接,无需手动配置)
59
59
 
60
- #### 1. 复制 Native 模块文件
60
+ #### 自动链接工作原理
61
61
 
62
- 将以下文件从 `templates/native/android/` 复制到你的 Android 项目:
62
+ 安装包后,React Native CLI 会自动:
63
+ 1. 读取包中的 `react-native.config.js` 配置
64
+ 2. 将 `ModuleLoaderPackage` 添加到 `PackageList` 中
65
+ 3. 在构建时自动集成原生模块
66
+
67
+ #### 验证自动链接
68
+
69
+ 安装包后,可以通过以下命令验证自动链接是否生效:
70
+
71
+ ```bash
72
+ npx react-native config
73
+ ```
74
+
75
+ 在输出中应该能看到 `@bm-fe/react-native-multi-bundle` 的配置信息。
76
+
77
+ #### 确保 assets 目录存在
78
+
79
+ 确保 `android/app/src/main/assets/` 目录存在,bundle 文件将放在这里:
80
+
81
+ ```bash
82
+ mkdir -p android/app/src/main/assets/modules
83
+ ```
84
+
85
+ #### 手动集成(可选,仅在自动链接不工作时使用)
86
+
87
+ 如果自动链接不工作(极少数情况),可以手动集成:
88
+
89
+ <details>
90
+ <summary>点击展开手动集成步骤</summary>
91
+
92
+ ##### 1. 复制 Native 模块文件
93
+
94
+ 将以下文件从 `node_modules/@bm-fe/react-native-multi-bundle/templates/native/android/` 复制到你的 Android 项目:
63
95
 
64
96
  - `ModuleLoaderModule.kt` → `android/app/src/main/java/com/yourpackage/ModuleLoaderModule.kt`
65
97
  - `ModuleLoaderPackage.kt` → `android/app/src/main/java/com/yourpackage/ModuleLoaderPackage.kt`
66
98
 
67
99
  **重要**:修改文件中的包名 `com.yourpackage` 为你的实际包名。
68
100
 
69
- #### 2. 注册 ModuleLoaderPackage
101
+ ##### 2. 注册 ModuleLoaderPackage
70
102
 
71
- 根据你使用的 React Native 架构版本,选择对应的方式:
72
-
73
- ##### 方式一:新架构(React Native 0.82+,使用 ReactHost)
74
-
75
- 如果你的 `MainApplication.kt` 使用 `ReactHost`(新架构),在 `PackageList` 中添加:
103
+ `MainApplication.kt` 中添加:
76
104
 
77
105
  ```kotlin
78
- package com.yourpackage
79
-
80
- import android.app.Application
81
106
  import com.yourpackage.ModuleLoaderPackage
82
- import com.facebook.react.PackageList
83
- import com.facebook.react.ReactApplication
84
- import com.facebook.react.ReactHost
85
- import com.facebook.react.ReactNativeApplicationEntryPoint.loadReactNative
86
- import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
87
107
 
88
108
  class MainApplication : Application(), ReactApplication {
89
109
  override val reactHost: ReactHost by lazy {
90
110
  getDefaultReactHost(
91
111
  context = applicationContext,
92
- packageList =
93
- PackageList(this).packages.apply {
94
- // Packages that cannot be autolinked yet can be added manually here
95
- add(ModuleLoaderPackage())
96
- },
112
+ packageList = PackageList(this).packages.apply {
113
+ add(ModuleLoaderPackage()) // 手动添加
114
+ },
97
115
  )
98
116
  }
99
-
100
- override fun onCreate() {
101
- super.onCreate()
102
- loadReactNative(this)
103
- }
117
+ // ...
104
118
  }
105
119
  ```
106
120
 
107
- ##### 方式二:旧架构(使用 ReactNativeHost)
121
+ </details>
108
122
 
109
- 如果你的 `MainApplication.kt` 使用 `ReactNativeHost`(旧架构),在 `getPackages()` 方法中添加:
123
+ ### iOS(自动链接,无需手动配置)
110
124
 
111
- ```kotlin
112
- package com.yourpackage
125
+ #### 自动链接工作原理
113
126
 
114
- import android.app.Application
115
- import com.yourpackage.ModuleLoaderPackage
116
- import com.facebook.react.ReactApplication
117
- import com.facebook.react.ReactPackage
118
- import com.facebook.react.PackageList
119
- import com.facebook.react.ReactNativeHost
120
- import com.facebook.react.defaults.DefaultReactNativeHost
127
+ 安装包后,React Native CLI 和 CocoaPods 会自动:
128
+ 1. 读取包中的 `react-native-multi-bundle.podspec`
129
+ 2. 将 `ModuleLoader` 原生模块添加到项目中
130
+ 3. 在构建时自动集成
121
131
 
122
- class MainApplication : Application(), ReactApplication {
123
- override val reactNativeHost: ReactNativeHost =
124
- object : DefaultReactNativeHost(this) {
125
- override fun getPackages(): List<ReactPackage> =
126
- PackageList(this).packages.apply {
127
- // Packages that cannot be autolinked yet can be added manually here
128
- add(ModuleLoaderPackage())
129
- }
130
-
131
- override fun getJSMainModuleName(): String = "index"
132
- override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG
133
- // ... 其他配置
134
- }
132
+ #### 安装依赖
135
133
 
136
- // ... 其他代码
137
- }
134
+ 安装 npm 包后,运行 pod install:
135
+
136
+ ```bash
137
+ cd ios && pod install
138
+ ```
139
+
140
+ #### 验证自动链接
141
+
142
+ 安装包后,可以通过以下命令验证自动链接是否生效:
143
+
144
+ ```bash
145
+ npx react-native config
138
146
  ```
139
147
 
140
- **注意**:React Native 0.82+ 默认使用新架构(ReactHost),推荐使用方式一。
148
+ 在输出中应该能看到 `@bm-fe/react-native-multi-bundle` iOS 配置信息。
149
+
150
+ #### 准备 Bundles 目录
151
+
152
+ 确保 iOS 项目中有 `Bundles` 目录用于存放 bundle 文件:
153
+
154
+ 1. 在 Xcode 中,右键点击项目 → New Group,创建 `Bundles` 目录
155
+ 2. 或者在终端中创建:
156
+ ```bash
157
+ mkdir -p ios/YourProject/Bundles/modules
158
+ ```
159
+
160
+ 3. **重要**:确保 `Bundles` 目录已添加到 Xcode 项目的 Resources 中:
161
+ - 在 Xcode 中,右键点击 `Bundles` 目录 → Add Files to "YourProject"
162
+ - 确保 "Create folder references" 已选中(蓝色文件夹图标)
163
+ - 这样 bundle 文件才会被打包到 app 中
164
+
165
+ #### Bundle 文件结构
141
166
 
142
- #### 3. 确保 assets 目录存在
167
+ 构建完成后,iOS bundle 文件应放置在:
143
168
 
144
- 确保 `android/app/src/main/assets/` 目录存在,bundle 文件将放在这里。
169
+ ```
170
+ ios/YourProject/Bundles/
171
+ ├── bundle-manifest.json
172
+ ├── main.jsbundle
173
+ └── modules/
174
+ ├── home.jsbundle
175
+ ├── details.jsbundle
176
+ └── settings.jsbundle
177
+ ```
145
178
 
146
- ### iOS
179
+ #### 手动集成(可选,仅在自动链接不工作时使用)
147
180
 
148
- #### 1. 复制 Native 模块文件
181
+ <details>
182
+ <summary>点击展开手动集成步骤</summary>
149
183
 
150
- 将以下文件从 `templates/native/ios/` 添加到你的 iOS 项目:
184
+ ##### 1. 复制 Native 模块文件
185
+
186
+ 将以下文件从 `node_modules/@bm-fe/react-native-multi-bundle/ios/ModuleLoader/` 复制到你的 iOS 项目:
151
187
 
152
188
  - `ModuleLoaderModule.h`
153
- - `ModuleLoaderModule.m`
189
+ - `ModuleLoaderModule.mm`
154
190
 
155
191
  在 Xcode 中:
156
192
  1. 右键点击项目 → Add Files to "YourProject"
157
193
  2. 选择这两个文件
158
194
  3. 确保 "Copy items if needed" 已勾选
159
195
 
160
- #### 2. 注册模块
196
+ ##### 2. 验证编译
161
197
 
162
198
  iOS 端的模块会自动注册(通过 `RCT_EXPORT_MODULE`),无需额外配置。
163
199
 
164
- **注意**:iOS 端的 bundle 加载实现可能需要根据你的具体需求调整。当前模板提供了基础框架。
200
+ 重新构建项目:
201
+
202
+ ```bash
203
+ npx react-native run-ios
204
+ ```
205
+
206
+ </details>
165
207
 
166
208
  ## 初始化多 Bundle 系统
167
209
 
@@ -326,6 +368,53 @@ export const createHomeScreen = createModuleRouteLoader('home', 'Home');
326
368
  <Stack.Screen name="Home" getComponent={createHomeScreen} />
327
369
  ```
328
370
 
371
+ **路由 Props 传递说明:**
372
+
373
+ `createModuleRouteLoader` 会自动将 React Navigation 的路由 props 传递给模块组件,包括:
374
+
375
+ - `navigation`:导航对象,用于页面跳转
376
+ - `route`:路由对象,包含路由参数和配置
377
+
378
+ 模块组件可以直接使用这些 props:
379
+
380
+ ```typescript
381
+ // 在模块组件中(如 HomeScreen.tsx)
382
+ import React from 'react';
383
+ import { View, Text } from 'react-native';
384
+ import { NativeStackScreenProps } from '@react-navigation/native-stack';
385
+
386
+ type Props = NativeStackScreenProps<RootStackParamList, 'Home'>;
387
+
388
+ const HomeScreen = ({ navigation, route }: Props) => {
389
+ // 可以使用 navigation 进行页面跳转
390
+ const handleNavigate = () => {
391
+ navigation.navigate('Details', { id: '123' });
392
+ };
393
+
394
+ // 可以使用 route.params 获取路由参数
395
+ const params = route.params;
396
+
397
+ return (
398
+ <View>
399
+ <Text>Home Screen</Text>
400
+ {/* ... */}
401
+ </View>
402
+ );
403
+ };
404
+
405
+ export default HomeScreen;
406
+ ```
407
+
408
+ 如果需要传递额外的 props,可以通过 React Navigation 的 `initialParams` 或 `getInitialProps`:
409
+
410
+ ```typescript
411
+ <Stack.Screen
412
+ name="Home"
413
+ getComponent={createHomeScreen}
414
+ initialParams={{ customProp: 'value' }}
415
+ />
416
+ ```
417
+
329
418
  ### 方式二:使用路由注册系统
330
419
 
331
420
  ```typescript
@@ -364,6 +453,18 @@ const HomeScreen = getModuleRoute('home', 'Home');
364
453
  - 检查 `devServer` 配置是否正确
365
454
  - 验证 manifest URL 是否可访问
366
455
 
456
+ ### 5. 配置文件找不到
457
+
458
+ 如果执行打包命令时提示 `Config file not found`:
459
+
460
+ - **确认配置文件位置**:`multi-bundle.config.json` 应该在项目根目录(与 `package.json` 同级)
461
+ - **确认执行目录**:打包命令应该在项目根目录执行,或者通过 npm scripts 执行
462
+ - **手动指定项目根目录**:如果仍有问题,可以通过环境变量指定:
463
+ ```bash
464
+ PROJECT_ROOT=/path/to/your/project node node_modules/@bm-fe/react-native-multi-bundle/scripts/build-multi-bundle.js ios
465
+ ```
466
+ - **检查文件路径**:确认错误信息中的路径是否正确指向你的项目根目录
467
+
367
468
  ## 更多信息
368
469
 
369
470
  - [API 参考](./src/multi-bundle/README.md)
@@ -0,0 +1,93 @@
1
+ def isNewArchitectureEnabled() {
2
+ return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
3
+ }
4
+
5
+ apply plugin: "com.android.library"
6
+ apply plugin: "org.jetbrains.kotlin.android"
7
+
8
+ if (isNewArchitectureEnabled()) {
9
+ apply plugin: "com.facebook.react"
10
+ }
11
+
12
+ def getExtOrDefault(name) {
13
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["ModuleLoader_" + name]
14
+ }
15
+
16
+ def getExtOrIntegerDefault(name) {
17
+ return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["ModuleLoader_" + name]).toInteger()
18
+ }
19
+
20
+ def supportsNamespace() {
21
+ def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
22
+ def major = parsed[0].toInteger()
23
+ def minor = parsed[1].toInteger()
24
+
25
+ // Namespace support was added in 7.3.0
26
+ return (major == 7 && minor >= 3) || major >= 8
27
+ }
28
+
29
+ android {
30
+ if (supportsNamespace()) {
31
+ namespace "com.bitmart.exchange.module.loader"
32
+
33
+ sourceSets {
34
+ main {
35
+ manifest.srcFile "src/main/AndroidManifestNew.xml"
36
+ }
37
+ }
38
+ }
39
+
40
+ compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
41
+
42
+ defaultConfig {
43
+ minSdkVersion getExtOrIntegerDefault("minSdkVersion")
44
+ targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
45
+ buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
46
+ }
47
+
48
+ buildFeatures {
49
+ buildConfig true
50
+ }
51
+
52
+ buildTypes {
53
+ release {
54
+ minifyEnabled false
55
+ }
56
+ }
57
+
58
+ lintOptions {
59
+ disable "GradleCompatible"
60
+ }
61
+
62
+ compileOptions {
63
+ sourceCompatibility JavaVersion.VERSION_17
64
+ targetCompatibility JavaVersion.VERSION_17
65
+ }
66
+
67
+ kotlinOptions {
68
+ jvmTarget = "17"
69
+ }
70
+ }
71
+
72
+ repositories {
73
+ mavenCentral()
74
+ google()
75
+ }
76
+
77
+ def isInDevelopment = project.hasProperty('ModuleLoader_development') && project.property('ModuleLoader_development') == "true"
78
+
79
+ dependencies {
80
+ if (isInDevelopment) {
81
+ implementation("com.facebook.react:react-android:0.71.+")
82
+ } else {
83
+ // For < 0.71, this will be from the local maven repo
84
+ // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
85
+ //noinspection GradleDynamicVersion
86
+ implementation "com.facebook.react:react-native:+"
87
+ }
88
+
89
+ // Kotlin Coroutines
90
+ implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
91
+ implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
92
+ }
93
+
@@ -0,0 +1,4 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
+ package="com.bitmart.exchange.module.loader">
3
+ </manifest>
4
+
@@ -0,0 +1,3 @@
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ </manifest>
3
+