-
Notifications
You must be signed in to change notification settings - Fork 1
/
mithrilComponents.js
250 lines (207 loc) · 7 KB
/
mithrilComponents.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/* jshint node:true */
// This code is based on https://github.com/ng-vu/gulp-include-js . Much thanks!
'use strict';
var fs = require('fs');
var gutil = require('gulp-util');
var path = require('path');
var through = require('through2');
var trim = require('./lib/trim');
//var pluginName = 'mithril-components-html';
var magenta = gutil.colors.magenta;
var cyan = gutil.colors.cyan;
var caches = {};
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 ? 'mithril-components:' : false);
if (options.recursive === undefined) options.recursive = true;
return through.obj(function (file, enc, cb) {
var self = this;
var jsFile;
this.base = file.base || file.cwd;
this.filepath = file.path;
this.id = path.relative(this.base, this.filepath);
this.options = options;
this.includes = {};
/*todo enable caching
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()) throwError('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 str = file.contents.toString();
//str = makeComponent(str, this.id, [this.filepath]);
try {
var str = file.contents.toString();
str = makeComponent(str, this.id, [this.filepath]);
} catch(e) {
this.emit('error', e);
return cb();
}
file.contents =
new Buffer(str);
/*
// 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)));
}
file.path = gutil.replaceExtension(file.path, '.js');
this.push(file);
cb();
// handle the <!==js:
function makeComponent (html, id, stack) {
var regex = new RegExp('^\<\!--js: (.+) --\>', 'i');
var m = html.match(regex);
if (!m) throwError(file.path, ': no JS file specification.');
if (!m[1])throwError(file.path, ': no JS file specified.');
jsFile = m[1];
if (jsFile[0] === '.') jsFile = path.join(self.base, jsFile);
str = readIncFile(jsFile);
str = str.replace(/COMPONENT_NAME/g, path.basename(file.path, '.html'));
str = insertMixins(str, html);
return exec(str, self.id, [self.filepath]);
}
// handle MIXIN
function insertMixins (str, html) {
var mixinRegex = new RegExp('(//[^\r\n]*)?([^\\s]+[ \\t\\v]*)?' + 'MIXIN' + '\\s*\\( *[\'"]([^\'"]*)[\'"]\\s*\\)');
var result = '';
var m = mixinRegex.exec(str);
while (m) {
var isComment = m[1];
var inline = m[2];
var mixinName = m[3];
result += str.slice(0, m.index) + (isComment ? m[0] : ((inline || '') + extractMixin(html, mixinName)));
str = str.slice(m.index + m[0].length);
m = mixinRegex.exec(str);
}
return result + str;
}
// handle INCLUDE
function exec(str, id, stack) {
var r = new RegExp('(//[^\r\n]*)?([^\\s]+[ \\t\\v]*)?' + options.keyword + '\\s*\\( *[\'"]([^\'"]*)[\'"]\\s*\\)');
var result = '';
var m = r.exec(str);
while (m) {
var isComment = m[1];
var inline = m[2] || '';
var relativePath = m[3];
var mixinName = '';
var i = relativePath.indexOf(':');
if (i !== -1) {
mixinName = relativePath.substr(i + 1);
relativePath = relativePath.substr(0, i);
}
if (relativePath[0] === '.') relativePath = path.join(path.join(path.dirname(id), relativePath));
result += str.slice(0, m.index);
if (isComment) {
result += m[0];
} else {
var including = read(relativePath, stack);
including = extractMixin (including, mixinName);
if (inline) including = trim(including);
result += inline + including;
}
str = str.slice(m.index + m[0].length);
m = r.exec(str);
}
return result + str;
}
function read (relativePath, stack) {
var filePath = options.exactName ?
path.join(self.base, relativePath) :
path.join(self.base, path.dirname(relativePath), path.basename(relativePath));
var newStack = stack.concat([filePath]);
if (stack.indexOf(filePath) >= 0) throwError(file.path, jsFile, 'INCLUDE', ': circular ' +
newStack.map(function(i){ return magenta(i); }).join(', '));
self.includes[filePath] = self.includes[filePath] || time(filePath);
var s = readIncFile(filePath);
if (options.recursive) s = exec(s, relativePath, newStack);
if (options.transform) s = options.transform(s);
return s;
}
function readIncFile (filePath) {
var s;
/*
var isCache = options.cache;
if (isCache) {
var cache = self.cacheIncludes[filePath];
if (cache && time(filePath) === cache.time) return cache.content;
}
*/
try {
s = fs.readFileSync(filePath, {encoding: 'utf8'});
} catch (e) {
throwError(file.path, jsFile || '', filePath, ': ' + e.message);
}
/*
if (isCache) self.cacheIncludes[filePath] = {
time: time(filePath),
content: s
};
*/
return s;
}
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 time(filePath) {
try {
var stat = fs.statSync(filePath);
return stat ? stat.mtime.getTime() : null;
} catch(e) {
return null;
}
}
function extractMixin (html, mixinName) {
if (!mixinName) { return html; }
var blockRegex = new RegExp('<!--MIXIN ' + mixinName + ' -->([\\s\\S]*?)(?:<!--MIXIN|$)');
var m = blockRegex.exec(html);
if (m) {
return trim(m[1]);
} else {
//return '';
throwError(file.path, jsFile, 'MIXIN', mixinName, ': block not found.');
}
}
});
}
function throwError (/* arguments */) {
var args = Array.prototype.slice.call(arguments);
args[0] = cyan(args[0]);
throw new Error(args.join(' '));
}
module.exports = include;