-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
144 lines (111 loc) · 3.15 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
'use strict';
var util = require('gulp-util'),
path = require('path'),
through = require('through2'),
rejs = require('rejs'),
PluginError = util.PluginError,
File = util.File;
module.exports = function (filePath, options) {
var firstFile = null,
fileName = filePath,
blobs = {},
errors = [],
resolver = null;
var mVerbosity = 0;
if (!filePath) {
throw new PluginError('gulp-rejs', 'Missing file option');
}
options = options || {};
// to preserve existing |undefined| behaviour and to introduce |newLine: ""| for binaries
if (typeof options.newLine !== 'string') {
options.newLine = util.linefeed;
}
function readSource(pKey) {
return blobs[pKey];
}
if (typeof options.readSource !== 'function') {
options.readSource = readSource;
}
if (typeof options.verbosity !== 'number') {
options.verbosity = mVerbosity;
} else {
mVerbosity = options.verbosity;
}
function log(pVerbosity) {
if (mVerbosity >= pVerbosity) {
util.log.apply(util, Array.prototype.slice.call(arguments, 1));
}
}
if (typeof options.log !== 'function') {
options.log = log;
}
if (typeof options.globalClosureReferences !== 'object') {
options.globalClosureReferences = ['window'];
}
if (typeof filePath !== 'string') {
if (typeof filePath.path !== 'string') {
throw new PluginError('gulp-rejs', 'Missing path in file options');
}
fileName = path.basename(filePath.path);
firstFile = new File(filePath);
}
resolver = new rejs.Resolver(options);
function bufferContents(file, enc, cb) {
if (file.isNull()) {
return; // ignore
}
if (file.isStream()) {
cb(new PluginError('gulp-rejs', 'Only Buffers are supported'));
return;
}
if (!firstFile) {
firstFile = file;
}
blobs[file.path] = file.contents.toString();
try {
resolver.add(file.path);
} catch (e) {
errors.push({
key: file.path,
error: e
});
}
cb();
}
function endStream(cb) {
if (errors.length) {
var errorMessages = [];
errors.forEach(function(pError) {
errorMessages.push('\n' + pError.key + ':');
errorMessages.push(pError.error);
if (mVerbosity > 1 && pError.error.stack) {
errorMessages.push(pError.error.stack);
}
});
cb(new PluginError('gulp-rejs', errorMessages.join('\n')));
return;
}
var buffer = new Buffer(0),
sortedFiles = resolver.resolve(),
joinedFile;
if (firstFile) {
resolver.log(2, sortedFiles.join('\n'));
for (var i = 0, il = sortedFiles.length; i < il; i++) {
buffer = Buffer.concat([
buffer,
i > 0 ? new Buffer(options.newLine) : buffer,
new Buffer(blobs[sortedFiles[i]])
]);
}
joinedFile = firstFile;
if (typeof filePath === 'string') {
joinedFile = firstFile.clone({contents: false});
joinedFile.path = path.join(firstFile.base, filePath);
}
joinedFile.contents = buffer;
this.push(joinedFile);
}
cb();
}
return through.obj(bufferContents, endStream);
};