Module format

Caplin Trader 4 uses the CommonJS module format, while retaining support for Caplin Trader 3’s module format, NamespacedJS.

Modules written in CommonJS format and modules written in NamespacedJS format are fully interoperable in Caplin Trader 4 and can reference, extend and inherit from each other.

To take advantage of advances in JavaScript tooling, the next major release of Caplin Trader following Caplin Trader 4 will drop support for NamespacedJS modules.

A comparison of NamespacedJS and CommonJS formats

This section compares two functionally-equivalent source files for an example module, Bar. One source file is written in NamespacedJS format and the other source file is written in CommonJS format.

CommonJS module format

The Bar module is defined in a file named abc/def/Bar.js. The only object revealed by the module is the object assigned to module.exports. The externally-defined class, Foo, is referenced by its fully qualified name in the invocation of require; thereafter, the class is referenced by a local variable.

To learn more about module format in BladeRunnerJS, see Modules on the BladeRunnerJS website.

var Foo = require('abc/def/Foo');

function Bar() {
     this.foo = new Foo();
     this.foo.doFoo();
     this.doBar();
}

Bar.prototype.doBar = function(){
    alert("Doing bar!");
}

module.exports = Bar;

NamespacedJS module format

The Bar module is defined in a file named abc/def/Bar.js. Variables that are declared with a namespace prefix are accessible from outside of the module. The externally-defined class, Foo, must be fully qualified in order for BladeRunnerJS’s class loader to correctly locate the file in which the class is defined: abc/def/Foo.js.

abc.def.Bar = function() {
    this.foo = new abc.def.Foo();
    this.foo.doFoo();
    this.doBar();
}

abc.def.Bar.prototype.doBar = function() {
    alert("Doing bar!");
};

Migration path from NamespacedJS to CommonJS format

Caplin Trader 4 supports modules of both formats: NamespacedJS and CommonJS. The next major release of Caplin Trader will drop support for NamespacedJS.

In order to maintain a clean upgrade path from Caplin Trader 4 to future releases of Caplin Trader, it is recommended that you migrate your NamespacedJS modules to CommonJS format.

Migrating your modules to CommonJS

Caplin provide a command-line tool to automate the majority of the work in converting NamespacedJS code to CommonJS formatted modules. The tool supports stepped migration: one blade, bladeset, or library at a time. For instructions and to download the tool, see the caplin/gc-cli page on GitHub. Please raise any issues with the tool by posting to the caplin/gc-cli repository or by contacting the Caplin support team.

For information on CommonJS compliance in Caplin Trader, see BladeRunnerJS Wiki: CommonJS compliance.

Using existing NamespacedJS modules with Caplin Trader 4

By default, Caplin Trader 4’s build tool, BladeRunnerJS, assumes that a module’s directory hierarchy contains JavaScript files in CommonJS module format.

To indicate to BladeRunnerJS that a module’s directory hierarchy contains NamespacedJS-formatted code, in the root of the directory hierarchy, create a file called .js-style containing the text 'namespaced-js'. To create the file .js-style in one step from the Linux command line, execute the following command from the root of the module’s directory hierarchy:

echo namespaced-js > .js-style

For more information on using NamespacedJS modules within Caplin Trader 4, see Namespaced-style code on the BladeRunnerJS website.

Modules in Caplin Trader 4.4

Caplin Trader 4.4 is the first version of Caplin Trader 4 to be released with all SDK modules in CommonJS format. This changes some aspects of the bundling process and may have implications for your code. For more information, see the CT 4.4 Upgrade Guide.


See also: