React Native 之 main.jsbundle生成方法


通過react-native init yooweiProject 生成的RN項目(版本基於0.57),目錄結構如下

 

項目結構:

 

大家可以發現main.jsbundle 是紅色的,不存在的,這個屬於正常現象。

運行項目,本以為應該沒有問題的,但是:

打印的日志:

2018-09-30 15:21:21.194392+0800 yooweiProject[11164:1789544] Unhandled JS Exception: Application yooweiProject has not been registered.

Hint: This error often happens when you're running the packager (local dev server) from a wrong folder. For example you have multiple apps and the packager is still running for the app you were working on before.

If this is the case, simply kill the old packager instance (e.g. close the packager terminal window) and start the packager in the correct app folder (e.g. cd into app folder and run 'npm start').

This error can also happen due to a require() error during initialization or failure to call AppRegistry.registerComponent.

 突然間意識到,本地的RN服務還在剛才的那個yooweiRN項目哪里。果斷結束剛才的服務(control+c)

^C

切換到yooweiProject項目根目錄

$ cd /Users/galahad/Desktop/ziliao/RN0.57/yooweiProject 

$ pwd

/Users/galahad/Desktop/ziliao/RN0.57/yooweiProject

然后開啟本地服務

$ react-native start

 運行正常

代碼如下:

 但是main.jsbundle 是紅色的,不存在的,雖說屬於正常現象,但是我們可以嘗試生成。

1、切換到yooweiProject項目根目錄,並執行 npm start

$ cd /Users/galahad/Desktop/ziliao/RN0.57/yooweiProject 

$ npm start

注意:如果終端已經開啟過一個窗口,並且已經運行過該命令npm start(或者是模擬器上面運行RN,已經開啟了本地服務),將終端再開啟一個窗口,執行 npm start ,會出現下面的問題:

 

ERR! code ELIFECYCLE

errno 1

ERR! yooweiProject@0.0.1 start: `node node_modules/react-native/local-cli/cli.js start`

ERR! Exit status 1

npm 

ERR! Failed at the yooweiProject@0.0.1 start script.

ERR! This is probably not a problem with npm. There is likely additional logging output above.

 

ERR! A complete log of this run can be found in:

ERR!     /Users/galahad/.npm/_logs/2018-10-10T01_55_04_103Z-debug.log

解決方案就是關掉一個終端的窗口即可。

2、使用curl命令生成 main.jsbundle

由於終端窗口正在連接本地服務,沒有辦法執行新的命令。我們選中 “終端”圖標,右擊“新建窗口”完美解決

curl http://localhost:8081/index.ios.bundle -o main.jsbundle

不出意外的出現錯誤:

error: bundling failed: Error: The resource `/Users/galahad/Desktop/ziliao/RN0.57/yooweiProject/index.ios.js` was not found.

又是 index.ios.js 入口文件名稱不同惹的禍,修改命令

curl http://localhost:8081/index.bundle -o main.jsbundle

執行正常,該項目根目錄下生成了文件main.jsbundle

注意:上面的這個命令最后用下面的格式

curl http://當前電腦可用的網絡ip地址,注意不要寫localhost,因為可能取到的ip不是本地有效的ip地址:8081/index.bundle -o main.jsbundle

3、將生成的jsbundle文件導入到當前的項目中

4、在AppDelegate.m中選擇使用main.jsbundle注釋掉

jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];

打開注釋:

jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];

但是這種方式進行的總是會報錯:

暫時沒有解決,還需嘗試解決問題!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM