-
Notifications
You must be signed in to change notification settings - Fork 1
/
js.js
43 lines (35 loc) · 1.08 KB
/
js.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
var through = require('through')
, falafel = require('falafel')
, unparse = require('escodegen').generate
, util = require('util')
var re = /\.js$/
module.exports = function (file, options) {
if (!re.test(file)) return through()
options = options || {}
var lang = options.lang || options.l || process.env['JEDIFY_LANG'] || 'en'
var buf = []
, stream = through(write, end)
return stream
function write(chunk) {
buf.push(chunk)
}
function end () {
var output = buf.join('')
try {
output = falafel(output, function (node) {
if (node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
node.callee.name === 'requirePo')
{
var dir = new Function([], 'return ' + unparse(node.arguments[0]))()
dir = util.format(dir, lang)
node.update('require(' + JSON.stringify(dir) + ')')
}
}).toString()
} catch (err) {
this.emit('error', new Error(err.toString().replace('Error: ', '') + ' (' + file + ')'))
}
stream.queue(output)
stream.queue(null)
}
}