npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

yuku-codegen

v0.5.11

Published

High-performance JavaScript/TypeScript code generator written in Zig

Readme

yuku-codegen

A high-performance JavaScript and TypeScript code generator written in Zig, powered by Yuku.

Renders an ESTree / TypeScript-ESTree AST back to source code, with optional Source Map V3 output. The input is exactly the ParseResult produced by yuku-parser.

Install

npm install yuku-codegen

Usage

import { parse } from "yuku-parser";
import { print } from "yuku-codegen";

const ast = parse("const x = 1 + 2;");
const { code } = print(ast);

console.log(code); // "const x = 1 + 2;"

API

Three entry points share the same options and result shape. Each accepts the full ParseResult from yuku-parser.

| Function | Behavior | | -------- | ----------------------------------------------------------------------------------------------------- | | print | Renders verbatim, preserving TypeScript syntax. | | strip | Drops type-only syntax and emits plain JavaScript. See TypeScript stripping. | | minify | Applies size-reducing rewrites at print time. See Minification. |

All return a CodegenResult:

interface CodegenResult {
  code: string;
  errors: Diagnostic[];
  map: SourceMap | null;
}

errors is empty on a clean run. map is non-null only when sourceMaps is set.

Options

const result = print(ast, {
  format: "pretty",
  indent: 2,
  quotes: "double",
  comments: "some",
  sourceMaps: { sourceFileName: "in.js" },
});

| Option | Type | Default | Description | | ------------ | ---------------------------------------- | ---------- | ---------------------------------------------------------------------------- | | format | "pretty" \| "compact" | "pretty" | Whitespace mode. "compact" emits only the separators the grammar requires. | | indent | number | 2 | Spaces per indentation level. Applies in pretty mode only. | | quotes | "double" \| "single" | "double" | Quote style for emitted string literals. | | comments | boolean \| "some" \| "line" \| "block" | "some" | Comment passthrough filter. See Comments. | | sourceMaps | SourceMapOptions | disabled | Pass an object to emit a Source Map V3 alongside the code. Omit to skip. |

Source maps

Pass sourceMaps to emit a Source Map V3 alongside the generated code.

import { parse } from "yuku-parser";
import { print } from "yuku-codegen";

const source = `const greet = (name) => "Hello, " + name;`;
const ast = parse(source);

const { code, map } = print(ast, {
  sourceMaps: {
    file: "out.js",
    sourceFileName: "in.js",
    sourcesContent: source,
  },
});

await Bun.write("out.js", `${code}\n//# sourceMappingURL=out.js.map`);
await Bun.write("out.js.map", JSON.stringify(map));

SourceMapOptions

| Field | Type | Description | | ---------------- | -------- | ----------------------------------------------------------------------------------- | | file | string | Output filename, embedded as the map's file. | | sourceFileName | string | Source filename, embedded as the single entry of sources. | | sourceRoot | string | Prefix embedded as sourceRoot. | | sourcesContent | string | When set, embedded as the single entry of the map's sourcesContent. Omit to skip. |

Output shape

map is a Source Map V3 object, ready to serialize with JSON.stringify:

interface SourceMap {
  version: 3;
  file: string | null;
  sourceRoot: string | null;
  sources: string[];
  sourcesContent: (string | null)[] | null;
  names: string[];
  mappings: string;
}

Columns are 0-indexed UTF-16 code units, matching Chrome DevTools and consumer-side libraries like @jridgewell/trace-mapping and source-map.

TypeScript stripping

strip rewrites the AST as plain JavaScript.

import { parse } from "yuku-parser";
import { strip } from "yuku-codegen";

const ast = parse(`const x: number = 1;`, { lang: "ts" });
console.log(strip(ast).code); // "const x = 1;"

Type annotations, type aliases, interfaces, and other type-only constructs are dropped. Constructs that have no clean JavaScript equivalent (enum, namespace, import = require(), export =) are reported in errors and elided. The output is always syntactically valid JavaScript.

Comments

The comments option selects which entries from ast.comments are emitted. The default is "some", which matches the bundler convention of keeping legal banners, JSDoc, and tree-shaking annotations while dropping plain noise.

| Value | Behavior | | --------- | --------------------------------------------------------------- | | "some" | Emit legal headers, JSDoc, and @/# annotations. (default) | | true | Emit every comment. | | false | Drop every comment. | | "line" | Emit // ... only. | | "block" | Emit /* ... */ only. |

const ast = parse(`// hello\nconst x = 1;`);
print(ast, { comments: true }).code;
// "// hello\nconst x = 1;"

Minification

minify applies size-reducing rewrites at print time:

  • true and false rewrite to !0 and !1.
  • undefined rewrites to void 0 (in expression position).
  • Infinity rewrites to 1/0.
  • Numeric literals shorten to their shortest form (1000000 becomes 1e6, 0.5 becomes .5).
  • obj["foo"] rewrites to obj.foo when the key is a valid identifier.
  • { "foo": x } rewrites to { foo: x } when safe.

Combine with format: "compact" for full minification:

import { minify } from "yuku-codegen";

const { code } = minify(ast, { format: "compact" });

License

MIT