本文只是對https://www.jianshu.com/p/bb7c5f1d304e的簡單摘抄,方便自己以后查找,如有版權糾葛,發文必改(刪)。
所謂離線包即是打包本地文件(圖片等),不需要保存在后端。或者針對移動端開發而言,你可以把包分別導入工程,ios端的話是可以的,安卓端沒嘗試過,僅提供這條思路。
打包命令詳情:
使用 react-native bundle --help 來查看打包的具體參數(全是英文,看中文吧)
react-native bundle [參數] 構建 js 離線包
Options:
-h, --help 輸出如何使用的信息
--entry-file <path> RN入口文件的路徑, 絕對路徑或相對路徑
--platform [
string] ios 或 andorid
--transformer [string] Specify a custom transformer to be used
--dev [boolean] 如果為false, 警告會不顯示並且打出的包的大小會變小
--prepack 當通過時, 打包輸出將使用Prepack格式化
--bridge-config [string] 使用Prepack的一個json格式的文件__fbBatchedBridgeConfig 例如: ./bridgeconfig.json --bundle-output <string> 打包后的文件輸出目錄, 例: /tmp/groups.bundle
--bundle-encoding [string] 打離線包的格式 可參考鏈接https://nodejs.org/api/buffer.html#buffer_buffer.
--sourcemap-output [string] 生成Source Map,但0.14之后不再自動生成source map,需要手動指定這個參數。例: /tmp/groups.map --assets-dest [string] 打包時圖片資源的存儲路徑
--verbose 顯示打包過程
--reset-cache 移除緩存文件
--config [string] 命令行的配置文件路徑
打包流程(ios,安卓好像不需要打離線包):
1.cd到工程目錄
2.終端輸入打包命令
react-
native bundle --entry-file index.ios.js --bundle-output ./ios/bundle/index.ios.jsbundle --platform ios --assets-dest ./ios/bundle --dev false
3.最后導入ios工程,在AppDelegate中修改圖片加載的路徑即可
NSURL *jsCodeLocation;
// jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil]; 自帶
jsCodeLocation = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"index.ios.jsbundle" ofType:nil]];修改后
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
moduleName:@"RNBundleDemo" initialProperties:nil launchOptions:launchOptions];
