-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
69 lines (53 loc) · 2.07 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
var through = require('through');
var os = require('os');
var path = require('path');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var File = gutil.File;
module.exports = function(fileName, lengthLimit, options){
var pluginName = 'gulp-concat-limit';
lengthLimit = lengthLimit || 25600;
options = options || {};
var buffer = [],
resultFileBuffers = [],
firstFile = null,
currentResultFileLength = 0,
fileSeparator = options.newLine && gutil.linefeed || '';
function endFile(){
resultFileBuffers.push(buffer);
buffer = [];
currentResultFileLength = 0;
}
function bufferContents(file){
if (file.isNull()) return; // ignore
if (file.isStream()) return this.emit('error', new PluginError(pluginName, 'Streaming not supported'));
if (!fileName) return this.emit('error', new PluginError(pluginName, 'fileName parameter is required. None given.'));
if (!firstFile) firstFile = file;
var fileContents = file.contents.toString('utf8'),
fileLength = fileContents.length;
if(!fileLength) return;
if(buffer.length && currentResultFileLength + fileLength > lengthLimit){
endFile();
}
buffer.push(fileContents);
currentResultFileLength += fileLength;
}
function endStream(){
if (buffer.length > 0){
resultFileBuffers.push(buffer);
}
if(resultFileBuffers.length === 0) this.emit('end');
resultFileBuffers.map(function(resultFileBuffer, index){
var extension = path.extname(fileName),
fileBasename = path.basename(fileName, extension);
this.push(new File({
cwd: firstFile.cwd,
base: firstFile.base,
path: path.join(firstFile.base, fileBasename + index + extension),
contents: new Buffer(resultFileBuffer.join(fileSeparator))
}));
}.bind(this));
this.emit('end');
}
return through(bufferContents, endStream);
};