forked from lpommers/gulp-bundle-file
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (124 loc) · 3.77 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
145
'use strict';
var path = require('path')
var through2 = require('through2');
var concat = require('gulp-concat');
var gutil = require('gulp-util');
var fs = require('vinyl-fs');
var map = require('map-stream');
var sourcemaps = require('gulp-sourcemaps');
var pluginName = 'gulp-bundle-file';
// pushes files from pipe to array
function pushTo(array) {
return map(function (file, cb) {
array.push(file);
cb(null, file);
});
}
// validates file
function checkFile(file, cb) {
if (file.isNull()) {
cb(null, file);
return false;
}
if (file.isStream()) {
cb(new gutil.PluginError(pluginName, 'Streaming not supported'));
return false;
}
return true;
}
// creates new pipe for files from bundle
function processBundleFile(file, bundleExt, variables, bundleHandler) {
// get bundle files
var lines = file.contents.toString().trim().split('\n');
var resultFilePaths = [];
lines.forEach(function(line) {
resultFilePaths.push(getFilePathFromLine(file, line, variables));
});
// find files and send to buffer
var bundleSrc = fs.src(resultFilePaths)
.pipe(recursiveBundle(bundleExt, variables));
if (bundleHandler && typeof bundleHandler === 'function')
bundleSrc = bundleHandler(bundleSrc);
return bundleSrc;
}
// parses file path from line in bundle file
function getFilePathFromLine(bundleFile, line, variables) {
// get paths
var relative = path.relative(process.cwd(), bundleFile.path);
var dir = path.dirname(relative);
// handle variables
var varRegex = /@{([^}]+)}/;
var match;
while (match = line.match(varRegex)) {
var varName = match[1];
if (!variables || typeof(variables[varName]) === 'undefined')
throw new gutil.PluginError(pluginName, relative + ': variable "' + varName + '" is not specified');
var varValue = variables[varName];
line = line.substr(0, match.index) + varValue + line.substr(match.index + match[0].length);
}
if (line === '')
return null;
// handle `!` negated file paths
var negative = line[0] === '!';
if (negative)
line = line.substr(1);
// get file path
var filePath;
if (line.indexOf('./') === 0)
filePath = line.substr(2);
else
filePath = path.join(dir, line);
// return path
if (negative)
return '!' + filePath;
return filePath;
}
// recursively processes files and unwraps bundle files
function recursiveBundle(bundleExt, variables) {
return through2.obj(function(file, enc, cb) {
if (!checkFile(file, cb))
return;
// standart file push to callback
if (path.extname(file.path).toLowerCase() != bundleExt)
return cb(null, file);
// bundle file should be parsed
processBundleFile(file, bundleExt, variables)
.pipe(pushTo(this))
.on('end', cb);
});
}
module.exports = {
// addes files from bundle to current pipe
list: function (variables) {
return through2.obj(function(file, enc, cb) {
if (!checkFile(file, cb))
return;
var ext = path.extname(file.path).toLowerCase();
processBundleFile(file, ext, variables)
.pipe(pushTo(this))
.on('end', cb);
});
},
// concatenates files from bundle and replaces bundle file in current pipe
// first parameter is function that handles source stream for each bundle
concat: function (variables, bundleHandler) {
// handle if bundleHandler specified in first argument
if (!bundleHandler && typeof(variables) === 'function') {
bundleHandler = variables;
variables = null;
}
return through2.obj(function(file, enc, cb) {
if (!checkFile(file, cb))
return;
var ext = path.extname(file.path).toLowerCase();
var resultFileName = path.basename(file.path, ext);
var bundleFiles = processBundleFile(file, ext, variables, bundleHandler);
if (file.sourceMap)
bundleFiles = bundleFiles.pipe(sourcemaps.init());
bundleFiles
.pipe(concat(resultFileName))
.pipe(pushTo(this))
.on('end', cb);
});
}
}