How to use the Node.js API.
var babel = require("babel");
Transforms the passed in code
. Returning an object with the generated code,
source map, and AST.
babel.transform(code, [options]) // => { code, map, ast }
Example
var result = babel.transform("code();", options);
result.code;
result.map;
result.ast;
Asynchronously transforms the entire contents of a file.
babel.transformFile(filename, [options], callback)
Example
babel.transformFile("filename.js", options, function (err, result) {
result; // => { code, map, ast }
});
Synchronous version of babel.transformFile
. Returns the transformed contents of
the filename
.
babel.transformFileSync(filename, [options]) // => { code, map, ast }
Example
babel.transformFileSync("filename.js", options).code;
require("babel-core")
While the regular babel
package includes the CLI, we also publish a
babel-core
package that does not come with the CLI and has fewer dependencies
for API-only users. The API itself is identical to the babel
package.