You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ni...@apache.org on 2016/05/05 22:28:55 UTC

cordova-lib git commit: CB-11194 Improve cordova load time

Repository: cordova-lib
Updated Branches:
  refs/heads/master f409d27cb -> 441fd90e8


CB-11194 Improve cordova load time

 This closes #434


Project: http://git-wip-us.apache.org/repos/asf/cordova-lib/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-lib/commit/441fd90e
Tree: http://git-wip-us.apache.org/repos/asf/cordova-lib/tree/441fd90e
Diff: http://git-wip-us.apache.org/repos/asf/cordova-lib/diff/441fd90e

Branch: refs/heads/master
Commit: 441fd90e8a9e7e647ca3c19a796bbde01734cc50
Parents: f409d27
Author: Nikhil Khandelwal <ni...@microsoft.com>
Authored: Thu May 5 14:38:26 2016 -0700
Committer: Nikhil Khandelwal <ni...@microsoft.com>
Committed: Thu May 5 15:28:07 2016 -0700

----------------------------------------------------------------------
 cordova-common/cordova-common.js               | 51 +++++++++++----------
 cordova-common/src/ConfigChanges/ConfigFile.js | 44 ++++++++++--------
 cordova-common/src/util/addProperty.js         | 13 ++++++
 cordova-lib/cordova-lib.js                     | 34 ++++++++++----
 cordova-lib/src/cordova/prepare.js             |  5 +-
 cordova-lib/src/cordova/restore-util.js        |  2 +-
 cordova-lib/src/cordova/util.js                |  6 +--
 cordova-lib/src/platforms/PlatformApiPoly.js   |  8 +++-
 cordova-lib/src/plugman/registry/registry.js   |  4 +-
 9 files changed, 104 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/441fd90e/cordova-common/cordova-common.js
----------------------------------------------------------------------
diff --git a/cordova-common/cordova-common.js b/cordova-common/cordova-common.js
index e85a701..dcaf7a4 100644
--- a/cordova-common/cordova-common.js
+++ b/cordova-common/cordova-common.js
@@ -17,27 +17,30 @@
     under the License.
 */
 
-exports = module.exports = {
-    events: require('./src/events'),
-    superspawn: require('./src/superspawn'),
-
-    ActionStack: require('./src/ActionStack'),
-    CordovaError: require('./src/CordovaError/CordovaError'),
-    CordovaLogger: require('./src/CordovaLogger'),
-    CordovaExternalToolErrorContext: require('./src/CordovaError/CordovaExternalToolErrorContext'),
-    PlatformJson: require('./src/PlatformJson'),
-    ConfigParser: require('./src/ConfigParser/ConfigParser.js'),
-    FileUpdater: require('./src/FileUpdater'),
-
-    PluginInfo: require('./src/PluginInfo/PluginInfo.js'),
-    PluginInfoProvider: require('./src/PluginInfo/PluginInfoProvider.js'),
-
-    PluginManager: require('./src/PluginManager'),
-
-    ConfigChanges: require('./src/ConfigChanges/ConfigChanges.js'),
-    ConfigKeeper: require('./src/ConfigChanges/ConfigKeeper.js'),
-    ConfigFile: require('./src/ConfigChanges/ConfigFile.js'),
-    mungeUtil: require('./src/ConfigChanges/munge-util.js'),
-
-    xmlHelpers: require('./src/util/xml-helpers')
-};
+var addProperty = require('./src/util/addProperty');
+
+module.exports = { };
+
+addProperty(module, 'events', './src/events');
+addProperty(module, 'superspawn', './src/superspawn');
+
+addProperty(module, 'ActionStack', './src/ActionStack');
+addProperty(module, 'CordovaError', './src/CordovaError/CordovaError');
+addProperty(module, 'CordovaLogger', './src/CordovaLogger');
+addProperty(module, 'CordovaExternalToolErrorContext', './src/CordovaError/CordovaExternalToolErrorContext');
+addProperty(module, 'PlatformJson', './src/PlatformJson');
+addProperty(module, 'ConfigParser', './src/ConfigParser/ConfigParser');
+addProperty(module, 'FileUpdater', './src/FileUpdater');
+
+addProperty(module, 'PluginInfo', './src/PluginInfo/PluginInfo');
+addProperty(module, 'PluginInfoProvider', './src/PluginInfo/PluginInfoProvider');
+
+addProperty(module, 'PluginManager', './src/PluginManager');
+
+addProperty(module, 'ConfigChanges', './src/ConfigChanges/ConfigChanges');
+addProperty(module, 'ConfigKeeper', './src/ConfigChanges/ConfigKeeper');
+addProperty(module, 'ConfigFile', './src/ConfigChanges/ConfigFile');
+addProperty(module, 'mungeUtil', './src/ConfigChanges/munge-util');
+
+addProperty(module, 'xmlHelpers', './src/util/xml-helpers');
+

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/441fd90e/cordova-common/src/ConfigChanges/ConfigFile.js
----------------------------------------------------------------------
diff --git a/cordova-common/src/ConfigChanges/ConfigFile.js b/cordova-common/src/ConfigChanges/ConfigFile.js
index dd9ebbc..ad12f38 100644
--- a/cordova-common/src/ConfigChanges/ConfigFile.js
+++ b/cordova-common/src/ConfigChanges/ConfigFile.js
@@ -17,13 +17,17 @@
 var fs = require('fs');
 var path = require('path');
 
