transpile-code.js 708 B

12345678910111213141516171819202122
  1. var recast = require('recast');
  2. var transform = require('./transform-tree.js');
  3. module.exports = function(input, options) {
  4. options || (options = {});
  5. var sourceFileName = options.sourceFileName || '';
  6. var sourceMapName = options.sourceMapName || '';
  7. var createSourceMap = sourceFileName && sourceMapName;
  8. var tree = recast.parse(input, {
  9. 'sourceFileName': sourceFileName
  10. });
  11. tree = transform(tree);
  12. if (createSourceMap) {
  13. // If a source map was requested, return an object with `code` and `map`
  14. // properties.
  15. return recast.print(tree, {
  16. 'sourceMapName': sourceMapName
  17. });
  18. }
  19. // If no source map was requested, return the transpiled code directly.
  20. return recast.print(tree).code;
  21. };