What happens if a library preload file / library dependencies is not updated in the UI5-manifest? - sapui5

In the best practices for app developers, the Load Only What You Really Need section is written:
Keep Your Library Dependencies Up To Date
A library preload file, the library styles and text translations are loaded for every library you define in the application descriptor or the OpenUI5 bootstrap. Always define libraries in the manifest and remove all libraries that you do not intend to use in your code.
For instance:
"sap.ui5": {
"dependencies": {
"minUI5Version": "1.85.0",
"libs": {
"sap.ui.core": {},
"sap.m": {},
"sap.ui.layout": {}
}
}
...
}
My questions:
Do I understand it correctly, that in the libs I have to add all the libraries which appear in sap.ui.define([…]) of any controller of the UI5-app?
What happens if I forget to add a library here? Is it just excluded from the Component-preload.js and loaded in a non-optimized way or there are more serious disadvantages?

Yes, in order to benefit from performance optimizations, you have to keep the manifest.json section /sap.ui5/dependencies/libs always in sync with the UI5 libraries* (incl. custom ones) that can be identified from the application code and visible in API Reference if the lib is from the framework. I.e. not only "sap.ui.core" and "sap.m" but also, for example, "sap.ui.table" if one of the views contains sap.ui.table.Table.
If you have libraries which don't need to be preloaded immediately on app start but are supposed to be loaded on-demand, you can assign "lazy": true to those libraries, e.g.:
"libs": {
"sap.ui.core": {},
"sap.m": {},
"sap.ui.comp": {
"lazy": true
}
}
But that "lazy": true is purely informational. In the app, such libs should be loaded explicitly before they're in use:
// Core required from "sap/ui/core/Core"
await Core.loadLibrary("sap.ui.comp", /*async:*/ true);
Further info regarding loadLibrary: https://sdk.openui5.org/api/sap.ui.core.Core#methods/loadLibrary
* In the context of UI5, a "library" is a named bundle of controls, elements, types, interfaces, etc.
List of such libraries can be found at:
SAPUI5: https://ui5.sap.com/resources/sap-ui-version.json
OpenUI5: https://sdk.openui5.org/resources/sap-ui-version.json
Adding a single module (e.g. sap/ui/Device) to the /sap.ui5/dependencies/libs section won't work. To see which library the module belongs to, check the "Library" information from the corresponding API reference page:
The same applies when working with UI5 tooling.
All libraries required by your project must be listed in the libraries section of the framework configuration:
framework:
name: OpenUI5
version: 1.108.0
libraries:
- name: sap.ui.core
- name: sap.m
- name: sap.ui.table
- name: ...

The component-preload.js only contains elements of your app, never any UI5 code.
To answer your question, take a look at this small ui5 demo app.
When loading it with:
"sap.ui5": {
"dependencies": {
"minUI5Version": "1.108.0",
"libs": {
"sap.ui.core": {},
"sap.m": {}
}
}
}
Open the DevTools and take a look at the Network Tab. Because of the references of the libs in the manifest.json, both library-preload.js are loaded before your Component.js.
When you remove:
"sap.ui.core": {},
"sap.m": {}
from the manifest.json and run the app again, then you can see that all UI5 elements are loaded one-by-one after your component.js. This just slows down your app.

Related

How do react-css-modules (babel) and css-loader (webpack) work together?

