Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #65 from SAP/new-preload-format
Browse files Browse the repository at this point in the history
Usage of sap.ui.require.preload for library-preload.js >=1.54
  • Loading branch information
matz3 authored Mar 12, 2018
2 parents 581c547 + 6376e6f commit 6fac9ff
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 120 deletions.
27 changes: 27 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,24 @@ module.exports = function(grunt) {
components: '**'
},

'component_compat_140': {
options: {
resources: 'test/preload/fixtures/app',
dest: 'tmp/preload/component_compat_140',
compatVersion: '1.40'
},
components: '**'
},

'component_compat_154': {
options: {
resources: 'test/preload/fixtures/app',
dest: 'tmp/preload/component_compat_154',
compatVersion: '1.54'
},
components: '**'
},

'library_default_options': {
options: {
resources: 'test/preload/fixtures/library',
Expand All @@ -159,6 +177,15 @@ module.exports = function(grunt) {
libraries: '**'
},

'library_compat_154': {
options: {
resources: 'test/preload/fixtures/library',
dest: 'tmp/preload/library_compat_154',
compatVersion: '1.54'
},
libraries: '**'
},

'library_resource_prefix': {
options: {
resources: [
Expand Down
69 changes: 41 additions & 28 deletions tasks/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ var defaultResourcePatterns = [
var copyrightCommentsPattern = /copyright|\(c\)|released under|license|\u00a9/i;
var xmlHtmlPrePattern = /<(?:\w+:)?pre>/;

function createJSPreload(preloadObject) {
return 'sap.ui.require.preload(' + JSON.stringify(preloadObject.modules, null, '\t') + ', "' + preloadObject.name + '");';
}
function createLegacyJSPreload(preloadObject) {
return 'jQuery.sap.registerPreloadedModules(' + JSON.stringify(preloadObject, null, '\t') + ');';
}
function createLegacyJSONPreload(preloadObject) {
return JSON.stringify(preloadObject, null, '\t');
}

module.exports = function (grunt) {

grunt.registerMultiTask('openui5_preload', 'Create OpenUI5 preload files', function() {
Expand Down Expand Up @@ -117,44 +127,50 @@ module.exports = function (grunt) {
return;
}

if (options.compatVersion !== "edge") {
var aVersionMatch = options.compatVersion.match(/^([0-9]+)\.([0-9]+)$/);
if (!aVersionMatch) {
grunt.fail.warn('\'' + options.compatVersion + '\' is not a valid value for option compatVersion!');
return;
}
iMajor = parseInt(aVersionMatch[1], 10);
iMinor = parseInt(aVersionMatch[2], 10);
}

if (preloadType === 'libraries') {
preloadInfo = {
moduleName: 'library-preload',
indicatorFile: 'library.js',
processModuleName: function(moduleName) {
return moduleName.replace(/\//g, '.');
}
ext: '.js',
indicatorFile: 'library.js'
};

if (options.compatVersion !== "edge") {
var aVersionMatch = options.compatVersion.match(/^([0-9]+)\.([0-9]+)$/);
if (!aVersionMatch) {
grunt.fail.warn('\'' + options.compatVersion + '\' is not a valid value for option compatVersion!');
return;
}
iMajor = parseInt(aVersionMatch[1], 10);
iMinor = parseInt(aVersionMatch[2], 10);
}

if (options.compatVersion === "edge" || (iMajor === 1 && iMinor >= 40) || iMajor > 1) {
if (options.compatVersion === "edge" || (iMajor === 1 && iMinor >= 54) || iMajor > 1) {
// Build library-preload as .js file
preloadInfo.ext = ".js";
preloadInfo.processContent = function(content) {
return 'jQuery.sap.registerPreloadedModules(' + content + ');';
};
preloadInfo.createContent = createJSPreload;
} else if (iMajor === 1 && iMinor >= 40 && iMinor < 54) {
// Build library-preload as .js file
preloadInfo.createContent = createLegacyJSPreload;
} else {
// Build as .json file (legacy, needed because UI5 <1.40 only loads the .json files)
// Build as .json file (legacy, needed because UI5 < 1.40 only loads the .json files)
preloadInfo.ext = ".json";
preloadInfo.processModuleName = function(moduleName) {
return moduleName.replace(/\//g, '.');
};
preloadInfo.createContent = createLegacyJSONPreload;
}
} else {
preloadInfo = {
moduleName: 'Component-preload',
ext: '.js',
indicatorFile: 'Component.js',
processContent: function(content) {
return 'jQuery.sap.registerPreloadedModules(' + content + ');';
}
indicatorFile: 'Component.js'
};

if (options.compatVersion === "edge" || (iMajor === 1 && iMinor >= 54) || iMajor > 1) {
preloadInfo.createContent = createJSPreload;
} else {
preloadInfo.createContent = createLegacyJSPreload;
}

}


Expand Down Expand Up @@ -293,10 +309,7 @@ module.exports = function (grunt) {
preloadObject.modules[preloadFile] = fileContent;
});

var content = JSON.stringify(preloadObject, null, '\t');
if (typeof preloadInfo.processContent === 'function') {
content = preloadInfo.processContent(content);
}
var content = preloadInfo.createContent(preloadObject);

var destPath = options.dest;
var preloadResourceInfo = resourceMap[preloadFile];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my/app/Component-preload",
"modules": {
"my/app/Component.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/app/view/my.view.xml": "<my>XML</my>\n",
"my/app/view/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>"
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sap.ui.require.preload({
"my/app/Component.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/app/view/my.view.xml": "<my>XML</my>\n",
"my/app/view/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>"
}, "my/app/Component-preload");
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my/app/Component-preload",
"modules": {
"my/app/Component.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/app/view/my.view.xml": "<my>XML</my>\n",
"my/app/view/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>"
}
});
sap.ui.require.preload({
"my/app/Component.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/app/view/my.view.xml": "<my>XML</my>\n",
"my/app/view/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>"
}, "my/app/Component-preload");
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my/app/Component-preload",
"modules": {
"my/app/Component.js": "/* © */\n'use strict';\n\n/**\n* This is a copyright comment\n*/\n\n/* (c) */\n\n/* released under */\n/* normal comment */\n/* license */\n\n\nconsole.log('myJS');\n\n/**\n * This is a little comment\n */\nfunction myFunction(longVariableName, longerVariableName) {\n\treturn longVariableName + longerVariableName;\n}\n",
"my/app/view/my.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->",
"my/app/view/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>"
}
});
sap.ui.require.preload({
"my/app/Component.js": "/* © */\n'use strict';\n\n/**\n* This is a copyright comment\n*/\n\n/* (c) */\n\n/* released under */\n/* normal comment */\n/* license */\n\n\nconsole.log('myJS');\n\n/**\n * This is a little comment\n */\nfunction myFunction(longVariableName, longerVariableName) {\n\treturn longVariableName + longerVariableName;\n}\n",
"my/app/view/my.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->",
"my/app/view/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>"
}, "my/app/Component-preload");
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my/app/Component-preload",
"modules": {
"my/app/Component.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/app/view/my.view.xml": "<my>XML</my>\n",
"my/app/view/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>"
}
});
sap.ui.require.preload({
"my/app/Component.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/app/view/my.view.xml": "<my>XML</my>\n",
"my/app/view/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>"
}, "my/app/Component-preload");
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my.ui.lib.library-preload",
"name": "my/ui/lib/library-preload",
"modules": {
"my/ui/lib/library.js": "jQuery.sap.require(\"sap.ui.core.library\"),jQuery.sap.declare(\"my.ui.lib.library\"),sap.ui.getCore().initLibrary({name:\"my.ui.lib\",version:\"0.0.0\",dependencies:[\"sap.ui.core\"]});",
"my/ui/lib/myJS.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sap.ui.require.preload({
"my/ui/lib/library.js": "jQuery.sap.require(\"sap.ui.core.library\"),jQuery.sap.declare(\"my.ui.lib.library\"),sap.ui.getCore().initLibrary({name:\"my.ui.lib\",version:\"0.0.0\",dependencies:[\"sap.ui.core\"]});",
"my/ui/lib/myJS.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/ui/lib/my.view.xml": "<my>XML</my>\n",
"my/ui/lib/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>",
"my/ui/lib/foo.properties": "FOO=BAR\n"
}, "my/ui/lib/library-preload");
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my.ui.lib.library-preload",
"modules": {
"my/ui/lib/library.js": "jQuery.sap.require(\"sap.ui.core.library\"),jQuery.sap.declare(\"my.ui.lib.library\"),sap.ui.getCore().initLibrary({name:\"my.ui.lib\",version:\"0.0.0\",dependencies:[\"sap.ui.core\"]});",
"my/ui/lib/myJS.js": "/* © */\n\"use strict\";function myFunction(longVariableName,longerVariableName){return longVariableName+longerVariableName+\"\\u518d\\u6309\\u4e00\\u6b21\\u9000\\u51fa\\u4f19\\u62fc\"}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/ui/lib/my.view.xml": "<my>XML</my>\n",
"my/ui/lib/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>",
"my/ui/lib/foo.properties": "FOO=BAR\n"
}
});
sap.ui.require.preload({
"my/ui/lib/library.js": "jQuery.sap.require(\"sap.ui.core.library\"),jQuery.sap.declare(\"my.ui.lib.library\"),sap.ui.getCore().initLibrary({name:\"my.ui.lib\",version:\"0.0.0\",dependencies:[\"sap.ui.core\"]});",
"my/ui/lib/myJS.js": "/* © */\n\"use strict\";function myFunction(longVariableName,longerVariableName){return longVariableName+longerVariableName+\"\\u518d\\u6309\\u4e00\\u6b21\\u9000\\u51fa\\u4f19\\u62fc\"}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/ui/lib/my.view.xml": "<my>XML</my>\n",
"my/ui/lib/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>",
"my/ui/lib/foo.properties": "FOO=BAR\n"
}, "my/ui/lib/library-preload");
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my.ui.lib.library-preload",
"modules": {
"my/ui/lib/library.js": "jQuery.sap.require(\"sap.ui.core.library\"),jQuery.sap.declare(\"my.ui.lib.library\"),sap.ui.getCore().initLibrary({name:\"my.ui.lib\",version:\"0.0.0\",dependencies:[\"sap.ui.core\"]});",
"my/ui/lib/myJS.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/ui/lib/my.view.xml": "<my>XML</my>\n",
"my/ui/lib/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>",
"my/ui/lib/foo.properties": "FOO=BAR\n"
}
});
sap.ui.require.preload({
"my/ui/lib/library.js": "jQuery.sap.require(\"sap.ui.core.library\"),jQuery.sap.declare(\"my.ui.lib.library\"),sap.ui.getCore().initLibrary({name:\"my.ui.lib\",version:\"0.0.0\",dependencies:[\"sap.ui.core\"]});",
"my/ui/lib/myJS.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/ui/lib/my.view.xml": "<my>XML</my>\n",
"my/ui/lib/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>",
"my/ui/lib/foo.properties": "FOO=BAR\n"
}, "my/ui/lib/library-preload");

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 7 additions & 11 deletions test/preload/expected/library_resource_prefix/library-preload.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my.ui.lib.library-preload",
"modules": {
"my/ui/lib/library.js": "jQuery.sap.require(\"sap.ui.core.library\"),jQuery.sap.declare(\"my.ui.lib.library\"),sap.ui.getCore().initLibrary({name:\"my.ui.lib\",version:\"0.0.0\",dependencies:[\"sap.ui.core\"]});",
"my/ui/lib/myJS.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/ui/lib/my.view.xml": "<my>XML</my>\n",
"my/ui/lib/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>",
"my/ui/lib/foo.properties": "FOO=BAR\n"
}
});
sap.ui.require.preload({
"my/ui/lib/library.js": "jQuery.sap.require(\"sap.ui.core.library\"),jQuery.sap.declare(\"my.ui.lib.library\"),sap.ui.getCore().initLibrary({name:\"my.ui.lib\",version:\"0.0.0\",dependencies:[\"sap.ui.core\"]});",
"my/ui/lib/myJS.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/ui/lib/my.view.xml": "<my>XML</my>\n",
"my/ui/lib/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>",
"my/ui/lib/foo.properties": "FOO=BAR\n"
}, "my/ui/lib/library-preload");
14 changes: 5 additions & 9 deletions test/preload/fixtures/app-same-dest/my/app/Component-preload.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my/app/Component-preload",
"modules": {
"my/app/Component.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/app/view/my.view.xml": "<my>XML</my>\n",
"my/app/view/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>"
}
});
sap.ui.require.preload({
"my/app/Component.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/app/view/my.view.xml": "<my>XML</my>\n",
"my/app/view/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>"
}, "my/app/Component-preload");
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my.ui.lib.library-preload",
"modules": {
"my/ui/lib/library.js": "jQuery.sap.require(\"sap.ui.core.library\"),jQuery.sap.declare(\"my.ui.lib.library\"),sap.ui.getCore().initLibrary({name:\"my.ui.lib\",version:\"0.0.0\",dependencies:[\"sap.ui.core\"]});",
"my/ui/lib/myJS.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/ui/lib/my.view.xml": "<my>XML</my>\n",
"my/ui/lib/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>",
"my/ui/lib/foo.properties": "FOO=BAR\n"
}
});
sap.ui.require.preload({
"my/ui/lib/library.js": "jQuery.sap.require(\"sap.ui.core.library\"),jQuery.sap.declare(\"my.ui.lib.library\"),sap.ui.getCore().initLibrary({name:\"my.ui.lib\",version:\"0.0.0\",dependencies:[\"sap.ui.core\"]});",
"my/ui/lib/myJS.js": "/* © */\n\"use strict\";function myFunction(n,o){return n+o}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/ui/lib/my.view.xml": "<my>XML</my>\n",
"my/ui/lib/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>",
"my/ui/lib/foo.properties": "FOO=BAR\n"
}, "my/ui/lib/library-preload");
Loading

0 comments on commit 6fac9ff

Please sign in to comment.