wilton.js  v202103141
 All Namespaces Functions
Functions
loader Namespace Reference

wilton/loader
Load resource files using logical module names. More...

Functions

String findModuleDirectory (Object|String module, Function|Undefined callback)
 Find out the full path to the specified module direcotory.
String findModulePath (Object|String module, Function|Undefined callback)
 Find out the full path to the specified module.
Object loadAppConfig (Object startupModule, Function|Undefined callback)
 Load config.json file from appdir/conf/ directory.
Object loadModuleJson (String modname, Function|Undefined callback)
 Load JSON file using logical module path.
String loadModuleResource (String modname, Object|Undefined options, Function|Undefined callback)
 Load resource file using logical module path.

Detailed Description

This module allows to load resource files that are stored inside the JavaScript source tree in file system directories on inside ZIP files.

Additonally it contains loadAppConfig() function that loads config.json file from its expected location inside appdir/conf/ directory.

Usage example:

// full path to the "foo/bar" module, may point to file system or ZIP path
var path = loader.findModulePath("foo/bar");
// load text file from module "foo"
var text = loader.loadModuleResource("foo/baz.txt");
// load JSON file from module "foo", parse it and return it as an object
var obj = loader.loadModuleJson("foo/boo.json");
// load application config from `appdir/conf/` directory
var conf = loader.loadAppConfig();

Function Documentation

String loader::findModuleDirectory ( Object|String  module,
Function|Undefined  callback 
)

Finds out directory where resides file of the specified module.

Parameters
moduleObject|String either module object or module ID String
callbackFunction|Undefined callback to receive result or error
Returns
String path to module directory
String loader::findModulePath ( Object|String  module,
Function|Undefined  callback 
)

Returns the full path to the specified module. Returned path may point either to file system or to ZIP path.

Note, that to get the path to module file in FS input module ID must include the file extension, example: module.id + ".js".

Parameters
moduleObject|String either module object or module ID String
callbackFunction|Undefined callback to receive result or error
Returns
String full path to the specified module
Object loader::loadAppConfig ( Object  startupModule,
Function|Undefined  callback 
)

This method expects that application is running using the "standard" directory structure:

  • appdir: application directory, can have arbitrary name
    • bin: (optional) contains executables and libraries
    • conf: contains config.json
    • log: contains log output files
    • work: contains temporary files
    • index.js: startup module

Loaded file contents are preprocessed replacing references to {{{appdir}}} with an actual path to application directory.

Parameters
startupModuleObject RequireJS startup module
callbackFunction|Undefined callback to receive result or error
Returns
Object configuration object parsed from config.json file
Object loader::loadModuleJson ( String  modname,
Function|Undefined  callback 
)

Loads JSON file with the specified logical module path from file system or from ZIP file.

Loaded JSON file is parsed and returned as an Object.

Parameters
modnameString logical module path to the JSON file
callbackFunction|Undefined callback to receive result or error
Returns
Object parsed contents of the specified JSON file
String loader::loadModuleResource ( String  modname,
Object|Undefined  options,
Function|Undefined  callback 
)

Loads text resource file with the specified logical module path from file system or from ZIP file.

Parameters
modnameString logical module path to the resource file
optionsObject|Undefined configuration object, can be omitted, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
String contents of the specified file

Options

  • hex Boolean whether data read from specified resources needs to be converted to HEX format before returning it to caller; false by default