ES6 module support for JerryScript

    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.

    module.js

    1. export exported_value;
    • 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'
    • anonymous default exports
      • export default function () {}
    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. import * as module from './module.js';
    2. print(">> Engine: " + module.engine);
    3. print(">> Version: " + module.version);

    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. 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. }

    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