0.7.0 • Published 3 years ago

escorn v0.7.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

escorn

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 escorn
# or via yarn
yarn add -D escorn

Transform example:

import { transform, parse } from "escorn";

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 "escorn";

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.