docs

Step 2: Bootstrap (TypeScript)

Before we can do something with OpenUI5, we need to load and initialize it. This process of loading and initializing OpenUI5 is called bootstrapping. Once this bootstrapping is finished, we simply display an alert.


Preview

An alert “UI5 is ready” is displayed

An alert "UI5 is ready" is displayed


Coding

You can view all files at OpenUI5 TypeScript Walkthrough - Step 2: Bootstrap and download the solution as a zip file.


package.json

As we’d like to use TypeScript with our project, we first need to install the TypeScript package. It provides the necessary tools and utilities to compile TypeScript code into JavaScript.

Open a terminal in the app root folder and execute the following command:

npm install typescript --save-dev

This installs the latest stable version of TypeScript to your project and adds it as development dependency to your package.json.


tsconfig.json (New)

Let’s create the file tsconfig.json in the app root folder to indicate that this folder is the root of a TypeScript project. This file specifies the root files and the compiler options required to compile the project.

Specify the compiler options as follows:

{
    "compilerOptions": {
      "target": "es2023",
      "module": "es2022",
      "moduleResolution": "node",
      "skipLibCheck": true,
      "allowJs": true,
      "strict": true,
      "strictPropertyInitialization": false,
      "rootDir": "webapp",
      "baseUrl": "./",
      "paths": {
        "ui5/walkthrough/*": ["webapp/*"]
      }
    },
    "include": ["webapp/**/*"]
  }

webapp/index.ts (New)

Create a new index.ts script in the webapp folder and add a native alert() method with the message “UI5 is ready” to it. We’ll integrate this script to the index.html page next.

alert("UI5 is ready");

webapp/index.html

In this step, we change the index.html page to make it load the OpenUI5 framework from the webserver provided by UI5 Tooling. We initialize the core modules with the following configuration options:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>UI5 TypeScript Walkthrough</title>
	<script
		id="sap-ui-bootstrap"
		src="resources/sap-ui-core.js"
		data-sap-ui-theme="sap_horizon"
		data-sap-ui-compat-version="edge"
		data-sap-ui-async="true"
		data-sap-ui-on-init="module:ui5/walkthrough/index"
		data-sap-ui-resource-roots='{
			"ui5.walkthrough": "./"
		}'>
	</script>
</head>
<body>
	<div>Hello World</div>
</body>
</html>

package.json

Now it’s time to enhance our tooling setup once again. This time we install some custom middleware for the ui5-server to facilitate the handling of our development project.

Open a terminal and navigate to the app root folder. Then execute the following command:

npm install ui5-middleware-livereload ui5-middleware-serveframework ui5-tooling-transpile --save-dev

This will install the ui5-middleware-livereload, ui5-middleware-serveframework, and ui5-tooling-transpile tooling extensions and add them as development dependencies to your package.json:


ui5.yaml

Next, we need to do some additional configuration work for our UI5 Tooling setup.

Open a terminal in the app root folder and execute the following commands:

Next, we have to configure the tooling extensions we installed from npm in our UI5 Tooling setup, so we can use them in our project. To hook a custom task into a certain build phase of a project, it needs to reference another task that will get executed before or after it. The same applies for a custom middleware:

Note:

Middleware configurations are applied in the order in which they are defined.

framework:
  name: OpenUI5
  version: "1.139.0"
  libraries:
    - name: sap.ui.core
    - name: themelib_sap_horizon
builder:
  customTasks:
  - name: ui5-tooling-transpile-task
    afterTask: replaceVersion
server:
  customMiddleware:
  - name: ui5-tooling-transpile-middleware
    afterMiddleware: compression
  - name: ui5-middleware-serveframework
    afterMiddleware: compression
  - name: ui5-middleware-livereload
    afterMiddleware: compression

Now you can benefit from live reload on changes, built framework resources at development time, and make use of TypeScript in OpenUI5!

Note:

During its initial run, the ui5-middleware-serveframework middleware will build the framework, which can take a while. In all following steps, the build will not happen again and the framework is served from the built resources.

Parent topic:Walkthrough Tutorial (TypeScript)

Next:Step 1: Hello World! (TypeScript)

Previous:Step 3: Controls (TypeScript)

Related Information

What is a tsconfig.json?

Bootstrapping: Loading and Initializing

Content Security Policy

UI5 Tooling: Consuming OpenUI5 Libaries

NPM Package: ui5-middleware-livereload

NPM Package: ui5-middleware-serveframework

NPM Package: ui5-tooling-transpile

UI5 Tooling: Custom Tasks

UI5 Tooling: Custom Server Middleware