Class “Module”

Object > Module

Represents a JavaScript module as outlined in the commonJS standard.

Type: Module extends Object
Constructor: public
Singleton: No
Namespace: tabris
Direct subclasses: None
JSX Support: No

See also:

Modules
TSX module-addpath.tsx [► Run in Playground]
TSX module-define.tsx [► Run in Playground]

Constructor

new Module(id, parent, content)

Parameter Type Description
id string  
parent Module | null  
content ModuleLoader | object  

Methods

require(request)

Finds a module relative to the id of this module and returns the exports object of that module. Throws if no matching module can be found.

Parameter Type Description
request string  

Returns: object

Static Methods

addPath(options)

Maps imports matching the given patterns to the associated paths. Patterns may contain exactly one wildcard (‘*’) and must not start with ‘/’ or ‘.’. Paths are relative to ‘baseUrl’, may contain one wildcard and must start with ‘.’. The order of the paths determines the order of the path lookup. The ‘baseUrl’ must start with ‘/’, which refers to the project root directory.

Parameter Type Description
options {
  baseUrl: string, // optional
  paths: {[pattern]: string[]}
}
Contains the paths and baseUrl. If omitted, ‘baseUrl’ defaults to ‘/’.

Returns: undefined

See also:

TSX module-addpath.tsx [► Run in Playground]

addPath(pattern, paths)

Maps imports matching the given patterns to the associated paths. Short for `addPath({baseUrl: ‘/’, paths: {[pattern]: paths});

Parameter Type Description
pattern string That import pattern to match.
paths string[] The module paths to look up for the given pattern

Returns: undefined

See also:

TSX module-addpath.tsx [► Run in Playground]

createLoader(url)

Loads the given JavaScript file from the given (local or http) url and wraps it as a module loader function.

Parameter Type Description
url string  

Returns: ModuleLoader

createRequire(path)

Creates a “require” function that finds a module relative to the given path. If found the exports are returned. Throws if no matching module can be found.

Parameter Type Description
path string An absolute path, beginning with “/”. The path does not have to point to an existing file.

Returns: (request) => exports

define(path, exports)

Defines a module at the given path. It will be available for import as though there was a file at that location. Can also be used to override an actual module if it was not imported yet.

Parameter Type Description
path string The path of the new module. Must start with a /, which is the directory of the project’s package.json. Keep in mind that the source directory may not be the same at runtime if the code is pre-processed.
exports any The exports of the new module. This can be any type, though typically it is an object.

Returns: undefined

See also:

TSX module-define.tsx [► Run in Playground]

execute(code, url)

Evaluates the given JavaScript code and returns the result of the last expression. The url is used to identify the source in stack traces.

Parameter Type Description
code string  
url string  

Returns: unknown

getSourceMap(url)

Returns the source map object (decoded from base64 and parsed from JSON) for the JavaScript file of the given url. Returns null if no source map can be found. This method only works with code side-loaded via the tabris CLI serve command.

Parameter Type Description
url string  

Returns: unknown

load(url)

Loads a text from the given (local or http) url and returns its content. File is expected to be utf-8 encoded. This is a blocking operation, in almost all cases it is preferable to use fetch() or the file system API to read a text file.

Parameter Type Description
url string  

Returns: string

readJSON(url)

Loads the JSON file from the given (local or http) url, parses it and returns the result. This is a blocking operation, in almost all cases it is preferable to use fetch() or the file system API to obtain and parse a JSON file.

Parameter Type Description
url string  

Returns: unknown

Properties

exports

Type: object
Settable: No

This property can only be set via constructor. Once set, it cannot change anymore.

id

Full id (path) of the module

Type: string
Settable: No

This property can only be set via constructor. Once set, it cannot change anymore.

parent

The module that first required this module

Type: Module
Settable: No

This property can only be set via constructor. Once set, it cannot change anymore.