Skip to content

Require Library

Rohan Singh edited this page Feb 14, 2025 · 2 revisions

Exports

require

require(fileName: string): object

Loads a module from a file and returns its exports.

Configuration

Mond will, by default, load modules from the filesystem in the current working directory or relative to other scripts loaded by require. This behavior can be changed by configuring the RequireLibrary for the MondState so you can restrict which files can be loaded or bypass using the filesystem all together.

var state = new MondState(); // require is included by default
state.Libraries.Configure(libraries =>
{
  var requireLibrary = libraries.Get<RequireLibrary>();

  // default is [ "." ]
  requireLibrary.SearchDirectories = [ "/scripts" ];

  // default is true, adds the path to the script that called require() to the search directories
  requireLibrary.SearchBesideScript = true;
  
  requireLibrary.Resolver = (name, directories) =>
  {
    // return the resolved full path for module `name`, in any of the search `directories`
    return $"/scripts/{name}.mnd";
  };

  requireLibrary.Loader = resolvedName =>
  {
    // return the script source code for the path returned by the resolver method above
    return moduleSources[resolvedName];
});

Clone this wiki locally