Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-7568] iOS: Using CommonJS Modules in Core Ti SDK

GitHub Issuen/a
TypeNew Feature
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2012-02-13T17:33:13.000+0000
Affected Version/sRelease 2.0.0
Fix Version/sSprint 2012-04, Release 2.0.0, Release 1.8.2
ComponentsiOS
Labelsn/a
ReporterDawson Toth
AssigneeDawson Toth
Created2012-02-07T14:02:26.000+0000
Updated2017-03-16T22:02:54.000+0000

Description

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();

Comments

  1. Neeraj Gupta 2012-02-07

    We need this support to integrate ACS CommonJS module.
  2. Dawson Toth 2012-02-08

    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.
  3. Lee Morris 2017-03-16

    Closing ticket as fixed.

JSON Source