-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
26 lines (22 loc) · 911 Bytes
/
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
var babel = require('@babel/core'),
djangoGettextTransformPlugin = require('./babel-django-gettext-transform-plugin');
var matchPatterns = [
'\{\{(.+?)\}\}',
':[a-zA-Z]+="[^_\n]*(_[\(]\'.+\'[\)])"'
],
regExp = new RegExp(matchPatterns.join('|'), 'gi');
module.exports = function (source) {
source = source.replace(regExp, function (full, ...matched) {
var matchedExpression = matched.slice(0, matchPatterns.length).filter(Boolean)[0];
var result = babel.transform(matchedExpression.trim(), {
plugins: [djangoGettextTransformPlugin],
retainLines: true
});
var newExpression = result.code.replace(/;$/, '');
if (new RegExp(matchPatterns[0]).test(full)) {
newExpression = ' ' + newExpression + ' ';
}
return full.replace(matchedExpression, newExpression);
});
return source;
};