diff --git a/index.js b/index.js index d5991b6..e9eb01e 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,7 @@ module.exports = function (options) { options = options || {}; options.isLibrary = !!options.isLibrary; - options.fileName = options.fileName || (options.isLibrary ? 'library-preload.json' : 'Component-preload.js'); + options.fileName = options.fileName || ((options.isLibrary ? 'library-preload.' : 'Component-preload.') + (options.format || 'js')); if (typeof options.base !== 'string') { throw new PluginError('gulp-ui5-preload', '`base` parameter required'); @@ -64,9 +64,11 @@ module.exports = function (options) { var template = 'jQuery.sap.registerPreloadedModules(JSON_CONTENT);'; var suffix = '.Component-preload'; if (options.isLibrary) { - template = 'JSON_CONTENT'; suffix = '.library-preload'; } + if (options.fileName.slice(-4) === 'json') { + template = 'JSON_CONTENT'; + } var jsonContent = JSON.stringify( { diff --git a/readme.md b/readme.md index 436eacb..b5f294b 100644 --- a/readme.md +++ b/readme.md @@ -71,7 +71,7 @@ The namespace at the `base` path. All source files are treated as sub-namespaces ##### fileName * Type: `string` -* Default: 'Component-preload.js' or 'library-preload.json' (depends on `isLibrary` option) +* Default: 'Component-preload.js' or 'library-preload.js' (depends on `isLibrary` option) File name of the combined file to emit. @@ -79,7 +79,13 @@ File name of the combined file to emit. * Type: `boolean` * Default: false -If set to `true` a `library-preload.json` file is emitted instead of a `Component-preload.js` file (default). The emitted file contents between those options vary a little bit. +If set to `true` a `library-preload.js` file is emitted instead of a `Component-preload.js` file (default). The emitted file contents between those options vary a little bit. + +##### format +* Type: `string` +* Default: 'js' + +The output format. Can be set to 'json' in order to emit a `library-preload.json` file (which is required for UI5 versions < 1.40). ## License diff --git a/test.js b/test.js index c314776..b504713 100644 --- a/test.js +++ b/test.js @@ -30,8 +30,33 @@ lab.test('creates a preload file full of unicorns and zebras :-)', function (don stream.end(); }); -lab.test('creates a library-preload file full of unicorns and zebras :-)', function (done) { +lab.test('creates a library-preload.js file full of unicorns and zebras :-)', function (done) { var stream = ui5Preload({base: 'src/conf/ui', namespace: 'sap.pdms.fdn', isLibrary: true}); + var expectedFile = 'jQuery.sap.registerPreloadedModules({\n\t"name": "sap.pdms.fdn.library-preload",\n\t"version": "2.0",\n\t"modules": {\n\t\t"sap/pdms/fdn/app/unicorns.js": "unicorns",\n\t\t\"sap/pdms/fdn/app/zebras.xml": "zebras"\n\t}\n});'; + + stream.write(new Vinyl({ + base: __dirname, + path: __dirname + '/src/conf/ui/app/unicorns.js', + contents: new Buffer('unicorns') + })); + + stream.write(new Vinyl({ + base: __dirname, + path: __dirname + '/src/conf/ui/app/zebras.xml', + contents: new Buffer('zebras') + })); + + stream.on('data', function (file) { + expect(file.contents.toString()).to.equal(expectedFile); + expect(file.path.split(path.sep).pop()).to.equal('library-preload.js'); + }); + + stream.on('end', done); + stream.end(); +}); + +lab.test('creates a library-preload.json file full of unicorns and zebras :-)', function (done) { + var stream = ui5Preload({base: 'src/conf/ui', namespace: 'sap.pdms.fdn', isLibrary: true, format: 'json'}); var expectedFile = '{\n\t"name": "sap.pdms.fdn.library-preload",\n\t"version": "2.0",\n\t"modules": {\n\t\t"sap/pdms/fdn/app/unicorns.js": "unicorns",\n\t\t\"sap/pdms/fdn/app/zebras.xml": "zebras"\n\t}\n}'; stream.write(new Vinyl({ @@ -54,3 +79,53 @@ lab.test('creates a library-preload file full of unicorns and zebras :-)', funct stream.on('end', done); stream.end(); }); + +lab.test('creates a foo.js file full of unicorns and zebras :-)', function (done) { + var stream = ui5Preload({base: 'src/conf/ui', namespace: 'sap.pdms.fdn', isLibrary: true, fileName: 'foo.js'}); + var expectedFile = 'jQuery.sap.registerPreloadedModules({\n\t"name": "sap.pdms.fdn.library-preload",\n\t"version": "2.0",\n\t"modules": {\n\t\t"sap/pdms/fdn/app/unicorns.js": "unicorns",\n\t\t\"sap/pdms/fdn/app/zebras.xml": "zebras"\n\t}\n});'; + + stream.write(new Vinyl({ + base: __dirname, + path: __dirname + '/src/conf/ui/app/unicorns.js', + contents: new Buffer('unicorns') + })); + + stream.write(new Vinyl({ + base: __dirname, + path: __dirname + '/src/conf/ui/app/zebras.xml', + contents: new Buffer('zebras') + })); + + stream.on('data', function (file) { + expect(file.contents.toString()).to.equal(expectedFile); + expect(file.path.split(path.sep).pop()).to.equal('foo.js'); + }); + + stream.on('end', done); + stream.end(); +}); + +lab.test('creates a foo.json file full of unicorns and zebras :-)', function (done) { + var stream = ui5Preload({base: 'src/conf/ui', namespace: 'sap.pdms.fdn', isLibrary: true, fileName: 'foo.json'}); + var expectedFile = '{\n\t"name": "sap.pdms.fdn.library-preload",\n\t"version": "2.0",\n\t"modules": {\n\t\t"sap/pdms/fdn/app/unicorns.js": "unicorns",\n\t\t\"sap/pdms/fdn/app/zebras.xml": "zebras"\n\t}\n}'; + + stream.write(new Vinyl({ + base: __dirname, + path: __dirname + '/src/conf/ui/app/unicorns.js', + contents: new Buffer('unicorns') + })); + + stream.write(new Vinyl({ + base: __dirname, + path: __dirname + '/src/conf/ui/app/zebras.xml', + contents: new Buffer('zebras') + })); + + stream.on('data', function (file) { + expect(file.contents.toString()).to.equal(expectedFile); + expect(file.path.split(path.sep).pop()).to.equal('foo.json'); + }); + + stream.on('end', done); + stream.end(); +});