-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
73 lines (57 loc) · 1.84 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
var Buffer = require('buffer').Buffer;
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
var File = gutil.File;
var path = require('path');
var through = require('through');
var URL_SEPARATOR = '/';
module.exports = function(fileName, opt, remoteFiles) {
remoteFiles = remoteFiles || [];
if (!fileName)
throw new PluginError('gulp-pseudoconcat-js', 'Missing fileName option for gulp-pseudoconcat-js');
if (!opt)
opt = {};
if (typeof opt.webRoot !== 'string') {
opt.webRoot = process.cwd();
} else {
opt.webRoot = path.resolve(process.cwd(), opt.webRoot);
}
var buffer = [];
var firstFile = null;
function bufferContents(file) {
if (file.isNull()) {
return;
}
if (file.isStream()) {
return this.emit('error', new PluginError('gulp-pseudoconcat-js', 'Streaming not supported'));
}
if (!firstFile)
firstFile = file;
buffer.push(file);
}
function endStream() {
if (buffer.length === 0)
return this.emit('end');
var remoteScripts = remoteFiles.map(function(filePath) {
return '<script src="' + filePath + '"></script>';
});
var scripts = buffer.map(function(file) {
var scriptPath = path.relative(opt.webRoot, file.path);
if (path.sep !== URL_SEPARATOR) {
scriptPath = scriptPath.split(path.sep).join(URL_SEPARATOR);
}
return '<script src="' + scriptPath + '"></script>';
});
var joinedContents = new Buffer('document.write(\'' + remoteScripts.join('') + scripts.join('') + '\');', 'utf-8');
var joinedPath = path.join(firstFile.base, fileName);
var joinedFile = new File({
cwd: firstFile.cwd,
base: firstFile.base,
path: joinedPath,
contents: joinedContents
});
this.emit('data', joinedFile);
this.emit('end');
}
return through(bufferContents, endStream);
};