ES6 module support for JerryScript

The module system allows users to write import and export statements in scripts, which can be used to separate the logic of the application into custom modules. The standard’s relevant part can be found here. Embedders wishing to use native builtin modules with ES6 imports can use the Port API to do so.

General

If a script contains import statements, then JerryScript will open and evaluate the the referenced modules before the main script runs, resolving and creating bindings for the referenced identifiers in the process. It is not necessary to use any specific filename extensions for modules, JerryScript will try to open the given file paths as they are, but will try to normalize them before doing so. The exact normalization process is dependant on the port implementation provided. It is the user’s responsibility to verify that the given files are valid EcmaScript modules.

main.js

  1. import { exported_value } from "./module.js"
  2. print (exported_value);

module.js

  1. var exported_value = 42;
  2. export exported_value;

Supported features

  • exporting identifiers from the module’s lexical environment
    • specifying export names for the exported values
  • importing exported identifiers from a module
    • specifying local binding names for the imported values
  • module namespace imports
    • import * as module from 'module.js
  • indirect export statements
    • export {variable} from 'module.js'
  • star export statements
    • export * from 'module.js'
  • importing a module for side-effects
    • import 'module.js'
  • default import and export statements
    • export default local_identifier
    • import def from 'module.js'
  • anonymous default exports
    • export default function () {}

Example

  1. import {
  2. engine,
  3. version as v
  4. } from "./module.js"
  5. import { getFeatureDetails } from "./module_2.js"
  6. var version = "v3.1415";
  7. print("> main.js");
  8. print(">> Engine: " + engine);
  9. print(">> Version: " + v);
  10. print (">> " + getFeatureDetails());
  11. print (">> Script version: " + version);
  1. // module.js
  2. var _engine = "JerryScript";
  3. export _engine as engine;
  4. export var version = "1.0 (e92ae0fb)";
  1. // module_2.js
  2. var featureName = "EcmaScript modules";
  3. var year = 2018;
  4. export function getFeatureDetails() {
  5. return "Feature name: " + featureName + " | developed in " + year;
  6. }

Module namespace import statements

A module namespace object can be imported. In this case the local binding will contain an object holding the exported values of the module, including local exports and all indirect exports. Ambiguous exported names are exluded from the namespace object.

  1. import * as module from './module.js';
  2. print(">> Engine: " + module.engine);
  3. print(">> Version: " + module.version);

Indirect export statements

An export statement can transitively export variables from another module, either via named indirect exports or a star export statement. In this case the resolving process will follow the chain until it reaches a module containing a local binding for that export name. If there are multiple modules which satisfy the export, that means the export is ambiguous, and will result in a SyntaxError.

  1. import { a, b } from 'module.js'
  2. print (a + b);
  1. // module.js
  2. export var a = 2;
  3. export { b } from 'module2.js'
  1. // module2.js
  2. export var b = 40;

Default imports and exports

Each module can optionally provide a single default export by using the export default statement. Default exports can either reference identifiers in the module’s lexical environment, or be an anonymous default export, in which case they will only be accessible by an importing script.

  1. import defaultExport, { b as c } from 'module.js'
  2. print (defaultExport); // 2
  3. print (c ()); // 42
  1. // module.js
  2. export default 2;
  3. export function b () {
  4. return 42;
  5. }

Importing modules for side-effects

Evaluate a module without importing anything. Any errors encountered in the module will be propagated.

  1. import 'module.js' // > module.js
  2. // "> module.js" is printed
  3. b (); // (ReferenceError) b is not defined
  1. // module.js
  2. export function b () {
  3. print ("> module.js");
  4. return 42;
  5. }
  6. b ();

Unsupported features

  • snapshot