Third-party libraries in a UI5 TypeScript app
Using third-party libraries might be a bit more complicated in UI5 with TypeScript. The solution to do this is way easier than you would expect. Around a year ago, Peter Muessig already shared the solution for this. Maybe you’ve already seen this blog post of Peter so you know how to use this:
It could be that you missed it or just didn’t know it also works for UI5 with TypeScript.
In this blogs, I want to point out that the same approach is valid for UI5 with TypeScript by providing a simple example.
Start by generating a new UI5 TypeScript app following the steps in this blog post:
As an example, I will add the third-party library “mobx” to my project by running the following command from in the project folder:
npm i -D mobx
For making this work in UI5 we need the UI5 middleware and builder taks “ui5-tooling-modules”. This one can also be installed with the following command:
npm i -D ui5-tooling-modules
Once both are installed, configure the UI5 tooling to use the middleware and task by adding it to the UI5 dependencies:
Next, the “ui5-tooling-modules” task and middleware needs to be configured. Actually, you just have to add it as a task for the builder and as a middleware for the server configuration.
This will make sure that the app can load the third-party library when running and it will be added to the “dist” folder when building:
You are ready to use the third-party library like any other control/library of the UI5 framework, check line 5:
By importing “mobx” at the top, TypeScript will give me full autocompletion support with syntax checks in case of errors.
I’m simply using “mobx” to observe any changes in the JSONModel:
The view will simply show the value of the JSONModel:
I also added a button that changes the value of the description which automatically updates the value thanks to “mobx”. Feel free to look a bit deeper into the project.
Simply run “npm start” to test the app:
Building the app is just as before by running the build script:
The only difference now is that it will also copy the “mobx” library from the node_modules into the “resources” folder of the app:
The build process will also handle the third-party library differently compared to UI5 controls/libraries to make it resolvable:
All fine until here, you can run and build the app which will work perfect! Although this might fail when running this app from in a Fiori Launchpad on-premise or in BTP. Once deployed, the app will not be able to find the library as it will search in the resources folder. All requests to this folder are forwarded to the UI5 SDK.
As a solution you have to configure the build task as followed: “prependPathMappings”
This will add UI5 config in the component.js which maps the third-party library with the path to the library in the project:
With this you should be good to go for consuming third-party libraries in UI5 with TypeScript.
More information regarding the task and middleware: https://www.npmjs.com/package/ui5-tooling-modules
The demo app: https://github.com/lemaiwo/UI5WithThirdPartyLib