Yuku

Codegen

Codegen prints a tree back to source, with optional type stripping, minification, and source maps. From JavaScript, see print.

var tree = try parser.parse(allocator, "const x: number = 1;", .{ .lang = .ts });
defer tree.deinit();

const result = try parser.codegen.generate(allocator, &tree, .{ .strip = true });
defer result.deinit(allocator);

std.debug.print("{s}\n", .{result.code}); // const x = 1;
pub fn generate(allocator: std.mem.Allocator, tree: *const ast.Tree, options: Options) error{OutOfMemory}!Result

pub const Result = struct {
    code: []const u8,
    diagnostics: []const ast.Diagnostic, // what strip could not print
    map: ?SourceMap = null,

    pub fn deinit(self: Result, allocator: std.mem.Allocator) void
};

generate prints from tree.root and leaves the tree unchanged. The result belongs to allocator.

Options

FieldDefaultDescription
stripfalsePrint TypeScript as JavaScript, see Type stripping
minifyfalseRewrite syntax to shorter forms, see Minification
format.pretty.pretty, or .compact with only the whitespace the grammar needs
indent2Spaces per level in the pretty format
quotes.preserve.preserve, .double, .single, or .shortest
comments.some.some, .all, .none, .line, or .block
source_mapnullA SourceMapOptions, see Source maps

Comments print from the nodes they are attached to, so parse with .comments = .attach to keep them. .some keeps the block comments bundlers keep: those opening with ! or *, those whose text starts with @ or #, and those holding @license, @preserve, or @cc_on.

Type stripping

strip prints TypeScript as JavaScript. Types, interfaces, type aliases, generics, assertions, satisfies, non-null !, declare, and abstract strip cleanly. enum, namespace, export =, import = require(), and parameter properties need new runtime code, so they are left out and reported in diagnostics. Their ambient forms strip silently.

Minification

minify rewrites syntax, and with .compact and .shortest it fully minifies.

const result = try parser.codegen.generate(allocator, &tree, .{
    .minify = true,
    .format = .compact,
    .quotes = .shortest,
});
// const enabled = true; const size = 1000000;
// prints as const enabled=!0;const size=1e6
  • true and false become !0 and !1.
  • Numbers take their shortest form, so 1000000 becomes 1e6 and 0.5 becomes .5.
  • obj["foo"] becomes obj.foo, and a quoted key such as { "foo": x } becomes { foo: x }, where that keeps the meaning.
  • </script, <!--, and --> are escaped in strings and untagged templates, so the output is safe inside a <script> tag.

Source maps

const result = try parser.codegen.generate(allocator, &tree, .{
    .source_map = .{
        .file = "out.js",
        .source_file_name = "in.js",
        .source_root = null,
        .sources_content = source,
    },
});
defer result.deinit(allocator);

const map = result.map.?;
pub const SourceMap = struct {
    version: u8 = 3,
    file: ?[]const u8,                     // borrowed from the options
    source_root: ?[]const u8,              // borrowed from the options
    sources: []const []const u8,           // one entry
    sources_content: ?[]const ?[]const u8,
    names: []const []const u8,             // empty
    mappings: []const u8,                  // VLQ
};

Columns are UTF-16 code units, as browsers and source map libraries expect. Written as JSON, source_root and sources_content take the Source Map V3 keys sourceRoot and sourcesContent.

A build that never needs maps compiles them out, and map is then always null.

const yuku = b.dependency("yuku", .{ .@"codegen-source-maps" = false });