Skip to content
yuchi edited this page Dec 30, 2014 · 2 revisions

Writing a cross-environment package/module

To write packages that work both on Node and Titanium you just need to add a single property on your package.json file, which is guid:

{
  "name": "my-module",
  "version": "1.0.0",
  "guid": "b876a690-6550-415c-9d0e-0976e32553e6"
}

See this package as an example.

Having a different name for Titanium

If you want to have a different require name (the one you use with require('module-name')) between Node and Titanium, you have to override the manifest of the generated module. In you package.json file write the following:

{
  "name": "my-module",
  "version": "1.0.0",
  "titaniumManifest": {
    "moduleid": "it.smc.mymodule",
    "guid": "b876a690-6550-415c-9d0e-0976e32553e6"
  }
}

See this package from Fokke Zanderberg as an example.

Porting a package from Node to Titanium

If the package you’re porting requires changes or some monkey-patching to make it work in Titanium, then you can easily wrap it, do your job, and export your module with the original name.

// index.js
module.exports = require('wrapped-module');
module.exports.somethingThatYouNeedToPatch = function () {
  // your patch…
};
{
  "name": "ti-wrapped-module",
  "version": "1.0.0",
  "titaniumManifest": {
    "moduleid": "wrapped-module"
  }
}

See this package which wraps superagent as an example.

Use a substitute package

If a dependency of your package doesn’t work in Titanium you can shim (re-implement) it with either a different package or a local file.

For example, if you want to use superagent you’ll need ti-superagent to make it work in Titanium. To make the substitution work use the following form in your package.json file:

{
  "name": "my-module",
  // you need both as a dependency
  "dependencies": {
    "superagent": "*",
    "ti-superagent": "*"
  },
  // and then inside `titanium` you can replace the packages
  "titanium": {
    "superagent": "ti-superagent"
  }
}

If you want to shim it locally then you can also do this:

{
  "name": "my-module",
  "dependencies": {
    "a-node-only-dep": "*"
  },
  "titaniumifier": {
    "a-node-only-dep": "./path/to/a/local/shim"
  }
}
// ./path/to/a/local/shim.js
module.exports = function () {
  return Titanium.Platform.architecture;
};