THIS 22.1

ECMAScript 6 Modules

ECMAScript 6 modules

T/HIS 22.1 supports ES6 modules. For more information on ES6 modules, please refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules.

Prior to support for ES6 modules, the only way to split up a script in T/HIS was to use the non-standard Use() functionality in the Oasys LS-DYNA Environment software. ES6 modules now give JavaScript built-in support for modular programming using the import and export keywords. T/HIS supports both static and dynamic imports for modules and this section gives a brief guide to how T/HIS locates modules.

To be able to support ES6 modules, T/HIS has to compile the script in a different way to a 'normal' script that does not use modules. So that T/HIS can tell how to compile the script we use a different extension .mjs for scripts that use modules. This follows the convention used by V8 and Node.js. Alternatively, if you prefer, you can put a special module comment at the top of the script and continue to use the extension .js (or whatever other extension you prefer).

When importing modules using import then if the module filename is an absolute filename T/HIS can locate the file directly. However if a relative filename is used T/HIS will search for the file in the following order.

  1. Relative to the directory that the main script is in.
  2. Relative to any parent module directory.
  3. Relative to any script_directory preference specified in a preference file given by a -pref=xxxx command line option.
  4. Relative to any script_directory preference specified in a preference file in the current directory.
  5. Relative to the script directory specified in the HOME directory. By default this will be $HOME/primer_library/scripts but this can be changed with the script_directory preference in the HOME oa_pref file.
  6. Relative to the script directory specified in the OA_INSTALL directory. By default this will be $OA_INSTALL/primer_library/scripts but this can be changed with the script_directory preference in the OA_INSTALL oa_pref file.
  7. Relative to the script directory specified in the OA_ADMIN directory. By default this will be $OA_ADMIN/primer_library/scripts but this can be changed with the script_directory preference in the OA_ADMIN oa_pref file.
  8. Relative to any module_directory preference specified in a preference file given by a -pref=xxxx command line option (unset by default).
  9. Relative to any module_directory preference specified in a preference file in the current directory (unset by default).
  10. Relative to any module_directory preference specified in the HOME oa_pref file (unset by default).
  11. Relative to any module_directory preference specified in the OA_INSTALL oa_pref file (unset by default).
  12. Relative to any module_directory preference specified in the OA_ADMIN oa_pref file (unset by default).

Note that the non-standard Use() functionality and ES6 modules cannot both be used when compiling a script. You must use one or the other. Where possible you should now use ES6 modules in preference as they are now part of core JavaScript.

Individual module files can be encrypted if required so if you want to protect only some parts of your code/process and leave the rest of it open/visible this can easily be done.

One difference between using the non-standard Use() method and ES6 modules is that with the Use() method T/HIS could merge all of the individual files back into a single file using the Merge command which could then be encrypted if required to only have to give out a single file instead of a 'package'. For ES6 modules an external tool such as rollup.js or Webpack is required to merge the files. Once combined into a single file, T/HIS can encrypt it.