Feature
Add support for CommonJS modules to be included in the Titanium Mobile SDK itself, and automatically injected in to the Ti.* namespace.
Why
We are now developing several modules to be included in the core distribution that are CommonJS modules. Android has precedent for this (Ti.Yahoo.*), but iOS does not.
Test
An example "test.js" could be included in Ti SDK:
(function (Ti, Titanium, exports) {
var Test;
if (Titanium == undefined /* Android */) {
Test = {};
exports.bootstrap = function (TiSDK) {
Ti = Titanium = TiSDK;
return Test;
};
}
else {
Test = exports;
}
Test.retrieveSuccessLabel = function () {
return Ti.UI.createLabel({
text: 'Success!', textAlign: 'center',
color: '#000'
});
};
})(Ti, Titanium, exports);
On Android it would need to be bootstrapped (to follow the existing convention yahoo.js sets forth) in titanium.js:
Titanium.Test = require('test').bootstrap(Titanium);
But on iOS it can simply be require'd normally.
It would be used in an app like this:
var win = Ti.UI.createWindow({
backgroundColor: '#fff'
});
win.add(Ti.Test.retrieveSuccessLabel());
win.open();
We need this support to integrate ACS CommonJS module.
PR opened for one solution: https://github.com/appcelerator/titanium_mobile/pull/1373 In addition to the changes to TiHost.m and KrollBridge.mm, the following steps need to be taken to include the example CommonJS compiled module in core for iOS: 1. Create a module named "test" with ID "test". All JavaScript must be in assets/test.js. 2. Compile it and unzip to a temporary directory. 3. Copy libtest.a to iphone/lib/libtest.a in Titanium Mobile. 4. Add libtest.a to titanium.xcodeproj. 5. In site_scons/package.py, duplicate and update line 246 to copy libtest.a to the SDK distribution. 6. In support/iphone/builder.py, duplicate and update lines 1013-1014 to add a check to copy libtest.a if it does not already exist. 7. In support/iphone/iphone.py, duplicate and update line 116 to get a reference to libtest.a's path. 8. In support/iphone/iphone.py, duplicate and update line 122 to copy libcloud.a to generated projects.
Closing ticket as fixed.