When using webpack and babel together, one needs to configure both in order to use React CSS Modules. For example:
webpack.config.js will need a rule like this:
{
// Translates CSS into CommonJS modules
loader: 'css-loader',
options: {
modules: {
mode: "local",
localIdentName: CSS_CLASS_NAME_PATTERN,
},
sourceMap: true
}
babel.config.js will need a plugin like this:
[
'react-css-modules',
{
generateScopedName: CSS_CLASS_NAME_PATTERN,
filetypes: {
'.scss': {
syntax: 'postcss-scss',
plugins: ['postcss-nested']
}
},
}
]
Why the need to configure CSS Modules in two places? How the two work together? I.e. what happens in what order?
They don't. css-loader does its own thing: class name transformation in CSS, and replacement of CSS imports in JS code by mappings between original and generated names.
babel-plugin-react-css-modules works independently, and it replaces styleName attributes of react components by className with correct generated names. To do so it calculates class name mappings independently from css-loader, that's why it needs separate configuration matching that of css-loader, and that's why after a few years being abandoned by its creators it has compatibility issues with latest css-loader (css-loader changed internal class name generation logic).
Shameless self-promo: I maintain an up-to-date fork of babel-plugin-react-css-modules which solves compatibility issues with latest css-loader versions.

Refer separately deployed components for reuse in an ui5 app

i'm developing some apps, for which everyone needs to show a document in the same way. For this i created a new component which handles my documents in a separate component. I then just want to reuse this component from my different apps.
To embed my reuse component i used something like this in my view.xml:
<core:ComponentContainer
name="de.mycomp.base.DocViewer"
component="de.mycomp.base.DocViewer"
settings='\{"param1":"value1"\}'/>
To access it during runtime i have to declare the namespace of the reuse component and associate it with an resource-url. To achive this, i used the following coding in the init-method of my Component.js which uses my reuse-component DocViewer.
jQuery.sap.registerModulePath("de.mycomp.base.DocViewer", "/sap/bc/ui5_ui5/sap/zdocviewer");
zdocviewer in this case is the name of the bsp-application to which the reuse-component was deployed on premise. To have this also work in webide and on SAP-Cloud-Plattform i needed to add an entry to neo-app.json
like this:
{
"path": "/sap/bc/ui5_ui5/sap/zdocviewer",
"target": {
"type": "application",
"name": "docviewer"
},
"description": "my base document viewer"
},
where docviewer is the name of the deployed app with the reuse component on SCP.
The type application implies that this destination is an app.
This works so far on premise and on sap cloud.
but my problem is that i dont want to have the registerModulePath in my Component.js. Nearly every configuration of components takes place in the manifest.json file. So i tried to move this coding line to configuration in manifest.json, but i failed so far.
Here's what i did:
i added a dependency in section sap.ui5 and there in the dependency-entry like this:
"components": {
"de.dvelop.base.DvelopBaseDocViewer": {
"lazy": true
}
}
i added the following entry to sap.ui5 part
"resourceRoots": {
"de.dvelop.base.DvelopBaseDocViewer": "/sap/bc/ui5_ui5/sap/zdocviewer"
},
The problem here is, that its not allowed to use absolute paths in the value-part of the resource-root entries. So this is invalid:
- /sap/bc/ui5_ui5/sap/zdocviewer
- ../sap/bc/ui5_ui5/sap/zdocviewer
Only this is valid:
- sap/bc/ui5_ui5/sap/zdocviewer
- ./sap/bc/ui5_ui5/sap/zdocviewer
So this annotation is only usable for embedded components, where the reuse-component is in the same deployed project. But this is not my understanding of a reuseable component. So changing something in that component would make it neccessary to copy the files to all the app-projects using the component. And they all have to be deployed again.
So for now i made a fallback to the jquery.sap.registerModulePath because this works with deployed components to refer them from other independent components.
Or does anybody have an idea how to handle this better or more proper within manifest.json?
kind regards
Matthias
see here: ui5 reuse components: https://github.com/Yelcho/UI5-Comp-Routing
you mixed up here old and new approches, wrinting all steps in this answer would be very long. Here brief step to step approche:
define your componentUsages
"componentUsages": {
"myDoc": {
"name": "com.company.myDoc",
"settings": {},
"componentData": {},
"lazy": true
}}
define path mapping, in resourceRoots
"resourceRoots": {
"com.company.myDoc": "/sap/bc/ui5_ui5/sap/zdocviewer"
},
define a targed type component
"targets": {
"myDocTarget": {
"type": "Component",
"usage": "myDoc"
}
use nested routing
.getRouter()
.navTo("myDocRoute", {
id: oBindingContext.getProperty("CategoryID")
}, {
products: {
route: "list",
parameters: {
}
}
});

SAP custom translations for standard SAPUI5 application

I am currently implementing an extension to a standard application from SAP Marketing.
The extension contains new texts that need to be translated into different languages. In my previous extensions I could use the translation key of the standard application for my extension as well. The first line in the i18n.properties file in this case was always structured as follows:
# SAPUI5 TRANSLATION-KEY XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
However, in the standard app that I'm currently editing, it looks like this:
# This is the resource bundle for Campaigns
# __ldi.translation.uuid = 8e965d5e-c905-4b60-ac2a-205abb14046
In transaction se63, the translation key (is it even a translation key?) is not found - either with hyphens or without. Furthermore, in the standard app, the translations are kept in a single file for each language (e.g., i18n_de.properties). That's why I'm not sure if there's even a translation key for this standard app.
I don't want to create a new translation key for my extension and use this one. Once I did this, all the translations of the standard app had to be maintained for the new translation key as well.
Is anyone familiar with this type of translation? How can I maintain the translations for my extension?
Best Regards,
Christian
I found some solution to my problem:
I generated a new translation key by running program /UI5/TEXT_FILE_GEN_TRANS_KEY in transaction se38
I created a new folder i18n and added a i18nCustom.properties file to it. Then I added the translation key and default translations to the file just like for a regular i18n.properties file.
In the next step, I added the following code to the sap.ui5 property of the extension's manifest.json file to initiate the custom translation file:
"models": {
"i18nCustom": {
"type": "sap.ui.model.resource.ResourceModel",
"settings": {
"bundleName": "<Your Component>.i18n.i18nCustom"
}
}
}
Please note that you have to use something like {i18nCustom>property} in your view now instead of using i18n model.
To enhance the standard translation file with the custom one, I added the following code to the BaseController. You could also add the code only in the controller whose view is using the custom translations.
onBeforeRendering: function() {
var i18n = this.getModel("i18n"); // Get the standard i18n file
var sBundleURL = this.getModel("i18nCustom").getResourceBundle().oUrlInfo.url;
i18n.enhance({bundleUrl:sBundleURL}); // Merge the custom i18n file with the standard one
}
Hope this helps if someone has the same problem.

How to use stage 3 syntax in svelte/sapper?

I want to use class property and private fields in my sapper project. Apparently they have to be preprocessed by babel right now.
I tried to add the corresponding babel plugins to rollup.config.js, only to realize a few things.
the babel rollup plugin is only used in legacy mode.
the server part doesn't use babel at all.
I tried to add the babel rollup plugin to the end of server plugins like this,
babel({
extensions: ['.js', '.mjs', '.html', '.svelte'],
runtimeHelpers: true,
exclude: ['node_modules/#babel/**'],
plugins: [
'#babel/plugin-proposal-class-properties',
'#babel/plugin-proposal-private-methods',
],
}),
But it doesn't seem to take effect at all.
I also added it to the client plugins (before the legacy entry), but it complained about I needed to add #babel/plugin-syntax-dynamic-import, so looks like babel has to recognize the whole syntax in order to preprocess, and I don't really want to compile dynamic import for modern browsers.
How do I enable the use of esnext syntax in sapper?
You would need to preprocess the contents of <script>, using the preprocess option in rollup-plugin-svelte:
plugins: [
svelte({
// ...
preprocess: {
script: ({ content }) => {
return transformWithBabel(content);
}
},
// ...
})
]
In an ideal world we'd have a ready-made preprocessor plugin for doing this; as it is, the transformWithBabel function is left as an exercise to the reader for now. Essentially it would involve import * as babel from '#babel/core' and using the Babel API directly, which I guarantee will be lots of fun.
Note that #babel/plugin-syntax-dynamic-import doesn't compile dynamic import, it only allows Babel to parse it. Without it, Babel can't generate a valid AST from the code inside <script>.

sails.js setup: How to make a node module available across the sails project (controller / model, etc)?

I just getting started with SailsJS as my first web framework on Node. Let's say I wanna add MomentJS in and use across the app. How to set it up?
you can use the bootstrap.js (in config/)
like:
module.exports.bootstrap = function (cb) {
sails.moment = require('moment');
cb();
};
in all Sails-Files you can use
sails.moment()
now.
If you're trying to include your node_modules into the client side, such as jQuery, AngularJS or one of the various many font libraries, then you can npm install them as normal, but just to be sure in sails you edit your tasks/config/copy.js and add a new block, example:
grunt.config.set('copy', {
dev: {
files: [{
expand:true,
cwd: './node_modules/font-awesome/fonts',
src: ['**/*'],
dest: '.tmp/public/fonts'
}
}
});
LESS can be #imported like normal without being copied around. Other assets will need to be copied as above. If you're using the sails linker then don't forget to add your JS paths to tasks/pipeline.js too (if necessary).
You can read more here:
http://ash.zi.vc/sails/2016/02/02/including-client-side-node-modules-in-my-sails-application/
It's not directly obvious how to sync npm modules to the web accessible directories.
SailsJS is no different to any other NodeJS app. So on top of your (say) Controller.js file, you do
var m = require("moment");
And you're good to go. #mdunisch's method will obviously let you use the moment package throughout your app, without having to do "require" in each file.