openui5 icon indicating copy to clipboard operation
openui5 copied to clipboard

Component-preload.js: files are separately loaded

Open born2dev opened this issue 8 years ago • 11 comments

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

born2dev avatar Nov 16 '18 08:11 born2dev

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.

codeworrior avatar Nov 16 '18 08:11 codeworrior

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 avatar Nov 18 '18 09:11 born2dev

@born2dev can you share the content of the home3/Component.js file?

matz3 avatar Nov 19 '18 08:11 matz3

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;
      }

    });
  }
);

born2dev avatar Nov 20 '18 08:11 born2dev

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");

matz3 avatar Nov 20 '18 09:11 matz3

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
      }
    }

born2dev avatar Nov 20 '18 12:11 born2dev

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
                • ...

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
    }
}

matz3 avatar Nov 20 '18 12:11 matz3

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?

born2dev avatar Nov 20 '18 13:11 born2dev

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?

codeworrior avatar Nov 20 '18 14:11 codeworrior

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 avatar Nov 20 '18 15:11 born2dev

@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

matz3 avatar Nov 21 '18 15:11 matz3