This repository has been archived by the owner on Dec 3, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
include.js
186 lines (147 loc) · 4.79 KB
/
include.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
var fs = require('fs');
var gutil = require('gulp-util');
var path = require('path');
var through = require('through2');
var trim = require('./lib/trim');
var pluginName = 'include-js';
var magenta = gutil.colors.magenta;
var cyan = gutil.colors.cyan;
var caches = {};
function error(context, includedId, err) {
err.message = 'Error in ' + cyan(context.filepath) + ': ' +
(includedId? context.options.keyword + '("' + cyan(includedId) + '"): ': '') + err.message;
err.filename = context.filepath;
throw err;
}
function exec(s, id, stack) {
var result = '';
var r = new RegExp('(//[^\r\n]*)?([^\\s]+[ \\t\\v]*)?' + this.options.keyword + '\\s*\\( *[\'"]([^\'"]*)[\'"]\\s*\\)');
var m = r.exec(s);
while (m) {
var isCmt = m[1];
var inline = m[2] || '';
var childId = m[3];
// Resolve relative path
if (childId[0] === '.') childId = path.join(path.join(path.dirname(id), childId));
result += s.slice(0, m.index);
if (isCmt) {
result += m[0];
} else try {
var sinc = read.call(this, childId, stack);
if (inline) sinc = trim(sinc);
result += inline + sinc;
} catch(e) {
error(this, childId, e);
return;
}
s = s.slice(m.index + m[0].length);
m = r.exec(s);
}
return result + s;
}
function read(id, stack) {
var filepath;
if (this.options.exactName) {
filepath = path.join(this.base, id);
} else {
var basename = path.basename(id);
basename = basename[0] === '_'? basename : '_' + basename;
basename = path.extname(basename) === this.options.ext? basename : basename + '.' + this.options.ext;
filepath = path.join(this.base, path.dirname(id), basename);
}
var newStack = stack.concat([filepath]);
if (stack.indexOf(filepath) >= 0) throw new Error('Circular ' +
newStack.map(function(i){ return magenta(i); }).join(', '));
this.includes[filepath] = this.includes[filepath] || time(filepath);
var s = readIncFile.call(this, filepath);
if (this.options.recursive) s = exec.call(this, s, id, newStack);
if (this.options.transform) s = this.options.transform(s);
return s;
}
function readIncFile(filepath) {
var isCache = this.options.cache;
if (isCache) {
var cache = this.cacheIncludes[filepath];
if (cache && time(filepath) === cache.time) return cache.content;
}
var s = fs.readFileSync(filepath, {encoding: 'utf8'});
if (isCache) this.cacheIncludes[filepath] = {
time: time(filepath),
content: s
};
return s;
}
function time(filepath) {
try {
var stat = fs.statSync(filepath);
return stat? stat.mtime.getTime() : null;
} catch(e) {
return null;
}
}
function isDirty(filepath, cacheModules, cacheIncludes) {
var cache = cacheModules[filepath];
if (!cache || cache.time !== time(filepath)) return true;
if (!cache.includes) return true;
for (var incFile in cache.includes) {
if (cacheIncludes[incFile].time !== time(incFile)) return true;
}
return false;
}
function include(options) {
options = options || {};
options.keyword = options.keyword || 'INCLUDE';
options.cache = options.cache || false;
options.showFiles = typeof options.showFiles === 'string'? options.showFiles :
options.showFiles? 'include-js:' : false;
options.ext = options.ext || 'js';
if (options.recursive === undefined) options.recursive = true;
if (options.ext[0] === '.') options.ext = options.ext.slice(1);
return through.obj(function(file, enc, cb) {
this.base = file.base || file.cwd;
this.filepath = file.path;
this.id = path.relative(this.base, this.filepath);
this.options = options;
this.includes = {};
if (options.cache) {
var cachename = options.cache === true? 'default': options.cache;
caches[cachename] = caches[cachename] || {
includes: {},
modules: {}
};
this.cacheIncludes = caches[cachename].includes;
this.cacheModules = caches[cachename].modules;
}
if (file.isNull()) {
this.push(file);
return cb();
}
if (file.isStream()) throw new Error('Streaming not supported');
// ignore _* files
var filename = path.basename(file.path);
if (filename[0] === '_') return cb();
// check if file was cached
if (options.cache && !isDirty(file.path, this.cacheModules, this.cacheIncludes)) {
return cb();
}
var s = file.contents.toString();
try {
s = exec.call(this, s, this.id, [this.filepath]);
} catch(e) {
this.emit('error', e);
return cb();
}
file.contents = new Buffer(s);
// save to cache
if (options.cache) this.cacheModules[file.path] = {
time: time(file.path),
includes: this.includes
};
if (options.showFiles) {
gutil.log(options.showFiles, magenta(path.relative(file.cwd, file.path)));
}
this.push(file);
cb();
});
}
module.exports = include;