ES6 modules let you split the JavaScript of a widget into separate files. This keeps the Javascript tab short, lets you reuse the same code in several widgets, and lets you use libraries that come only as ES6 modules. When the same widget is on a page more than once, the browser loads each module file only once.
There are three ways to use a module: include it on the js/css Includes tab, import it at the top of the Javascript tab, or import it from your code when you need it.
Step 1
Open your widget in the widget creator. On the Assets tab, set an assets folder for the widget and upload your module file to it. To learn how, see Assets.
Step 2
To include the module file, open the js/css Includes tab. Under Choose From Browser, select the checkbox next to the file, so it is added to Js Includes.
Click the Include Settings (gear) icon next to the file, select Include as ES6 module and click Update. The file is now loaded as a module.
Step 3
To import a module at the top of your code, open the Javascript tab and select load as module below the code editor. Then write the import on the first line. You can use {{uc_assets_url}} for the URL of the assets folder:
import { myFunction } from "{{uc_assets_url}}my-module.js";
myFunction();
Step 4
To import a module only when you need it, use the import() function in your code. This works with or without load as module:
import("{{uc_assets_url}}my-module.js").then(function(module){
module.myFunction();
});
Click Update to save the widget.