I'm trying to use babel with npm and I think the package I need is babel-core. Specifically what I want to do is pass it a string of ES6 code and get it to hand me back a string of transpiled code. That's it. You'd think that would be simple but I can not figure this out with the documentation. From what I've read, I should be able to do this:
var babel = require('babel-core');
var code = 'x => x + 1';
var result = babel.transform(code);
But the problem is, result is an object containing an AST, not a string. I tried to run transformFromAst on that object but that doesn't work either. Can anyone help me get the actual transpiled string?
result.code will have the ES5 code in it. result.ast will have the ast. It probably didn't look like it because you aren't passing any options to Babel, so it will just pass through the code unchanged, e.g.
npm install babel-preset-es2015
and
var result = babel.transform(code, {
presets: ['es2015']
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With