From a9ebf407bd4700136b0d7277cdac53d2c6366bb7 Mon Sep 17 00:00:00 2001 From: Matthias Osswald Date: Tue, 2 Feb 2016 13:14:39 +0100 Subject: [PATCH] Always exclude corresponding preload files This change will always add an exclude to prevent adding a previously created preload file into the same preload. As there should be no use case to do that this is enabled by default without an option to disabled it. Other preload files (e.g. from sub-components) should be possible to include. Fixes https://github.com/SAP/grunt-openui5/issues/21 --- Gruntfile.js | 76 ++++++++++++++++++- tasks/preload.js | 10 ++- .../app-same-dest/my/app/Component-preload.js | 9 +++ .../app-same-dest/my/app/Component.js | 22 ++++++ .../app-same-dest/my/app/view/my.view.xml | 2 + .../my/app/view/myHtmlPre.view.xml | 3 + .../my/ui/lib/foo.properties | 1 + .../my/ui/lib/library-preload.json | 11 +++ .../library-same-dest/my/ui/lib/library.js | 8 ++ .../library-same-dest/my/ui/lib/my.view.xml | 2 + .../my/ui/lib/myHtmlPre.view.xml | 3 + .../library-same-dest/my/ui/lib/myJS.js | 22 ++++++ .../library-same-dest/my/ui/lib/myJSON.json | 3 + test/preload_test.js | 16 ++++ 14 files changed, 186 insertions(+), 2 deletions(-) create mode 100644 test/preload/fixtures/app-same-dest/my/app/Component-preload.js create mode 100644 test/preload/fixtures/app-same-dest/my/app/Component.js create mode 100644 test/preload/fixtures/app-same-dest/my/app/view/my.view.xml create mode 100644 test/preload/fixtures/app-same-dest/my/app/view/myHtmlPre.view.xml create mode 100644 test/preload/fixtures/library-same-dest/my/ui/lib/foo.properties create mode 100644 test/preload/fixtures/library-same-dest/my/ui/lib/library-preload.json create mode 100644 test/preload/fixtures/library-same-dest/my/ui/lib/library.js create mode 100644 test/preload/fixtures/library-same-dest/my/ui/lib/my.view.xml create mode 100644 test/preload/fixtures/library-same-dest/my/ui/lib/myHtmlPre.view.xml create mode 100644 test/preload/fixtures/library-same-dest/my/ui/lib/myJS.js create mode 100644 test/preload/fixtures/library-same-dest/my/ui/lib/myJSON.json diff --git a/Gruntfile.js b/Gruntfile.js index 8b006f3c..9711b557 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -29,7 +29,11 @@ module.exports = function(grunt) { // Before generating any new files, remove any previously-created files. clean: { - tests: ['tmp'] + tests: [ + 'tmp', + 'test/preload/fixtures/app-same-dest/my/app/Component-preload.js', + 'test/preload/fixtures/library-same-dest/my/ui/lib/library-preload.json' + ] }, // Configuration to be run (and then tested). @@ -111,6 +115,24 @@ module.exports = function(grunt) { components: '**' }, + // The following two targets are testing that a generated preload file + // doesn't get included when runnign the preload again + // (same folder for src and dest) + 'component_same_dest_1': { + options: { + resources: 'test/preload/fixtures/app-same-dest', + dest: 'test/preload/fixtures/app-same-dest' + }, + components: '**' + }, + 'component_same_dest_2': { + options: { + resources: 'test/preload/fixtures/app-same-dest', + dest: 'test/preload/fixtures/app-same-dest' + }, + components: '**' + }, + 'library_default_options': { options: { resources: 'test/preload/fixtures/library', @@ -139,6 +161,58 @@ module.exports = function(grunt) { compress: false }, libraries: '**' + }, + + // The following two targets are testing that a generated preload file + // doesn't get included when runnign the preload again + // (same folder for src and dest) + 'library_same_dest_1': { + options: { + resources: [ + { + cwd: 'test/preload/fixtures/library-same-dest', + src: [ + // Defaults + '**/*.js', + '**/*.fragment.html', + '**/*.fragment.json', + '**/*.fragment.xml', + '**/*.view.html', + '**/*.view.json', + '**/*.view.xml', + '**/*.properties', + // Include "library-preload.json" for this test + '**/library-preload.json' + ] + } + ], + dest: 'test/preload/fixtures/library-same-dest' + }, + libraries: '**' + }, + 'library_same_dest_2': { + options: { + resources: [ + { + cwd: 'test/preload/fixtures/library-same-dest', + src: [ + // Defaults + '**/*.js', + '**/*.fragment.html', + '**/*.fragment.json', + '**/*.fragment.xml', + '**/*.view.html', + '**/*.view.json', + '**/*.view.xml', + '**/*.properties', + // Include "library-preload.json" for this test + '**/library-preload.json' + ] + } + ], + dest: 'test/preload/fixtures/library-same-dest' + }, + libraries: '**' } }, diff --git a/tasks/preload.js b/tasks/preload.js index d23d0226..1cf30cd3 100644 --- a/tasks/preload.js +++ b/tasks/preload.js @@ -176,7 +176,15 @@ module.exports = function (grunt) { preloadObject.name = preloadModuleName; } - var preloadPatterns = preloadOption.src ? preloadOption.src : (preloadDir + '/**'); + var preloadPatterns = preloadOption.src ? preloadOption.src : [ preloadDir + '/**' ]; + if (typeof preloadPatterns === "string") { + preloadPatterns = [ preloadPatterns ]; + } + + // Always exclude the corresponding preload file (Component-preload.js / library-preload.json) + // Otherwise it might get included every time the build runs if src / dest are the same dir + preloadPatterns.push('!' + preloadDir + '/' + preloadInfo.moduleName + preloadInfo.ext); + var preloadFiles = grunt.file.match(preloadPatterns, resourceFiles); if (preloadFiles.length === 0) { var patternsString = (typeof preloadPatterns === 'string') ? preloadPatterns : preloadPatterns.join('", "'); diff --git a/test/preload/fixtures/app-same-dest/my/app/Component-preload.js b/test/preload/fixtures/app-same-dest/my/app/Component-preload.js new file mode 100644 index 00000000..8cfddd3c --- /dev/null +++ b/test/preload/fixtures/app-same-dest/my/app/Component-preload.js @@ -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": "XML\n", + "my/app/view/myHtmlPre.view.xml": "XML\n\n " + } +}); \ No newline at end of file diff --git a/test/preload/fixtures/app-same-dest/my/app/Component.js b/test/preload/fixtures/app-same-dest/my/app/Component.js new file mode 100644 index 00000000..8084737b --- /dev/null +++ b/test/preload/fixtures/app-same-dest/my/app/Component.js @@ -0,0 +1,22 @@ +/* © */ +'use strict'; + +/** +* This is a copyright comment +*/ + +/* (c) */ + +/* released under */ +/* normal comment */ +/* license */ + + +console.log('myJS'); + +/** + * This is a little comment + */ +function myFunction(longVariableName, longerVariableName) { + return longVariableName + longerVariableName; +} diff --git a/test/preload/fixtures/app-same-dest/my/app/view/my.view.xml b/test/preload/fixtures/app-same-dest/my/app/view/my.view.xml new file mode 100644 index 00000000..ac389a3b --- /dev/null +++ b/test/preload/fixtures/app-same-dest/my/app/view/my.view.xml @@ -0,0 +1,2 @@ +XML + \ No newline at end of file diff --git a/test/preload/fixtures/app-same-dest/my/app/view/myHtmlPre.view.xml b/test/preload/fixtures/app-same-dest/my/app/view/myHtmlPre.view.xml new file mode 100644 index 00000000..baa83f00 --- /dev/null +++ b/test/preload/fixtures/app-same-dest/my/app/view/myHtmlPre.view.xml @@ -0,0 +1,3 @@ +XML + + \ No newline at end of file diff --git a/test/preload/fixtures/library-same-dest/my/ui/lib/foo.properties b/test/preload/fixtures/library-same-dest/my/ui/lib/foo.properties new file mode 100644 index 00000000..6ac867af --- /dev/null +++ b/test/preload/fixtures/library-same-dest/my/ui/lib/foo.properties @@ -0,0 +1 @@ +FOO=BAR diff --git a/test/preload/fixtures/library-same-dest/my/ui/lib/library-preload.json b/test/preload/fixtures/library-same-dest/my/ui/lib/library-preload.json new file mode 100644 index 00000000..abd7e7a2 --- /dev/null +++ b/test/preload/fixtures/library-same-dest/my/ui/lib/library-preload.json @@ -0,0 +1,11 @@ +{ + "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": "XML\n", + "my/ui/lib/myHtmlPre.view.xml": "XML\n\n ", + "my/ui/lib/foo.properties": "FOO=BAR\n" + } +} \ No newline at end of file diff --git a/test/preload/fixtures/library-same-dest/my/ui/lib/library.js b/test/preload/fixtures/library-same-dest/my/ui/lib/library.js new file mode 100644 index 00000000..ddc3b34c --- /dev/null +++ b/test/preload/fixtures/library-same-dest/my/ui/lib/library.js @@ -0,0 +1,8 @@ +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"] +}); diff --git a/test/preload/fixtures/library-same-dest/my/ui/lib/my.view.xml b/test/preload/fixtures/library-same-dest/my/ui/lib/my.view.xml new file mode 100644 index 00000000..ac389a3b --- /dev/null +++ b/test/preload/fixtures/library-same-dest/my/ui/lib/my.view.xml @@ -0,0 +1,2 @@ +XML + \ No newline at end of file diff --git a/test/preload/fixtures/library-same-dest/my/ui/lib/myHtmlPre.view.xml b/test/preload/fixtures/library-same-dest/my/ui/lib/myHtmlPre.view.xml new file mode 100644 index 00000000..baa83f00 --- /dev/null +++ b/test/preload/fixtures/library-same-dest/my/ui/lib/myHtmlPre.view.xml @@ -0,0 +1,3 @@ +XML + + \ No newline at end of file diff --git a/test/preload/fixtures/library-same-dest/my/ui/lib/myJS.js b/test/preload/fixtures/library-same-dest/my/ui/lib/myJS.js new file mode 100644 index 00000000..8084737b --- /dev/null +++ b/test/preload/fixtures/library-same-dest/my/ui/lib/myJS.js @@ -0,0 +1,22 @@ +/* © */ +'use strict'; + +/** +* This is a copyright comment +*/ + +/* (c) */ + +/* released under */ +/* normal comment */ +/* license */ + + +console.log('myJS'); + +/** + * This is a little comment + */ +function myFunction(longVariableName, longerVariableName) { + return longVariableName + longerVariableName; +} diff --git a/test/preload/fixtures/library-same-dest/my/ui/lib/myJSON.json b/test/preload/fixtures/library-same-dest/my/ui/lib/myJSON.json new file mode 100644 index 00000000..bffd0ff5 --- /dev/null +++ b/test/preload/fixtures/library-same-dest/my/ui/lib/myJSON.json @@ -0,0 +1,3 @@ +{ + "my": "json" +} diff --git a/test/preload_test.js b/test/preload_test.js index 321e4fec..4f541ad1 100644 --- a/test/preload_test.js +++ b/test/preload_test.js @@ -45,6 +45,14 @@ describe('openui5_preload', function() { }); }); + 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.' + }); + }); + }); describe('component', function() { @@ -73,6 +81,14 @@ describe('openui5_preload', function() { }); }); + it('component_same_dest', function() { + fileContent.equal({ + sActualFileSource: 'test/preload/fixtures/app-same-dest/my/app/Component-preload.js', + sExpectedFileSource: 'test/preload/expected/component_default_options/my/app/Component-preload.js', + sMessage: 'component preload JS should be correctly created.' + }); + }); + }); });