Sources Management

JavaScript Sources Location

The JavaScript sources of an application must be located in the project folder src/main/js. All JavaScript files (*.js) found in this folder, at any level, are processed.

JavaScript Sources Load Order

When several JavaScript files are found in the sources folder, they are loaded in alphabetical order of their relative path. For example, the following source files:

└── main
    └── js
        ├── components
        │   ├── component1.js
        │   └── component2.js
        ├── ui
        │   └── widgets.js
        ├── app.js
        ├── feature1.js
        └── feature2.js

are loaded in this order:

  1. app.js
  2. components/component1.js
  3. components/component2.js
  4. feature1.js
  5. feature2.js
  6. ui/widgets.js

JavaScript Sources Load Scope

All the code of the JavaScript source files are loaded in the same scope. It means a variable or function defined in a source file can be used in another one if it has been loaded first. In this example:

function sum(a, b) {
    return a + b;
print("5 + 3 = " + sum(5, 3));

the file src/main/js/lib.js is loaded before src/main/js/main.js so the function sum can be used in src/main/js/main.js.

JavaScript Sources Processing

JavaScript sources need to be processed before being executed. This processing is done in the following cases:

  • when building the project with MMM.
  • when developing the project in MicroEJ SDK. The MicroEJ SDK detects any change in JavaScript sources folder (addition/update/deletion) to trigger the processing.