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

ts-macros

v2.6.2

Published

A typescript transformer / plugin which allows you to write macros for typescript!

Downloads

395

Readme

ts-macros

ts-macros is a typescript transformer which allows you to create function macros that expand to javascript code during the transpilation phase of your program.

📖 Documentation 🎮 Playground ✍️ Examples

The Basics

All macro names must start with a dollar sign ($) and must be declared using the function keyword. Macros can then be called just like a normal function, but with a ! after it's name: $macro!(params). All the code inside of the macro is going to "expand" where the macro is called.

showcase

What you can do with ts-macros:

  • Generate repetitive code
  • Generate code conditionally, based on enviourment variables or other configuration files
  • Generate types which you can use in your code (read more here)
  • Create abstractions without the runtime cost

Usage

npm i --save-dev ts-macros
npm i --save-dev ts-patch

and add the ts-macros transformer to your tsconfig.json:

"compilerOptions": {
//... other options
"plugins": [
        { "transform": "ts-macros" }
    ]
}

Afterwards you can either:

  • Transpile your code using the tspc command that ts-patch provides.
  • Patch the instance of typescript that's in your node_modules folder with the ts-patch install command and then use the tsc command to transpile your code.
const TsMacros = require("ts-macros").default;

options: {
      getCustomTransformers: program => {
        before: [TsMacros(program)]
      }
}

To use transformers with ts-node, you'll have to change the compiler in the tsconfig.json:

npm i --save-dev ts-node
"ts-node": {
    "compiler": "ts-patch/compiler"
  },
  "compilerOptions": {
    "plugins": [
        { "transform": "ts-macros" }
    ]
  }

If you want to use ts-macros with:

  • tools that don't support typescript
  • tools that aren't written in javascript and therefore cannot run typescript transformers (tools that use swc, for example)
  • any tools' watch mode (webpack, vite, esbuild, etc)

you can use the CLI - read more about the CLI and example here

Security

This library has 2 built-in macros ($raw and $comptime) which execute arbitrary code during transpile time. The code is not sandboxed in any way and has access to your file system and all node modules.

If you're transpiling an untrusted codebase which uses this library, make sure to set the noComptime option to true. Enabling it will replace all calls to these macros with null without executing the code inside them. It's always best to review all call sites to $$raw and $$comptime yourself before transpiling any untrusted codebases.

ttypescript/ts-patch:

"plugins": [
        { "transform": "ts-macros", "noComptime": true }
    ]

manually creating the factory:

TsMacros(program, { noComptime: true });

Contributing

ts-macros is being maintained by a single person. Contributions are welcome and appreciated. Feel free to open an issue or create a pull request at https://github.com/GoogleFeud/ts-macros.