-var bplist = require('bplist-parser');
-var et   = require('elementtree');
-var glob = require('glob');
-var plist = require('plist');
-
-var plist_helpers = require('../util/plist-helpers');
-var xml_helpers = require('../util/xml-helpers');
+var modules = {};
+var addProperty = require('../util/addProperty');
+
+// Use delay loading to ensure plist and other node modules to not get loaded
+// on Android, Windows platforms
+addProperty(module, 'bplist', 'bplist-parser', modules);
+addProperty(module, 'et', 'elementtree', modules);
+addProperty(module, 'glob', 'glob', modules);
+addProperty(module, 'plist', 'plist', modules);
+addProperty(module, 'plist_helpers', '../util/plist-helpers', modules);
+addProperty(module, 'xml_helpers', '../util/xml-helpers', modules);
 
 /******************************************************************************
 * ConfigFile class
@@ -67,7 +71,7 @@ function ConfigFile_load() {
     // the same in a future release
     if (ext == '.xml' || ext == '.appxmanifest') {
         self.type = 'xml';
-        self.data = xml_helpers.parseElementtreeSync(filepath);
+        self.data = modules.xml_helpers.parseElementtreeSync(filepath);
     } else {
         // plist file
         self.type = 'plist';
@@ -76,8 +80,8 @@ function ConfigFile_load() {
         //       Do we still need to support binary plist?
         //       If yes, use plist.parseStringSync() and read the file once.
         self.data = isBinaryPlist(filepath) ?
-                bplist.parseBuffer(fs.readFileSync(filepath)) :
-                plist.parse(fs.readFileSync(filepath, 'utf8'));
+                modules.bplist.parseBuffer(fs.readFileSync(filepath)) :
+                modules.plist.parse(fs.readFileSync(filepath, 'utf8'));
     }
 }
 
@@ -88,7 +92,7 @@ ConfigFile.prototype.save = function ConfigFile_save() {
     } else {
         // plist
         var regExp = new RegExp('<string>[ \t\r\n]+?</string>', 'g');
-        fs.writeFileSync(self.filepath, plist.build(self.data).replace(regExp, '<string></string>'));
+        fs.writeFileSync(self.filepath, modules.plist.build(self.data).replace(regExp, '<string></string>'));
     }
     self.is_changed = false;
 };
@@ -98,14 +102,14 @@ ConfigFile.prototype.graft_child = function ConfigFile_graft_child(selector, xml
     var filepath = self.filepath;
     var result;
     if (self.type === 'xml') {
-        var xml_to_graft = [et.XML(xml_child.xml)];
-        result = xml_helpers.graftXML(self.data, xml_to_graft, selector, xml_child.after);
+        var xml_to_graft = [modules.et.XML(xml_child.xml)];
+        result = modules.xml_helpers.graftXML(self.data, xml_to_graft, selector, xml_child.after);
         if ( !result) {
             throw new Error('grafting xml at selector "' + selector + '" from "' + filepath + '" during config install went bad :(');
         }
     } else {
         // plist file
-        result = plist_helpers.graftPLIST(self.data, xml_child.xml, selector);
+        result = modules.plist_helpers.graftPLIST(self.data, xml_child.xml, selector);
         if ( !result ) {
             throw new Error('grafting to plist "' + filepath + '" during config install went bad :(');
         }
@@ -118,11 +122,11 @@ ConfigFile.prototype.prune_child = function ConfigFile_prune_child(selector, xml
     var filepath = self.filepath;
     var result;
     if (self.type === 'xml') {
-        var xml_to_graft = [et.XML(xml_child.xml)];
-        result = xml_helpers.pruneXML(self.data, xml_to_graft, selector);
+        var xml_to_graft = [modules.et.XML(xml_child.xml)];
+        result = modules.xml_helpers.pruneXML(self.data, xml_to_graft, selector);
     } else {
         // plist file
-        result = plist_helpers.prunePLIST(self.data, xml_child.xml, selector);
+        result = modules.plist_helpers.prunePLIST(self.data, xml_child.xml, selector);
     }
     if (!result) {
         var err_msg = 'Pruning at selector "' + selector + '" from "' + filepath + '" went bad.';
@@ -140,7 +144,7 @@ function resolveConfigFilePath(project_dir, platform, file) {
 
     if (file.indexOf('*') > -1) {
         // handle wildcards in targets using glob.
-        matches = glob.sync(path.join(project_dir, '**', file));
+        matches = modules.glob.sync(path.join(project_dir, '**', file));
         if (matches.length) filepath = matches[0];
 
         // [CB-5989] multiple Info.plist files may exist. default to $PROJECT_NAME-Info.plist
@@ -167,7 +171,7 @@ function resolveConfigFilePath(project_dir, platform, file) {
         } else if (platform == 'android') {
             filepath = path.join(project_dir, 'res', 'xml', 'config.xml');
         } else {
-            matches = glob.sync(path.join(project_dir, '**', 'config.xml'));
+            matches = modules.glob.sync(path.join(project_dir, '**', 'config.xml'));
             if (matches.length) filepath = matches[0];
         }
         return filepath;
@@ -180,7 +184,7 @@ function resolveConfigFilePath(project_dir, platform, file) {
 // Find out the real name of an iOS project
 // TODO: glob is slow, need a better way or caching, or avoid using more than once.
 function getIOSProjectname(project_dir) {
-    var matches = glob.sync(path.join(project_dir, '*.xcodeproj'));
+    var matches = modules.glob.sync(path.join(project_dir, '*.xcodeproj'));
     var iospath;
     if (matches.length === 1) {
         iospath = path.basename(matches[0],'.xcodeproj');

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/441fd90e/cordova-common/src/util/addProperty.js
----------------------------------------------------------------------
diff --git a/cordova-common/src/util/addProperty.js b/cordova-common/src/util/addProperty.js
new file mode 100644
index 0000000..5156a2b
--- /dev/null
+++ b/cordova-common/src/util/addProperty.js
@@ -0,0 +1,13 @@
+module.exports = function addProperty(module, property, modulePath, obj) {
+    
+    obj = obj || module.exports;
+    // Add properties as getter to delay load the modules on first invocation
+    Object.defineProperty(obj, property, {
+        configurable: true,
+        get: function () {
+            var delayLoadedModule = module.require(modulePath);
+            obj[property] = delayLoadedModule;
+            return delayLoadedModule;
+        }
+    });
+};

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/441fd90e/cordova-lib/cordova-lib.js
----------------------------------------------------------------------
diff --git a/cordova-lib/cordova-lib.js b/cordova-lib/cordova-lib.js
index be57db2..1252412 100644
--- a/cordova-lib/cordova-lib.js
+++ b/cordova-lib/cordova-lib.js
@@ -18,19 +18,35 @@
 */
 
 // For now expose plugman and cordova just as they were in the old repos
