1. Packaging
react-native-cli provides bundle command, we can work with metro.config.js to configure the bundle parameters.
After installing react-native-cli, execute react-native bundle -h in the root directory of the rn project to get the following help message.
1 | Options: |
A few parameters we need to use when packaging are:
–entry-file: the entry file, usually use index.js in the root directory as the entry file.
–platform: the platform for packaging, ios or android.
–dev: if or not in debug mode, default is debug mode, so you need to set it to false.
–minify: if or not the code should be compressed, when dev is true, minify will be false, when dev is false, minify will be true.
—bundle-output: path to store the packaged jsbundle.
–assets-dest: path to store the packaged resources, this path will generate the relative path of the resource files in the jsbundle, try to set it in the same directory as the jsbundle.
—sourcemap-output: path to output sourcemap, if not added, it will not output sourcemap.
–config: matroconfig configuration file, used to configure packaging information, can do some unpacking configuration.
2. Split bundle
By default, we can type a complete RN package directly with the following command.
1 | react-native bundle --entry-file ./index.js --platform ios --dev false --bundle-output ./build/ios/main.jsbundle --assets-dest ./build/ios/assets |
However, for a large project, many businesses are divided into business groups according to product lines or other scenarios, and it is not suitable for the whole application to hit a complete package. We can use something like Doodle Intelligence, each panel to create a RCTRootView and maintained by a separate RCTBridge form, the advantage is that there is no need to unpack, each panel business environment is independent, there is no variable pollution. The disadvantage is that each business package is very large, the download consumes resources, each panel creates a set of independent js runtime environment, it is not suitable for too many panels running at the same time.
We can also use the form of unpacking loading, each panel business is split into three parts.
RN framework part, this part of the RN framework for the core code, need to be loaded before the panel start to ensure that the subsequent loading logic is normal. This part will generally not change, you can follow the app version of the release iteration.
Public library part, such as some third-party dependencies, tuya-panel-kit and so on. This part can be released as a separate hot update version to fix the bugs in the public libraries.
Business part, this part is business code, which needs to be changed according to business requirements, and some non-public third-party library dependencies may be added.
For simplicity, we combine 1 and 2 into one module, which we call the base package. 3 as the business part is called the business package.
First we generate a configuration file for the base package, specifying which files need to be packaged into the base package and how the ids of each module are defined during the packaging process.
1 | const pathSep = require('path').sep; |
Once the configuration is complete, we use it for packaging.
1 | react-native bundle --config ./core.config.js --entry-file ./indexCore.js --platform ${PLATFORM} --dev false --bundle-output ${OUTPUT_PATH}/${BIZ_FOLDER}.${PLATFORM}.js --assets-dest ${OUTPUT_PATH} |
After typing the RN package, we look at how the contents differ from the RN package when packaged directly.
1 | __d(function(g,r,i,a,m,e,d){'use strict';m.exports=r(d[0])},3,[4]); |
At this point, look at the packaged code, the original digital definition of the moduleId into the relative path to the file definition of the moduleId, is not a clearer and better understanding of it.
Similarly, we modify the processModuleFilter filtering rules to play out multiple business packages. Packaging and unpacking part of the realization is complete.