Skip to content

Commit 9ba30ff

Browse files
author
Christian Theilemann
committed
simplify code and add support for arbitrary file formats
1 parent aec6ed3 commit 9ba30ff

File tree

4 files changed

+30
-45
lines changed

4 files changed

+30
-45
lines changed

index.js

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,64 +12,55 @@ module.exports = function (options) {
1212
throw new gutil.PluginError('gulp-ui5-preload', '`base` parameter required');
1313
}
1414

15-
var transformedContentsArr = [];
1615
var firstFile;
16+
var preloadModules = {};
1717

18-
function transformSingleFile(file, options) {
19-
20-
var ui5Path = (options.namespace ? options.namespace.split('.').join('/') + '/' : '' ) + path.relative(path.resolve(options.base), file.path);
21-
var contents = file.contents.toString();
22-
23-
if (/\.js$/.test(ui5Path)) {
24-
// Javascript file
25-
contents = 'function() {' + contents + '}';
26-
} else if (/\.xml$/.test(ui5Path)) {
27-
// XML file
28-
contents = contents.replace(/"/g, '\\"')
29-
.replace(/'/g, '\\\'')
30-
.replace(/(\r\n|\n|\r)/gm, '')
31-
.replace(/\t+/gm, ' ');
32-
contents = '\'' + contents + '\'';
33-
}
34-
contents = '\'' + ui5Path + '\': ' + contents;
35-
return contents;
36-
}
37-
38-
function collectAndTransformFileContentsFromStream(file, enc, done) {
18+
function collectFileContentsFromStream(file, enc, done) {
3919
// ignore empty files
40-
if (file.isNull()) {return;}
20+
if (file.isNull()) {
21+
return;
22+
}
4123
// we dont do streams (yet)
4224
if (file.isStream()) {
43-
return this.emit('error', new PluginError('gulp-ui5-preload', 'Streaming not supported'));
25+
return this.emit('error', new PluginError('gulp-ui5-preload', 'File Content streams not yet supported'));
26+
}
27+
if (!firstFile && file) {
28+
firstFile = file;
4429
}
45-
if (!firstFile && file) {firstFile = file;}
4630

4731
try {
48-
transformedContentsArr.push(transformSingleFile(file, options));
32+
33+
var resolvedPath = (options.namespace ? options.namespace.split('.').join('/') + '/' : '' ) + path.relative(path.resolve(options.base), file.path);
34+
preloadModules[resolvedPath] = file.contents.toString();
35+
4936
} catch (err) {
5037
this.emit('error', new gutil.PluginError('gulp-ui5-preload', err));
5138
}
5239
done();
5340
}
5441

55-
function pushCombinedFilesToStream() {
56-
57-
gutil.log('gulp-ui5-preload', gutil.colors.magenta('number of files combined to preload file ' + options.fileName + ': ', transformedContentsArr.length));
42+
function pushCombinedFileToStream() {
5843

59-
if (transformedContentsArr.length === 0) {
60-
return this.emit('end');
61-
}
44+
gutil.log('gulp-ui5-preload',
45+
gutil.colors.magenta(
46+
'number of files combined to preload file ' + options.fileName + ': ',
47+
Object.keys(preloadModules).length)
48+
);
6249

63-
var contents = 'jQuery.sap.registerPreloadedModules({"name" : "' + options.namespace + '.Component-preload' + '",\n"version" : "2.0",' +
64-
'"modules" : {' + transformedContentsArr.join(',\n') + '}});';
50+
var contents = 'jQuery.sap.registerPreloadedModules(' + JSON.stringify(
51+
{
52+
name: options.namespace + '.Component-preload',
53+
version: '2.0',
54+
modules: preloadModules
55+
}, null, '\t') + ');';
6556

6657
var preloadFile = firstFile.clone({contents: false});
67-
preloadFile.path = path.join(firstFile.base, options.fileName);
6858
preloadFile.contents = new Buffer(contents);
59+
preloadFile.path = path.join(firstFile.base, options.fileName);
6960

7061
this.push(preloadFile);
7162
this.emit('end');
7263
}
7364

74-
return through.obj(collectAndTransformFileContentsFromStream, pushCombinedFilesToStream);
65+
return through.obj(collectFileContentsFromStream, pushCombinedFileToStream);
7566
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "gulp-ui5-preload",
33
"version": "1.0.2",
44
"description": "Creates a Component-preload.js file for openui5 / sapui5 projects.",
5-
"license": "MIT",
5+
"license": "Apache 2.0",
66
"repository": "geekflyer/gulp-ui5-preload",
77
"author": {
88
"name": "Christian Theilemann",

readme.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# gulp-ui5-preload [![Build Status](https://travis-ci.org/geekflyer/gulp-ui5-preload.svg?branch=master)](https://travis-ci.org/geekflyer/gulp-ui5-preload) [![npm version](https://badge.fury.io/js/gulp-ui5-preload.svg)](http://badge.fury.io/js/gulp-ui5-preload)
22

3-
Creates a Component-preload.js file for openui5 / sapui5 projects. Supports .js and .xml artifacts.
3+
Creates a Component-preload.js file for openui5 / sapui5 projects. Creating a prelaod file can speed up the initial load time of your webapp, by reducing the number of HTTP requests / roundtrips to load your code. The preload file can combine your `js` artifacts, `xml`,`js`, `html` and `json` views as well as `.properties` files into a single `Component-preload.js` file.
44

55
## Install
66

@@ -75,12 +75,6 @@ The namespace at the `base` path. All source files are treated as sub-namespaces
7575

7676
File name of the combined file to emit.
7777

78-
79-
## Limitations
80-
81-
`.json` and `.html` views are not yet supported. Please open an issue or make a pull request if you need them. Implementation should be fairly easy - just a few LoC in the function `transformSingleFile`.
82-
83-
8478
## License
8579

8680
Apache 2.0 © [Christian Theilemann](https://github.com/geekflyer)

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var lab = exports.lab = require('lab').script();
66

77
lab.test('creates a preload file full of unicorns and zebras :-)', function (done) {
88
var stream = ui5Preload({base: 'src/conf/ui', namespace: 'sap.pdms.fdn'});
9-
var expectedFile = 'jQuery.sap.registerPreloadedModules({"name" : "sap.pdms.fdn.Component-preload",\n"version" : "2.0","modules" : {\'sap/pdms/fdn/app/unicorns.js\': function() {unicorns},\n\'sap/pdms/fdn/app/zebras.xml\': \'zebras\'}});';
9+
var expectedFile = 'jQuery.sap.registerPreloadedModules({\n\t"name": "sap.pdms.fdn.Component-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});';
1010

1111
stream.write(new gutil.File({
1212
base: __dirname,

0 commit comments

Comments
 (0)