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 🙏

© 2025 – Pkg Stats / Ryan Hefner

rolldown-plugin-spacetimedb

v0.0.1

Published

[中文](./README.md#chinese-version)

Readme

Rolldown Plugin for SpacetimeDB Reducer Syntax

中文

This is a Rolldown (and Rollup-compatible) plugin that transforms an intuitive, TypeScript-first syntax for SpacetimeDB reducers into the verbose code required by the underlying SpacetimeDB library.

Motivation

Writing reducers for SpacetimeDB often involves boilerplate code, such as manually registering types with registerType and constructing type arrays for registerReducer. This plugin aims to improve the developer experience by allowing you to write reducers in a more natural and idiomatic TypeScript style.

The key benefits are:

  • Reduced Boilerplate: Eliminates the need for manual registerType and registerReducer calls.
  • Improved Readability: Code becomes cleaner and easier to understand.
  • Enhanced Type Safety: Leverages standard TypeScript type aliases and function parameter types.

How It Works

The plugin performs the following transformations at build time:

  1. Type Alias to registerType: Converts standard TypeScript type aliases into registerType calls.
  2. useReducer to registerReducer: Transforms a simplified useReducer function call into the full registerReducer call.
  3. Automatic Type Extraction: Parses the TypeScript type annotations from the reducer's arrow function parameters and automatically generates the required type array for registerReducer.
  4. Import Management: Automatically removes the developer-facing imports (e.g., from spacetimedb/composable) and adds the necessary low-level imports (e.g., registerReducer, registerType, type from spacetimedb).

Usage

You write your reducer code in a simple, declarative way.

Before Transformation (What you write)

This code is more intuitive and leverages standard TypeScript features.

// src/index.ts
import { useReducer, f32, bool, str, i64 } from 'spacetimedb/composable';

type Foo = {
    bar: f32,
    baz: str,
    count: i64,
}

useReducer('beepboop', (x: f32[], y: bool, z: Foo) => {
    // Reducer logic here...
});

After Transformation (What the plugin generates)

The plugin processes the code above and generates the following production-ready code that the SpacetimeDB runtime expects.

// dist/index.ts
import { registerReducer, registerType, type } from "spacetimedb";

const Foo = registerType("Foo", type.product({
	bar: type.f32,
	baz: type.string,
	count: type.i64
}));

registerReducer("beepboop", [
	type.array(type.f32),
	type.bool,
	Foo
], (x, y, z) => {});

中文版本 (Chinese Version)

这是一个 Rolldown (兼容 Rollup) 插件,用于转换 SpacetimeDB Reducer 的代码。

目的

我们的目标是,通过 Rolldown 插件,让开发者可以使用更符合直觉的 TypeScript 语法来编写 SpacetimeDB 的 Reducer,最终由插件编译为适用于生产环境、调用底层库的 TypeScript 代码。这样可以显著改善开发体验,减少样板代码。

编译前的优化代码 (开发者编写的代码)

代码更符合 TypeScript 语法,减少了不必要的类型定义,更加简洁易读。

// src/index.ts
import { useReducer, f32, bool, str, i64 } from 'spacetimedb/composable';

type Foo = {
    bar: f32,
    baz: str,
    count: i64,
}

useReducer('beepboop', (x: f32[], y: bool, z: Foo) => {
    // Reducer 逻辑...
});

最终生成的 TS 代码 (插件编译后的代码)

插件会自动处理类型别名和 useReducer 调用,生成 SpacetimeDB 运行时所期望的代码。

// dist/index.ts
import { registerReducer, registerType, type } from 'spacetimedb';

const Foo = registerType(
    'Foo',
    type.product({
        bar: type.f32,
        baz: type.string,
        count: type.i64
    })
);

registerReducer('beepboop', [type.array(type.f32), type.bool, Foo], (x, y, z) => {
    // z.bar;
});