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

@onrails/twoslash

v0.1.3

Published

Extract #region snippet blocks from real, tested source modules into display + twoslash forms for type-checked documentation.

Readme

@onrails/twoslash

Extract #region snippet blocks from real, tested source modules into two forms for documentation: a clean display snippet and a self-contained twoslash module with hover types.

The idea: keep example code in actual .ts modules that your test/typecheck gate compiles, mark the part you want to show with #region snippet, and generate doc snippets from it — so rendered docs can never drift from compiling reality.

Runs on Node (≥18) and Bun. No runtime dependencies.

Install

bun add -d @onrails/twoslash
# or: npm i -D @onrails/twoslash

Usage

Mark a region in an example module. Anything imported from your fixtures file is hidden in the rendered output but kept for type-checking:

// examples/checkout.ts
import { cart, charge } from "./fixtures.js"; // hidden in docs, kept for type-checking

// #region snippet
import { ok } from "@scope/result";

const receipt = charge(cart);
export const result = ok(receipt);
// #endregion

Generate the snippets module:

import { extractSnippets } from "@onrails/twoslash";

await extractSnippets({
  srcDir: "examples",
  outFile: "docs/snippets.generated.ts",
  // optional: rewrite relative source imports to published specifiers
  rewriteImport: (line) =>
    line.replace(/from "(?:\.\.\/)+([\w-]+)\/src\/index\.js"/, 'from "@scope/$1"'),
});

Each entry in the generated module has two forms:

  • code — display form: region only, export stripped, fixture imports dropped, imports optionally rewritten.
  • twoslash — a self-contained module: shown imports on top, fixtures hidden inside // ---cut-start--- / // ---cut-end---, body below — so twoslash type-checks everything but renders only the snippet, with live hover types.
// docs/snippets.generated.ts (generated)
export const snippets = {
  checkout: { code: "…", twoslash: "…" },
} as const;

export type SnippetId = keyof typeof snippets;

API

extractSnippets(options)

Scans srcDir, writes the generated module to outFile. Returns { count, outFile, skipped }.

| Option | Default | Purpose | | --- | --- | --- | | srcDir | — | directory scanned for *.ts / *.tsx snippet modules | | outFile | — | path of the generated module to write | | fixtureName | "fixtures" | basename of the shared fixtures module in srcDir | | rewriteImport | identity | rewrites a single import line (e.g. relative → package specifier) | | sourceLabel | srcDir | label in the generated-file header comment | | generatedBy | "extract-snippets" | attribution in the // Generated by … header |

buildSnippetsModule(modules, fixturesSource, options?)

The pure core behind extractSnippets — no disk access, unit-testable. Takes { name, source }[] plus the fixtures source, returns { module, ids, skipped }. Use it when you source modules from somewhere other than the filesystem.

How it pairs with twoslash

Feed the twoslash form to fumadocs-twoslash or any twoslash renderer. The fixtures live below the cut markers, so they type-check the snippet without appearing in the output.