-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
102 lines (75 loc) · 2.46 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
'use strict';
var through = require('through2'),
glob = require('glob'),
path = require('path'),
replaceExt = require('replace-ext'),
gutil = require('gulp-util'),
fs = require('fs'),
PluginError = gutil.PluginError;
var PLUGIN_NAME = 'gulp-include-source';
var placeholders = {
'js' : '<script src="%"></script>',
'css' : '<link rel="stylesheet" href="%">'
};
function matchExpressions(contents) {
return contents.match(/<!--\s+include:([a-z]+)\(([^)]+)\)\s+-->/);
}
function replaceExtension(filename, type, options) {
if( options.scriptExt && type === 'js' ) {
filename = replaceExt(filename, '.' + options.scriptExt);
} else if( options.styleExt && type === 'css' ) {
filename = replaceExt(filename, '.' + options.styleExt);
}
return filename;
}
function parseFiles(source, cwd) {
if( source.indexOf('list:') === 0 ) {
var cleanSrc = source.replace('list:', '');
return fs.readFileSync( cleanSrc ).toString().split('\n');
}
return glob.sync( source, { cwd : cwd } );
}
function injectFiles(file, options) {
var contents = file.contents.toString();
var cwd = options.cwd || path.dirname(file.path);
var matches = matchExpressions(contents);
while( matches ) {
var type = matches[1];
var placeholder = placeholders[ type ];
var files = parseFiles(matches[2], cwd);
var includes = '';
if( placeholder && files && files.length > 0 ) {
includes = files.map(function(filename) {
filename = replaceExtension(filename, type, options);
return placeholder.split('%').join(filename);
}).join('\n');
}
contents = contents.substring(0, matches.index) + includes + contents.substring(matches.index + matches[0].length);
matches = matchExpressions(contents);
}
return contents;
}
function gulpIncludeSource(options) {
options = options || {};
var stream = through.obj(function(file, enc, callback) {
if (file.isNull()) {
this.push(file); // Do nothing if no contents
return callback();
}
if (file.isStream()) {
this.emit('error', new PluginError(PLUGIN_NAME, 'Streaming not supported!'));
return callback();
}
if (file.isBuffer()) {
try {
file.contents = new Buffer( injectFiles( file, options ) );
} catch (err) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, err));
}
}
this.push(file);
return callback();
});
return stream;
}
module.exports = gulpIncludeSource;