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

@typesugar/transformer

v0.1.0

Published

TypeScript transformer for typesugar macro expansion

Readme

@typesugar/transformer

TypeScript transformer for typesugar macro expansion.

Overview

@typesugar/transformer is the core transformation engine of typesugar. It integrates with the TypeScript compiler (via ts-patch or bundler plugins) to process macro invocations during compilation, expanding them into optimized JavaScript code.

You need this package if you're configuring the build pipeline directly. Most users should use unplugin-typesugar for bundler-specific plugins or configure via typesugar.

Installation

npm install @typesugar/transformer
# or
pnpm add @typesugar/transformer

Configuration

With ts-patch (for tsc users)

  1. Install ts-patch:

    npm install -D ts-patch
    npx ts-patch install
  2. Configure tsconfig.json:

    {
      "compilerOptions": {
        "plugins": [{ "transform": "@typesugar/transformer" }]
      }
    }

Transformer Options

interface MacroTransformerConfig {
  /** Enable verbose logging */
  verbose?: boolean;

  /** Custom macro module paths to load */
  macroModules?: string[];
}

How It Works

The transformer processes TypeScript source files during compilation:

  1. Import Resolution — Traces imports to their origin modules to determine which identifiers are macros
  2. Macro Expansion — Expands expression macros, attribute macros, derive macros, tagged templates, type macros, and labeled block macros
  3. Import Cleanup — Removes imports that only brought macro placeholders into scope
  4. Diagnostics — Reports errors and warnings through the TypeScript diagnostic pipeline

Supported Macro Types

| Type | Trigger | Example | | --------------- | ----------------- | ------------------------------ | | Expression | Function call | comptime(() => 1 + 1) | | Attribute | Decorator | @operators class Vec { } | | Derive | @derive() | @derive(Eq, Clone) | | Tagged Template | Template literal | sql`SELECT * FROM users` | | Type | Type reference | type X = Add<1, 2> | | Labeled Block | Labeled statement | let: { x << expr } |

CLI Usage

The transformer includes a CLI for direct usage:

# Build TypeScript files with macro expansion
npx typesugar build

# Watch mode
npx typesugar watch

# Type-check only (no emit)
npx typesugar check

# Show expanded output (like cargo expand)
npx typesugar expand src/file.ts

Programmatic Usage

import macroTransformerFactory from "@typesugar/transformer";
import * as ts from "typescript";

const program = ts.createProgram(["src/index.ts"], {
  // compiler options
});

const transformer = macroTransformerFactory(program, {
  verbose: true,
});

// Use with ts.transform() or a custom emit pipeline

Import-Scoped Macro Resolution

The transformer uses TypeScript's type checker to resolve macro identifiers:

// Direct import
import { comptime } from "typesugar";
comptime(() => 1 + 1); // ✓ Expanded

// Renamed import
import { comptime as ct } from "typesugar";
ct(() => 1 + 1); // ✓ Expanded (follows alias)

// Barrel re-export
// utils.ts: export { comptime } from "typesugar";
import { comptime } from "./utils";
comptime(() => 1 + 1); // ✓ Expanded (follows re-export chain)

// Not a macro (different module)
function comptime(fn: () => number) {
  return fn();
}
comptime(() => 1 + 1); // ✗ Not expanded (not from macro module)

License

MIT