In this blog post, we will go through the process of including an external library, in our case an NPM package, in a UI5 application that serves as a frontend for CAP application. We will cover the setup of the necessary tools and the development of the custom control that will glue the UI5 app and the library together. Finally, we will deploy the entire project to the BTP and include the UI5 app in a Launchpad Site using SAP Build Work Zone, Standard Edition.
I am aware that there are already several great blog posts on the subject of including external libraries in UI5 but going through the development myself, facing the different approaches to include an NPM package, I found that an end-to-end example including the deployment and the integration with CAP would have been very helpful. The code for this project can of course be found in a GitHub repository.
For the development, we will use the SAP Business Application Studio but most of the steps can be executed in VS Code as well with the right extensions installed.
As mentioned before in our scenario we will assume that the UI5 app will serve as the UI for a CAP application. So we will use the cds CLI to run our app in the development environment. Additionally, we will use the cds-plugin-ui5 since it integrates the UI5 development server provided by the UI5 Tooling into our cds sever and thus allows us to use the benefits of the UI5 Tooling without starting a UI5 development server separately.
To integrate the NPM package we will use ui5-tooling-modules. In the following section, I will elaborate, on how I came to that decision.
There are two approaches to how an external library can be integrated into a UI5 application.
In my opinion second one is the better approach for the following reasons:
For these reasons, we will use the ui5-tooling-modules.
Now let’s dive into development. I will assume that you already have a locally working CAP application that delivers some data that you want to display in your custom control.
As an example I have prepared a CAP app with a single service that returns common AI buzzwords which we want to display in a word cloud in the UI. The word cloud will be the custom control we are going to implement. It will have one aggregation, the words to display. To layout the word cloud we will use the NPM package d3-cloud. The entire code of the project can be found in the corresponding GitHub repository.
As the first step, we need to prepare our CAP application for deployment, because afterward when we add our UI we want the mta.yaml to be updated with the deployment configuration for the UI as well. To do so we first run
cds add mta
Then we add XSUAA and HANA Cloud to our production profile by running
cds add xsuaa,hana --for production
Now we have a working mta.yaml for the deployment of the CAP app.
For the UI setup, we first need to add the configuration for the managed approuter to our mta.yaml. This is the only step that, to my knowledge, must be done in the BAS if you do not want to type in the configuration in the mta.yaml manually.
Next, we need to generate our initial UI5 application. If you want to do this in VS Code make sure you have all the Fiori Tools extensions installed.
The steps conducted so far in this section are the same as for any other UI5 app to be used in SAP Build Work Zone, Standard Edition. The following is to integrate the UI5 development server into our cds server:
npm i --save-dev cds-plugin-ui5
"cds": {
...,
"cds-plugin-ui5": {
"modules": {
"<module-name>": {
"configFile": "ui5.yaml"
}
}
}
}
<module-name> needs to be replaced with the module name you chose for the UI5 app in the generation wizard. In case you do not remember it: It is the name of the folder of your UI5 app in the app folder of your project.
cds watch
Now let’s integrate the NPM package.
npm i --save-dev @ui5/cli@3 @sap/ux-ui5-tooling ui5-tooling-modules
"ui5": {
"dependencies": [
...
"ui5-tooling-modules",
"@sap/ux-ui5-tooling"
]
}
server:
customMiddleware:
- name: fiori-tools-proxy
afterMiddleware: compression
configuration:
backend:
- path: /resources
url: https://ui5.sap.com
- path: /test-resources
url: https://ui5.sap.com
- path: /odata
url: http://localhost:4004
- name: ui5-tooling-modules-middleware
afterMiddleware: compression
configuration:
addToNamespace: true
...
builder:
...
customTasks:
...
- name: ui5-tooling-modules-task
afterTask: replaceVersion
configuration:
addToNamespace: true
...
Notice the option addToNamespace being set to true. This makes the ui5-tooling-modules bundle the NPM package such that they can also be used in SAP Build Work Zone, Standard Edition. According to the documentation of the ui5-tooling-modules this only works seamlessly for non-UI5 packages. For details, please look at the documentation.
src="https://sapui5.hana.ondemand.com/1.120.0/resources/sap-ui-core.js"
Change it to the following to make the app load the UI5 resources through the UI5 development server:
src="resources/sap-ui-core.js"
npm i --save-dev d3-cloud
Now you are all set to use the library. You can import them into your Control or Controller as follows:
sap.ui.define([
...
"d3-cloud"
], function(..., cloud) {
"use strict";
return Control.extend("aibuzzwords.control.WordCloud", {
...
});
});
If you then run the app using cds watch and open it in your browser, the ui5-tooling-modules will resolve the packages on the fly.
Now we can start developing our controls. For the word cloud, I developed two of them: The parent control WordCloud which is responsible for computing the layout using the d3-cloud library and the child control Word which is used in the words aggregation of the WordCloud control. I think the code mostly speaks best for itself. Nevertheless, I would like to mention some quirks of UI5 I encountered during development.
Now we are going to deploy our application to SAP BTP. First, make sure that you have access to a Subaccount with an available SAP HANA Cloud and a subscription to SAP Build Work Zone, Standard Edition.
Then build your application by running
npm run build
This script should have been generated during Step 1. If not, you can find it here.
Now login to your Subaccount using the Cloud Foundry CLI and target the space you want to deploy to.
Run the deploy script
npm run deploy
If you do not have one, you can find it here. Once the script has finished, you can add your UI5 app to a Launchpad Site in SAP Build Work Zone, Standard Edition as described in step 4 “Create your SAP Build Work Zone, standard edition site” of this tutorial.
Based on my experience getting the application to appear in SAP Build Work Zone, Standard Edition is the most error-prone task in the development. Therefore, here are some troubleshooting tips to addressing the most common issues:
"crossNavigation": {
"inbounds": {
"AiBuzzword-view": {
"semanticObject": "AiBuzzword",
"action": "view",
"title": "{{flpTitle}}",
"signature": {
"parameters": {},
"additionalParameters": "allowed"
}
}
}
}
If you are missing this intent, your app will also not appear in SAP Build Work Zone, Standard Edition.
Now we have worked our way through the entire process of developing a UI5 application using an external library and deploying it to SAP BTP. I admit that there is still a lot of configuration to do at the beginning but I think the developer experience afterwards is worth it. If you feel that anything is unclear or that I overlooked an important aspect, feel free to leave a comment.