zecorn
v0.10.1
Published
Acorn transpiler with a babel compat layer
Downloads
25
Maintainers
Readme
zecorn
A babel compatibility layer built on acorn with strong source mapping capabilities. The main focus is to generate a readable output and making it easy to reuse existing babel plugins.
This library was built for WMR, but should be usable outside of that.
Usage
Installation:
npm install -D zecorn
# or via yarn
yarn add -D zecorn
Transform example:
import { transform, parse } from "zecorn";
function MyBabelPlugin({ types: t }) {
return {
name: "my-plugin",
visitor: {
NumericLiteral(path) {
path.replaceWith(t.identifier("foobar"));
},
},
};
}
const result = transform(`const a = 42`, {
parse,
plugins: [myBabelPlugin],
});
console.log(result.code);
// Logs: `const a = foobar;`
Alternative usage with a custom acorn instance:
import * as acorn from "acorn";
import { transform, acornPlugins } from "zecorn";
const parser = acorn.Parser.extend(...acornPlugins);
const parse = (code, opts) =>
parser.parse({
...opts,
sourceType: "module",
ecmaVersion: "latest",
});
const result = transform(`const a = 42`, {
parse,
plugins: [],
});
License
MIT, see the license file.