+
+
+function addProperty(obj, property, modulePath) {
+    // Add properties as getter to delay load the modules on first invocation
+    Object.defineProperty(obj, property, {
+        configurable: true,
+        get: function () {
+            var module = require(modulePath);
+            // We do not need the getter any more
+            obj[property] = module;
+            return module;
+        }
+    });
+}
+
 exports = module.exports = {
     set binname(name) {
         this.cordova.binname = name;
     },
     get binname() {
-       return this.cordova.binname;
+        return this.cordova.binname;
     },
-    plugman: require('./src/plugman/plugman'),
-    cordova: require('./src/cordova/cordova'),
-    events: require('cordova-common').events,
-    configparser: require('cordova-common').ConfigParser,
-    cordova_platforms: require('./src/platforms/platforms'),
-    ////  MAIN CORDOVA TOOLS API
-    PluginInfo: require('cordova-common').PluginInfo,
-    CordovaError: require('cordova-common').CordovaError
+    get events() { return require('cordova-common').events },
+    get configparser() { return require('cordova-common').ConfigParser },
+    get PluginInfo() { return require('cordova-common').PluginInfo },
+    get CordovaError() { return require('cordova-common').CordovaError }
+
 };
+
+addProperty(module.exports, 'plugman', './src/plugman/plugman');
+addProperty(module.exports, 'cordova', './src/cordova/cordova');
+addProperty(module.exports, 'cordova_platforms', './src/platforms/platforms');
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/441fd90e/cordova-lib/src/cordova/prepare.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/prepare.js b/cordova-lib/src/cordova/prepare.js
index 3aa61b2..dd82d52 100644
--- a/cordova-lib/src/cordova/prepare.js
+++ b/cordova-lib/src/cordova/prepare.js
@@ -28,7 +28,6 @@ var cordova_util      = require('./util'),
     Q                 = require('q'),
     restore           = require('./restore-util'),
     path              = require('path'),
