-
Notifications
You must be signed in to change notification settings - Fork 20
/
transpile-code.js
31 lines (29 loc) · 1.12 KB
/
transpile-code.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
'use strict';
const recast = require('recast');
const transform = require('./transform-tree.js');
module.exports = function(input, options) {
options || (options = {});
const sourceFileName = options.sourceFileName || '';
const sourceMapName = options.sourceMapName || '';
const enableDotAllFlag = options.dotAllFlag || false;
const enableUnicodePropertyEscapes = options.unicodePropertyEscape || false;
const useUnicodeFlag = options.useUnicodeFlag || false;
const createSourceMap = sourceFileName && sourceMapName;
const tree = recast.parse(input, {
'sourceFileName': sourceFileName
});
const transformed = transform(tree, {
'dotAllFlag': enableDotAllFlag ? 'transform' : false,
'unicodePropertyEscapes': enableUnicodePropertyEscapes ? 'transform' : false,
'unicodeFlag': useUnicodeFlag ? false : 'transform',
});
if (createSourceMap) {
// If a source map was requested, return an object with `code` and `map`
// properties.
return recast.print(transformed, {
'sourceMapName': sourceMapName
});
}
// If no source map was requested, return the transpiled code directly.
return recast.print(transformed).code;
};