Use RequireJS to Reference Third-Party JavaScript Libraries
If you want to use third-party JavaScript libraries in your App UI, you can import the library and add a requirejs statement to your App UI’s definition.
JS libraries can be imported at the App UI level or at the extension level. For typical usage within an App UI, import your JS library to the resources directory at the App UI level (extension1/sources/ui/self/applications/<app-ui-id>/resources). If you want to use external JS libraries with custom web components and fragments, import your JS library to the resources directory at the extension level (extension1/sources/ui/self/resources/).
- Open the
app.jsonfile for your App UI.- In the App UIs pane, select your App UI node, then click the JSON tab, or
- In the Source view, locate the file for your App UI under
extension1/sources/ui/self/applications/.
- Add a
requirejsstatement to the App UI’s definition. For example, if you’ve addedgl-matrix-min.jsto your App UI’s resources under…/applications/*`<app-ui-id>`*/resources/js/, add:"requirejs": { "paths": { "gl-matrix": "resources/js/gl-matrix-min" } }You can also use an expression as the value. For example, instead of
resources/js/gl-matrix-min, enter:"requirejs": { "paths": { "gl-matrix": "" } }Either way, make sure the
requirejsentry is a sibling of theidordescriptionentries. If arequirejssection already exists, simply add your entry underpaths.If you added a JS library (say,
HumanResourcesUtils) to your extension’s resources underextension1/sources/ui/self/resources/js/HumanResourcesUtils, yourrequirejsstatement might be similar to:"requirejs": { "paths": { "HumanResourcesUtils": "/ui/self/resources/js/HumanResourcesUtils" } }Where
HumanResourcesUtilsis a folder containing JS files such aspromotion.js,manager.js, andemployee.js. - To load and use your library in a module, use the
definestatement to make your library a dependency for your module. In your JS file, enter, for example:define(['gl-matrix', 'HumanResourcesUtils/promotion'], (glmatrix, Promotion) => { 'use strict'; ...