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

Commit

Permalink
Create library-preload.js files instead of .json
Browse files Browse the repository at this point in the history
Old behaviour can be achieved by setting target option "compatVersion" to
something lower than "1.40" (see task 'library_compat_138' in
Gruntfile.js)

Fixes #45
  • Loading branch information
RandomByte committed Nov 28, 2016
1 parent 077af46 commit f0764d3
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 39 deletions.
18 changes: 18 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ module.exports = function(grunt) {
libraries: '**'
},

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

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

'library_resource_prefix': {
options: {
resources: [
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ Default:

Glob pattern(s) for finding relevant resources inside `cwd`. If set, the default patterns will be replaced.

##### compatVersion
Type: `string`
Default: `edge`

Sets the UI5 version used for compatibility mode in the format `<major>.<minor>`. Use this when building older UI5 releases to ensure full functionality.

Example:
When building for UI5 target version 1.38.x, use `compatVersion: '1.38'`.

#### dest
Type: `string`
Default value: `.`
Expand All @@ -181,7 +190,7 @@ Default value: `true`
Optional parameter to set compression/minification of the files or to provide
additional options.

- Javascript is minified using [UglifyJS2](https://github.com/mishoo/UglifyJS2)
- JavaScript is minified using [UglifyJS2](https://github.com/mishoo/UglifyJS2)
- XML is minified using [pretty-data](https://github.com/vkiryukhin/pretty-data)
- JSON is parsed for correctness and to remove extra whitespace

Expand Down
59 changes: 43 additions & 16 deletions tasks/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ module.exports = function (grunt) {
var options = this.options({
resources: [],
dest: null,
compress: true
compress: true,
compatVersion: "edge"
});

var resourceMap = {};
Expand Down Expand Up @@ -109,28 +110,54 @@ module.exports = function (grunt) {
}

['components', 'libraries'].forEach(function(preloadType) {
var iMajor, iMinor, preloadInfo, preloadOptions;

var preloadOptions = preloadData[preloadType];

preloadOptions = preloadData[preloadType];
if (!preloadOptions) {
return;
}

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

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);
}
} : {
moduleName: 'Component-preload',
ext: '.js',
indicatorFile: 'Component.js',
processContent: function(content) {
return 'jQuery.sap.registerPreloadedModules(' + content + ');';

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



if (preloadOptions === true) {
preloadOptions = '**';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my.ui.lib.library-preload",
"modules": {
Expand All @@ -8,4 +8,4 @@
"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"
}
}
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my.ui.lib.library-preload",
"modules": {
Expand All @@ -8,4 +8,4 @@
"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"
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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"
}
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my.ui.lib.library-preload",
"modules": {
Expand All @@ -8,4 +8,4 @@
"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"
}
}
});
11 changes: 11 additions & 0 deletions test/preload/expected/library_resource_prefix/library-preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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"
}
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
jQuery.sap.registerPreloadedModules({
"version": "2.0",
"name": "my.ui.lib.library-preload",
"modules": {
Expand All @@ -8,4 +8,4 @@
"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"
}
}
});
44 changes: 30 additions & 14 deletions test/preload_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,57 @@ describe('openui5_preload', function() {

it('default_options', function() {
fileContent.equal({
sActualFileSource: 'tmp/preload/library_default_options/my/ui/lib/library-preload.json',
sExpectedFileSource: 'test/preload/expected/library_default_options/my/ui/lib/library-preload.json',
sActualFileSource: 'tmp/preload/library_default_options/my/ui/lib/library-preload.js',
sExpectedFileSource: 'test/preload/expected/library_default_options/my/ui/lib/library-preload.js',
sMessage: 'library preload JS should be correctly created.'
});
});

it('compat_138', function() {
fileContent.equal({
sActualFileSource: 'tmp/preload/library_compat_138/my/ui/lib/library-preload.json',
sExpectedFileSource: 'test/preload/expected/library_compat_138/my/ui/lib/library-preload.json',
sMessage: 'library preload JSON should be correctly created.'
});
});

it('compat_140', function() {
fileContent.equal({
sActualFileSource: 'tmp/preload/library_compat_140/my/ui/lib/library-preload.js',
sExpectedFileSource: 'test/preload/expected/library_compat_140/my/ui/lib/library-preload.js',
sMessage: 'library preload JS should be correctly created.'
});
});

it('resource_prefix', function() {
fileContent.equal({
sActualFileSource: 'tmp/preload/library_resource_prefix/library-preload.json',
sExpectedFileSource: 'test/preload/expected/library_resource_prefix/library-preload.json',
sMessage: 'library preload JSON should be correctly created.'
sActualFileSource: 'tmp/preload/library_resource_prefix/library-preload.js',
sExpectedFileSource: 'test/preload/expected/library_resource_prefix/library-preload.js',
sMessage: 'library preload JS should be correctly created.'
});
});

it('no_compress', function() {
fileContent.equal({
sActualFileSource: 'tmp/preload/library_no_compress/my/ui/lib/library-preload.json',
sExpectedFileSource: 'test/preload/expected/library_no_compress/my/ui/lib/library-preload.json',
sMessage: 'library preload JSON should be correctly created.'
sActualFileSource: 'tmp/preload/library_no_compress/my/ui/lib/library-preload.js',
sExpectedFileSource: 'test/preload/expected/library_no_compress/my/ui/lib/library-preload.js',
sMessage: 'library preload JS should be correctly created.'
});
});

it('custom_uglify_params', function() {
fileContent.equal({
sActualFileSource: 'tmp/preload/library_custom_uglify_params/my/ui/lib/library-preload.json',
sExpectedFileSource: 'test/preload/expected/library_custom_uglify_params/my/ui/lib/library-preload.json',
sMessage: 'library preload JSON should be correctly created.'
sActualFileSource: 'tmp/preload/library_custom_uglify_params/my/ui/lib/library-preload.js',
sExpectedFileSource: 'test/preload/expected/library_custom_uglify_params/my/ui/lib/library-preload.js',
sMessage: 'library preload JS should be correctly created.'
});
})

it('library_same_dest', function() {
fileContent.equal({
sActualFileSource: 'test/preload/fixtures/library-same-dest/my/ui/lib/library-preload.json',
sExpectedFileSource: 'test/preload/expected/library_default_options/my/ui/lib/library-preload.json',
sMessage: 'library preload JSON should be correctly created.'
sActualFileSource: 'test/preload/fixtures/library-same-dest/my/ui/lib/library-preload.js',
sExpectedFileSource: 'test/preload/expected/library_default_options/my/ui/lib/library-preload.js',
sMessage: 'library preload JS should be correctly created.'
});
});

Expand Down

0 comments on commit f0764d3

Please sign in to comment.