關(guān)于react-native bundle
react-native bundle是react-native-cli的一個命令,制作離線包需要用到react-native bundle命令行,我們先來了解下react-native?bundle可選參數(shù)都有哪些,如果熟悉webpack打包的朋友對下面的參數(shù)會很熟悉:
--entry-file <path>:配置入口JS文件路徑,可以是絕對路徑,也可以是相對于根目錄的相對路徑。
--platform [string]:需要編譯的平臺,“ios”或“android”(默認(rèn):ios)。
--transformer [string]:指定要使用的自定義代碼轉(zhuǎn)換工具。
--dev [boolean]:是否使用開發(fā)者環(huán)境,如果是false,則禁用警告并將壓縮代碼(默認(rèn)為true)。
--minify [boolean]:是否壓縮代碼。如果dev為真,則默認(rèn)為false,如果dev為false,則為true。禁用迷你化可以加速構(gòu)建,對于測試是很有用的。
--bundle-output <path>:bundle的輸出路徑,用于存儲所打包后的代碼。
--bundle-encoding [string]:編碼。(默認(rèn)值:utf8)。
--max-workers [number]:指定用于轉(zhuǎn)換文件的最大工作池。這個默認(rèn)值是電腦可用的內(nèi)核數(shù)。
--sourcemap-output [string]:sourcemap的輸出文件名。
--sourcemap-sources-root [string]:sourcemap的輸出目錄(默認(rèn)要寫相對路徑)。
--sourcemap-use-absolute-path:啟用sourcemap輸出目錄使用絕對路徑。
--assets-dest [string]:bundle中引用的文件目錄名稱。
--verbose:啟用日志。
--reset-cache:刪除緩存文件。
--read-global-cache:從全局緩存中獲取轉(zhuǎn)換的JS代碼。
--config [string]:指定一個CLI配置文件路徑。
-h, --help:使用幫助。
Android打包示例
react-native bundle --entry-file index.android.js --bundle-output ./android/app/src/main/assets/index.android.bundle --platform android --assets-dest ./android/app/src/main/res/ --dev false
注意:1.[./android/app/src/main/assets/]里的assets文件夾若不存在則創(chuàng)建一個。2.增量升級的話不要把圖片資源直接打包到res中,要用--assets-dest ./bundle/androidBundle/?。
IOS打包示例
1.在工程根目錄下執(zhí)行打包命令
react-native bundle –entry-file index.ios.js –bundle-output ./bundle/iosBundle/index.ios.bundle –platform ios –assets-dest ./bundle/iosBundle –dev false?
注意要先保證bundle文件夾存在。?
2.在xcode中添加assets【必須用Create folder references的方式,添加完是藍(lán)色文件夾圖標(biāo)】和index.ios.bundle?
3.參考官方文檔,修改AppDelegate.m文件, jsCodeLocation = [[NSBundle mainBundle] URLForResource:@”index.ios” withExtension:@”bundle”];