-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
92 lines (75 loc) · 2.4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
var PluginError = require('plugin-error');
var log = require('fancy-log');
var colors = require('ansi-colors');
var through = require('through2');
var path = require('path');
module.exports = function (options) {
options = options || {};
options.isLibrary = !!options.isLibrary;
options.fileName = options.fileName || (options.isLibrary ? 'library-preload.json' : 'Component-preload.js');
if (typeof options.base !== 'string') {
throw new PluginError('gulp-ui5-preload', '`base` parameter required');
}
var firstFile;
var preloadModules = {};
function collectFileContentsFromStream(file, enc, done) {
// ignore empty files
if (file.isNull()) {
done();
return;
}
// we dont do streams (yet)
if (file.isStream()) {
this.emit('error', new PluginError('gulp-ui5-preload', 'File Content streams not yet supported'));
done();
return;
}
if (!firstFile && file) {
firstFile = file;
}
try {
var resolvedPath = (options.namespace ? options.namespace.split('.').join('/') + '/' : '' ) + path.relative(path.resolve(options.base), file.path).replace(/\\/g, '/');
preloadModules[resolvedPath] = file.contents.toString();
} catch (err) {
this.emit('error', new PluginError('gulp-ui5-preload', err));
done();
return;
}
done();
}
function pushCombinedFileToStream(done) {
if (!firstFile) {
done();
log.error('gulp-ui5-preload', colors.red('WARNING: No files were passed to gulp-ui5-preload. Wrong path?. Skipping emit of Component-preload.js...'));
return;
}
log.info('gulp-ui5-preload',
colors.magenta('number of files combined to preload file ' + options.fileName + ': ' + Object.keys(preloadModules).length)
);
var template = 'jQuery.sap.registerPreloadedModules(JSON_CONTENT);';
var suffix = '.Component-preload';
if (options.isLibrary) {
template = 'JSON_CONTENT';
suffix = '.library-preload';
}
var jsonContent = JSON.stringify(
{
name: options.namespace + suffix,
version: '2.0',
modules: preloadModules
},
null,
'\t'
);
var contents = template.replace('JSON_CONTENT', function () {
return jsonContent;
});
var preloadFile = firstFile.clone({contents: false});
preloadFile.contents = new Buffer(contents);
preloadFile.path = path.join(firstFile.base, options.fileName);
this.push(preloadFile);
done();
}
return through.obj(collectFileContentsFromStream, pushCombinedFileToStream);
};