-    browserify = require('../plugman/browserify'),
     config            = require('./config'),
     _ = require('underscore');
 
@@ -113,8 +112,10 @@ function preparePlatforms (platformList, projectRoot, options) {
                 }
             })
             .then(function () {
-                if (options.browserify)
+                if (options.browserify) {
+                    var browserify = require('../plugman/browserify');
                     return browserify(project, platformApi);
+                }
             });
         });
     }));

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/441fd90e/cordova-lib/src/cordova/restore-util.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/restore-util.js b/cordova-lib/src/cordova/restore-util.js
index b816a35..2420734 100644
--- a/cordova-lib/src/cordova/restore-util.js
+++ b/cordova-lib/src/cordova/restore-util.js
@@ -22,7 +22,6 @@ var cordova_util = require('./util'),
     path         = require('path'),
     Q            = require('q'),
     fs           = require('fs'),
-    plugin       = require('./plugin'),
     events       = require('cordova-common').events,
     cordova      = require('./cordova'),
     semver      = require('semver'),
@@ -123,6 +122,7 @@ function installPluginsFromConfigXML(args) {
             cli_variables: pluginEntry.variables,
             searchpath: args.searchpath
         };
+        var plugin = require('./plugin');
         return plugin('add', installFrom, options);
     }, function (error) {
         // CB-10921 emit a warning in case of error

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/441fd90e/cordova-lib/src/cordova/util.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/cordova/util.js b/cordova-lib/src/cordova/util.js
index a055332..75ad1d6 100644
--- a/cordova-lib/src/cordova/util.js
+++ b/cordova-lib/src/cordova/util.js
@@ -24,11 +24,9 @@ var fs            = require('fs'),
     CordovaError  = require('cordova-common').CordovaError,
     shell         = require('shelljs'),
     url           = require('url'),
-    npm           = require('npm'),
     nopt          = require('nopt'),
     Q             = require('q'),
-    semver        = require('semver'),
-    superspawn    = require('cordova-common').superspawn;
+    semver        = require('semver');
 
 // Global configuration paths
 var global_config_path = process.env['CORDOVA_HOME'];
@@ -197,6 +195,7 @@ function getInstalledPlatformsWithVersions(project_dir) {
     var platforms_on_fs = listPlatforms(project_dir);
 
     return Q.all(platforms_on_fs.map(function(p) {
+        var superspawn    = require('cordova-common').superspawn;
         return superspawn.maybeSpawn(path.join(project_dir, 'platforms', p, 'cordova', 'version'), [], { chmod: true })
         .then(function(v) {
             result[p] = v || null;
@@ -397,6 +396,7 @@ function getLatestMatchingNpmVersion(module_name, version) {
  * @returns {Promise} Promise for an array of versions.
  */
 function getAvailableNpmVersions(module_name) {
+    var npm = require('npm');
     return Q.nfcall(npm.load).then(function () {
         return Q.ninvoke(npm.commands, 'view', [module_name, 'versions'], /* silent = */ true).then(function (result) {
             // result is an object in the form:

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/441fd90e/cordova-lib/src/platforms/PlatformApiPoly.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/platforms/PlatformApiPoly.js b/cordova-lib/src/platforms/PlatformApiPoly.js
index 1258325..6f6c90c 100644
--- a/cordova-lib/src/platforms/PlatformApiPoly.js
+++ b/cordova-lib/src/platforms/PlatformApiPoly.js
@@ -20,7 +20,6 @@
 var Q = require('q');
 var fs = require('fs');
 var path = require('path');
-var unorm = require('unorm');
 var shell = require('shelljs');
 var semver = require('semver');
 
@@ -474,7 +473,12 @@ function getCreateArgs(destinationDir, projectConfig, options) {
     // CB-6992 it is necessary to normalize characters
     // because node and shell scripts handles unicode symbols differently
     // We need to normalize the name to NFD form since iOS uses NFD unicode form
-    args.push(platformName == 'ios' ? unorm.nfd(projectConfig.name()) : projectConfig.name());
+    var name = projectConfig.name();
+    if (platformName == 'ios') {
+        var unorm = require('unorm');
+        name = unorm.nfd(name);
+    }
+    args.push(name);
 
     if (options.customTemplate) {
         args.push(options.customTemplate);

http://git-wip-us.apache.org/repos/asf/cordova-lib/blob/441fd90e/cordova-lib/src/plugman/registry/registry.js
----------------------------------------------------------------------
diff --git a/cordova-lib/src/plugman/registry/registry.js b/cordova-lib/src/plugman/registry/registry.js
index 08562d8..2772524 100644
--- a/cordova-lib/src/plugman/registry/registry.js
+++ b/cordova-lib/src/plugman/registry/registry.js
@@ -23,8 +23,7 @@ var npm = require('npm'),
     path = require('path'),
     Q = require('q'),
     npmhelper = require('../../util/npm-helper'),
-    events = require('cordova-common').events,
-    unpack = require('../../util/unpack');
+    events = require('cordova-common').events;
 
 module.exports = {
     settings: null,
@@ -123,6 +122,7 @@ function fetchPlugin(plugin) {
         events.emit('log', 'Fetching plugin "' + plugin + '" via npm');
         return Q.ninvoke(npm.commands, 'cache', ['add', plugin])
         .then(function (info) {
+            var unpack = require('../../util/unpack');
             var pluginDir = path.resolve(npm.cache, info.name, info.version, 'package');
             // Unpack the plugin that was added to the cache (CB-8154)
             var package_tgz = path.resolve(npm.cache, info.name, info.version, 'package.tgz');


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org