ECMAScript 6 Modules
ECMAScript 6 Modules
REPORTER supports ES6 modules. For more information on ES6 modules please refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules.
ES6 modules give JavaScript built-in support for modular programming using the import and export keywords. REPORTER now supports both static and dynamic imports for modules.
To be able to support ES6 modules, REPORTER has to compile the script in a different way to a 'normal' script that does not use modules. In order for REPORTER to detect whether a script uses modules, the file being run needs to have the extension .mjs. This follows the convention used by V8 and Node.js. Alternatively, you can use the extension .js, but you then need to add a special "// module: TRUE" comment on one of the first twenty lines of the script. For script items, the module comment is necessary as you are not running a script file.
All "normal" scripts in REPORTER share a "context", which means that all variables and functions declared in the global scope are shared across these scripts. Therefore, any variable or function defined in one script can be accessed or modified by another script running in the same instance of REPORTER. Sharing data between scripts can also be done using template variables.
Note, however, that modules work differently. Any variables or functions declared in the global scope of a module script will only be available in the "context" of that script. The modules will still have access to any variables or functions declared in the global scope of "normal" scripts.
When importing modules, if using a relative module filename, REPORTER will search for the file in the following order:
- Relative to the directory that the main script is in
- Relative to any parent module directory
- Relative to the current directory
- Relative to the script directory specified in the HOME directory. By default this will be $HOME/reporter_library/scripts but this can be changed with the script_directory preference in the HOME oa_pref file
- Relative to the script directory specified in the OA_INSTALL directory. By default this will be $OA_INSTALL/reporter_library/scripts but this can be changed with the script_directory preference in the OA_INSTALL oa_pref file
- Relative to the script directory specified in the OA_ADMIN directory. By default this will be $OA_ADMIN/reporter_library/scripts but this can be changed with the script_directory preference in the OA_ADMIN oa_pref file
Keep in mind that if you are using a script item as a module, this will be saved to a temporary directory to allow it to be compiled and executed. That means that the algorithm will first search there to resolve any module relative paths.