如何缓存 cordova 的平台项目以防止运行 "cordova platform add <platform>" 时远程下载?



每次我运行Cordova平台添加时,Cordova都会从Internet获取Cordova文件。是否可以将这些文件下载到本地目录并迫使Cordova使用它们代替远程目录?也许有一个神奇的Env变量?

您可以从github本地克隆平台,然后从路径中添加它,而不是从NPM/github添加它,但是您不能使用

cordova platform add <platform>

您将不得不添加

cordova platform add /path/to/platform/

我知道这是我问题的范围,但我仍然认为它值得发布,因为它可能对其他人有用。

说明鲍泽说明了如何设置本地/离线Cordova环境。如果您像我一样,这将为您节省您的时间,并有许多应用程序可以构建和部署到不同的平台。作为奖励,我还解释了如何设置本地gradle,这是Android需要更多时间的任务。

mkdir local_cordova && cd local_cordova && npm init
# confirm all stuff 
npm install cordova-fetch 
touch download-cordova-ios.js 
touch download-cordova-android.js 
open download-cordova-ios.js
# paste the lines below:   
var fetch = require('cordova-fetch'); 
var spec = 'cordova-ios@~4.5.1'; 
/* Version can be ommited */  
var dest = './bin/ios' 
var opts = { save: true } fetch(spec, dest, opts);
# save the file open download-cordova-android.js
# paste the lines below
# Now open the android file and do the same
open download-cordova-android.js

var fetch = require('cordova-fetch'); 
var spec = 'cordova-android@~6.3.0';
/* Version can be ommited */  
var dest = './bin/android' 
var opts = { save: true } 
fetch(spec, dest, opts);

# Now run the node command and wait for the script to complete
node download-cordova-android.js && node download-cordova-ios.js
# Now lets create some environment variables for each our local cordova platforms.
vim ~/.bash_profile
# add the following lines 
export CORDOVA_DROID="/path/to/your/cordova/cordova-fetch/bin/android/node_modules/cordova-android"; 
export CORDOVA_IOS="/path/to/your/cordova/cordova-fetch/bin/ios/node_modules/cordova-ios";
source ~/.bash_profile 
cd ~/Desktop/ 
cordova create test-app com.test.app test-app && cd test-app 
cordova platform add $CORDOVA_DROID 
cordova platform add $CORDOVA_IOS
cordova build android
#In a normal situation cordova will download a gradle which is 60+MB. Since this operation can take a while I suggest you to setup an environment variable that will save you lots of time.
# Go to https://services.gradle.org/distributions/
# and find the distribution that fits your needs
# mine was gradle-4.0.2-all.zip
# download it and place it somewhere in your disk. Consider a location that can be persisted over time.

vim ~/.bash_profile 
export CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL=file:///path/to/your/gradle-4.0-all.zip
# That's all

最新更新