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 🙏

© 2024 – Pkg Stats / Ryan Hefner

rbxts-transformer-macros

v0.0.11

Published

A roblox-ts transformer that allows you to add custom macros to (almost) any type.

Downloads

5

Readme

rbxts-transformer-macros

A roblox-ts transformer that allows you to add macros to instances, strings, etc at compile-time. This was specifically designed to act as a compile-time "wrapper" for instances, but works on almost any interface.

What is the purpose of wrapping instances?

Wrapping instances is a useful way to extend functionality of Roblox's instances, as they do not provide the ability to augment them natively.

Why use compile-time wrapping (macros), rather than runtime wrappers?

Runtime wrappers do not work well in practice, have performance overhead and require considerable thought when using them with C functions. For example, the following code would not work if instance was a runtime wrapped instance, since the value you act on is not an actual instance.

const objectValue: ObjectValue = ...;
const instance = ...;

objectValue.Value = instance; // runtime error, but no compile-time error

To use them with native C functions, or really anything expecting an actual instance, you'd have to convert your code to something like this.

const objectValue: ObjectValue = ...;
const instance = ...;

objectValue.Value = instance.inner;

Compile-time macros have zero overhead simply for existing, are easier to use, and generally more convenient.

Documentation

How to use the macros?

Even if you define the macros using $defineCallMacros or $definePropMacros, you can't access them quite yet. You'll have to add them to your type definitions, and there's two ways to do this.

You can declare an ambient interface to override the interface you're augmenting, like below.

// ambient.d.ts
interface Instance {
  GetComponent(name: string): Component;
  Components: Array<Component>;
}

Alternatively, you can make a wrapper interface, however this does require you to cast your value to the wrapper interface to work. This method is convenient if you have macros that only work in specific contexts, e.g client/server. It is far less convenient to have to cast values, so use ambient declarations when possible.

// anywhere.d?.ts
interface WrappedInstance extends Instance {
  GetComponent(name: string): Component;
  Components: Array<Component>;
}

const wrappedInstance = instance as WrappedInstance;
const wrappedPlayer = player as Player & WrappedInstance;

How to define a call macro?

Call macros must be in a ModuleScript (script.ts), and exported.

import { $defineCallMacros } from "rbxts-transformer-macros";

export const CALL_MACROS = $defineCallMacros<Instance>({
  GetComponent(name: string) {
    return findComponent(this, name);
  }
})

How to define a property macro?

Property macros must be in a ModuleScript (script.ts), and exported. A notable difference between property macros in runtime wrappers vs compile-time, compile-time property macros can yield if the macro yields. This can be a good thing, but it can also cause issues if you aren't aware of it.

import { $definePropMacros } from "rbxts-transformer-macros";

export const PROP_MACROS = $definePropMacros<Instance>({
  Components() {
    return getComponents(this);
  }
})