zecorn

raw JSON →
0.10.1 verified Fri May 01 auth: no javascript

zecorn is a Babel-compatible transpiler built on acorn, version 0.10.1. It provides a babel plugin layer with strong source mapping, enabling reuse of existing Babel plugins. Developed for WMR but usable standalone. Unlike Babel, it uses acorn for parsing, offering lighter weight and faster transforms. Ships TypeScript types. Release cadence is sporadic; check GitHub for latest.

error Cannot find module 'zecorn'
cause zecorn not installed or not in node_modules.
fix
Run 'npm install -D zecorn' or 'yarn add -D zecorn'.
error TypeError: (0 , zecorn.transform) is not a function
cause Incorrect import style; using require with ESM package.
fix
Use ES module import syntax: import { transform } from 'zecorn'
error Error: The 'parse' option must be a function
cause Missing or incorrect parse function passed to transform.
fix
Ensure you pass a valid parse function, e.g., import { parse } from 'zecorn' and pass it as { parse }.
breaking zecorn uses acorn for parsing, not Babel's parser. Some Babel plugins relying on Babel-specific AST types may not work.
fix Use only plugins that are compatible with acorn AST or wrap them with acorn-specific adapters.
gotcha The 'parse' option in transform must provide a function with a specific signature. Using a custom parser without proper signature may cause errors.
fix Ensure parse function accepts (code, opts) and returns an AST object compatible with acorn.
deprecated This library is designed for WMR and may not receive frequent updates. Check for new releases before using in production.
fix Monitor the repository for updates or consider using alternatives like Babel standalone.
npm install zecorn
yarn add zecorn
pnpm add zecorn

Shows basic usage of transform with a custom Babel plugin replacing a numeric literal.

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); // 'const a = foobar;'