-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (44 loc) · 1.3 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
var through = require('through2')
var regex = {}
function extToRegExp (ext) {
return new RegExp((ext + '$').replace(/\./g, '\\.'))
}
function isTemplate (filename, ext) {
if (!regex[ext]) regex[ext] = extToRegExp(ext)
return regex[ext].test(filename)
}
function getArgs (options) {
var args = options.a
if (!args && options.args) args = options.args || options.args._
if (!args && options.arguments) args = options.arguments._
if (!args) args = ['render', 'data']
return args
}
function getExt (options) {
var ext = options.e
if (!ext && options.ext) ext = options.ext._ ? options.ext._[0] : options.ext
if (!ext && options.extension) ext = options.jxtension._[0]
if (!ext) ext = '.html'
return ext
}
module.exports = function hyperify (filename, options) {
var args = getArgs(options)
var ext = getExt(options)
var chunks = []
if (!isTemplate(filename, ext)) return through()
return through(transfrom, flush)
function transfrom (chunk, encoding, callback) {
chunks.push(chunk)
callback()
}
function flush (callback) {
var stream = this
var source = Buffer.concat(chunks).toString()
stream.push([
'module.exports = function (' + args.join(', ') + ') {',
' return ' + args[0] + '`' + source + '`;',
'}'
].join('\n'))
callback()
}
}