Component-preload.js: files are separately loaded
Dear UI5-Team,
I use grunt-openui5 to create the Component-preload.js.
After the application has been loaded into Chrome, I checked the network tab. Here is an excerpt:
...
manifest.json
Component-preload.js
...
So the Component-preload.js is successfully loaded. I checked its content: all the relevant files like views, contollers etc. are there.
sap.ui.require.preload({
...
"home/view/App.view.xml": "<mvc:View ...
"home/controller/App.controller.js": "sap.ui.define([\"jqu...
...
}, "home3/Component-preload");
Strangely in the network tab below the Component-preload.js all the files of that Component are listed. This can't be right.
OpenUI5 version: 1.58.4
This sounds and looks like a resource name mismatch. The preload file in your snippet above got the name home3/Component-preload, but the content is named home/something.
With what name do you require the stuff at runtime (home or home3)? The content of the preload is found by its name only (home/something). If it is addressed as home3/something, the preload content won't be taken.
If this is the problem, you should use consistent names.
I checked my source text and the Component-preload.js, it's all consistent. I made a typo in the first comment, so the source text is
sap.ui.require.preload({
...
"home3/view/App.view.xml": "<mvc:View ...
"home3/controller/App.controller.js": "sap.ui.define([\"jqu...
...
}, "home3/Component-preload");
@born2dev can you share the content of the home3/Component.js file?
Yes, of course. It's
sap.ui.define(
[
"sap/ui/Device",
"sap/ui/core/UIComponent",
"com/abcd/ui/home3/model/Models"
],
function(Device, UIComponent, Models) {
"use strict";
return UIComponent.extend("com.abcd.ui.home3.Component", {
metadata: {
manifest: "json"
},
init: function() {
UIComponent.prototype.init.apply(this, arguments);
this.setModel(Models.createDeviceModel(), "device");
this.setModel(Models.createSettingsModel(), "settingsMdl");
this.getRouter().initialize();
},
getContentDensityClass: function() {
if (!this._sContentDensityClass) {
if (!Device.support.touch) {
this._sContentDensityClass = "sapUiSizeCompact";
} else {
this._sContentDensityClass = "sapUiSizeCozy";
}
}
return this._sContentDensityClass;
}
});
}
);
So your app namespace is "com/abcd/ui/home3", not just "home3" (as defined in the UIComponent.extend call).
The content of your Component-preload.js should look like this
sap.ui.require.preload({
//...
"com/abcd/ui/home3/view/App.view.xml": "<mvc:View ..."
"com/abcd/ui/home3/controller/App.controller.js": "sap.ui.define([\"jqu..."
//...
}, "com/abcd/ui/home3/Component-preload");
I would say that "home3/view/App.view.xml" is correct because the namespace com.abcd.ui is assigend to the sap-ui-bootstrap as data-sap-ui-resourceroots='{"com.abcd.ui" : "./"}'
So the folder structure is
root Gruntfile.js src main webapp index.html home3 manifest.json Component.js view controller ...
Gruntfile.js:
openui5_preload : {
component : {
options : {
resources : {
cwd : 'src/main/webapp',
src: [
'**/*.js',
'**/*.fragment.html',
'**/*.fragment.json',
'**/*.fragment.xml',
'**/*.view.html',
'**/*.view.json',
'**/*.view.xml',
'**/*.properties',
'**/*.json',
]
},
dest : 'src/main/webapp'
},
components : true
}
}
The "resourceRoots" define where certain modules for a namespace should be loaded from, but still the full namespace is used and must be part of the preload configuration. It does not define a shortcut or something.
In this case you either need to move your app in sub-folders to represent the namespace
- root
- Gruntfile.js
- src
- main
- webapp
- index.html
- com
- abcd
- ui
- home3
- manifest.json
- Component.js
- view
- controller
- ...
- home3
- ui
- abcd
- webapp
- main
or you configure the namespace prefix: 'com/abcd/ui':
openui5_preload : {
component : {
options : {
resources : {
cwd : 'src/main/webapp',
prefix: 'com/abcd/ui',
src: [
'**/*.js',
'**/*.fragment.html',
'**/*.fragment.json',
'**/*.fragment.xml',
'**/*.view.html',
'**/*.view.json',
'**/*.view.xml',
'**/*.properties',
'**/*.json',
]
},
dest : 'src/main/webapp'
},
components : true
}
}
prefix: 'com/abcd/ui', leads to
sap.ui.require.preload({
"com/abcd/ui/home3/view/App.view.xml": "<mvc:View ..."
which is wrong.
"home3/view/App.view.xml": "<mvc:View ...
is the correct (relative) path to the Component-preload.js.
It should work, right?
As @matz3 explained, the keys in the preload bundle are not relative paths, but module IDs.
We don't use paths as this makes the preload bundle independent from source locations. When multiple locations are combined (e.g. libraries from different origins), the bundle and the loader don't have to know the original locations.
Did you give the above preload bundle a try?
Yes, I have given prefix: 'com/abcd/ui' a try and it works. Thank you!
The issue here is that the explanation under https://www.npmjs.com/package/grunt-openui5#prefix was not very clear to me. Now it is.
@born2dev thanks for your feedback. I totally agree that this should be explained a bit better. Tracked in https://github.com/SAP/grunt-openui5/issues/82