The module system

What modules are and how WRT handles them

What are WRT modules?

A module is a unit of code which is ran in its own context. Simply put, each script (whether referenced as a file or not) is classified as a WRT module.

The main module is the module where execution began (usually from a script file). This starting module will define the working directory, initialize the module cache for the instance, and provide the parameters for submodules running under the main (starting) module.

Including modules

WRT supports a minimal version of the CommonJS format to allow for module inclusion. All includes will be resolved relative to the module path directory of the main module (the directory where the main module is contained) unless an absolute path is specified.

To include modules, you need to use the await include(path: String) function to include them. include is not limited to just modules - you can also use it to include text files to get their contents directly. Modules which are included in the current context are considered to be submodules, since they are dependencies.

Modules are cached, which means that the module is only loaded once depending on the path specification where it was loaded from.

Including a module is easy, as demonstrated below:

const myModule = await include("./test.js");

console.log(myModule);

/* === contents of ./test.js below for the sake of demonstration === */

module.exports = {
    test: () => "Hello, world!"
}

Global modules

Global modules are modules which can simply be included by specifying a module name without a relative/absolute path. They are stored in C:/system/local/lib.

To define a module, simply create a module definition file and its accompanying script file. To do this, create the definition file at C:/system/local/lib/modules/my_module.json with the following contents:

{
    "libPath": "c:/system/local/lib/my_module.js",
    "moduleName": "my_module",
    "version": "1.0.0"
}

When complete, create the script file at `C:/system/local/lib/my_module.js with the following contents:

module.exports = {
    test: function() {
        alert("hello, world!");
    }
}

If all is done correctly, you can test your global module like so (in a separate WRT script):

const my_module = await include("my_module");
my_module.test();

Last updated