OpenWGA 7.5 - OpenWGA Concepts and Features

Design and development » TMLScript

CommonJS support

CommonJS is a standard for creating JavaScript libraries that are portable between different server-side usages of JavaScript. It was created by the Node.js project, a server middleware for creating web applications and services.

Unfortunately CommonJS is no longer maintained and propagated by the Node.js team. However there are still a lot of CommonJS libraries out there that may prove to be useful in OpenWGA development and can be embedded into OpenWGA designs for use from TMLScript.

CommonJS libraries consist of one or more files with suffix ".js". To use in an OpenWGA project they should be put into a subfolder "/files/system/commonjs" in the design directory:

commonjs-lib.png

Once there they can be required from TMLScript code of this project by using the usual function require with the name of the CommonJS library file sans the suffix:

var handleBars = require("handlebars-v2.0.0");

The module object can then be used just like intended by the CommonJS module.

CommonJS Modules can also cross-reference themselves, just like usual, provided they are in the same OpenWGA design. It is also possible to use subfolders below "CommonsJS" to hold CommonJS modules. These are also addressed like subfolders in the name parameter of the "require" function (using "/" as path divider, unlike in OpenWGA design references).

For example: this would load a module from /files/system/commonjs/submodules/mymodule.js:

var myModule = require("submodules/mymodule");

Starting the ID with a double slash you can also load modules from other apps, provided these are readable for the current user. Continue after the double slash with the database key of that app, then after an additional slash, add the module ID. This loads "myexternalmodule.js" from a plugin with database key "plugin-myplugin":

var externalModule = require("//plugin-myplugin/myexternalmodule");