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

rollup-plugin-ast-macros

v0.8.0

Published

Apply AST-based macros at build-time

Downloads

12

Readme

AST-based macro transforms for Rollup

Functions can improve readability. Inlining expanded constants can improve performance.

Build-time AST macros allow you to take advantge of both.

Example

Virtual DOM Example

Build-time macro for an IJK-like transform for use with hyperapp, superfine, or preact

MACRO({
  H(nodeName, attributes, ...children) {
    const parts = { nodeName, attributes }
    if (0 !== children.length) {
      parts.children = `[${children.join(', ')}]`
    }

    const body = Object.entries(parts)
      .map(e => e.join(': '))
      .join(', ')

    return `({ ${body} })`
  },
})

export const view = count =>
  H("div", {}
    , H("h1", {}, count)
    , H("button", { onclick: () => render(count - 1) }, "-")
    , H("button", { onclick: () => render(count + 1) }, "+") )
Output (with prettier)
export const view = count => ({
  nodeName: 'div',
  attributes: {},
  children: [
    { nodeName: 'h1', attributes: {}, children: [count] },
    {
      nodeName: 'button',
      attributes: { onclick: () => render(count - 1) },
      children: ['-']
    },
    {
      nodeName: 'button',
      attributes: { onclick: () => render(count + 1) },
      children: ['+']
    }
  ]
})

CSS-in-JS Example

MACRO({
  EXPR: expr => new Function(`return (${expr})`).call(null),

  LET(expr) {
    Object.assign(global, this.EXPR(expr))
    return ''
  },

  CSS(strings, ...args) {
    let res = '`' + strings[0]
    for (let i=0; i<args.length; i++)
      res += args[i] + (strings[i+1] || '')
    res += '`'
    return res
  },
})

LET({
  color: 'blue',
  height: 100,
  dayIsOdd: new Date().getDay() & 1,
  buildDate: new Date().toISOString().replace(/:\d\d\..*Z/, ''),
})

const css_styles = CSS`
  .somewhere {
    top: ${ EXPR(20 + height) } px;
    border: ${ EXPR(1 + 2) } px ${ EXPR( dayIsOdd ? 'solid' : 'dashed' ) } ${color};
  }
  .somewhere::before {
    content: "${ EXPR(buildDate) }";
  }
`
Output (with prettier)
const css_styles = `
  .somewhere {
    top: 120 px;
    border: 3 px solid color;
  }
  .somewhere::before {
    content: "2018-08-31T16:05";
  }
`;

Implementation

The heavy lifting for AST transform is provided by the excellent transform-ast and magic-string packages. Macro functions are evaluated in VM2.NodeVM isolation, providing a measure of protection from abuse.

API

MACRO(«object expression»)

Allows defining new macros and variables. A single argument of type object is expected. The expression will be evaluated in a VM2.NodeVM context and the result assigned to the macro namespace. Any functions defined at the root will define new macros.

Defined macro functions

The macro's arguments are the getSource() strings of the AST arguments nodes. (e.g. ast_node.arguments.map(e => e.getSource())

MACRO({
  url: require('url'),

  EXAMPLE_SRC(...args) {
    return JSON.stringify(args)
  },
})
Allowed Builtin modules:
  • require('os')
  • require('util')
  • require('zlib')
  • require('assert')
  • require('path')
  • require('url')
  • require('querystring')
  • require('punycode')

This can be customized with {vm2: {builtin: []}}

AST transformations

Additionally, this.ast_node is the macro's CallExpression AST Node, enabling sophisticated AST-based transformations.

MACRO({
  EXAMPLE_ARROW_AST() {
    // translated from https://github.com/goto-bus-stop/transform-ast#example

    for (const node of this.ast_node.arguments) {
      if (node.type !== 'ArrowFunctionExpression')
        continue;

      const params = node.params.map(param =>param.getSource())

      if (node.body.type !== 'BlockStatement')
        node.body.edit.update(`{ return ${node.body.getSource()} }`)

      node.edit.update(`function (${params.join(', ')}) ${node.body.getSource()}`)
    }
  },
})

Rollup Plugin Options

import rpi_ast_macros from 'rollup-plugin-ast-macros'

// defaults:
const macros = null // or {}
const sandbox = null // or {}
const builtin = ['os', 'util', 'zlib', 'assert', 'path', 'url', 'querystring', 'punycode']

const plugins = [
  rpi_ast_macros({
    // include: 'code/**',
    exclude: ['node_modules/**'],

    ast_opt: {}, // passed to transform_ast(source, ast_opt)

    macros, // additional default macros & `this` context for macro invocation

    vm2_opt: {
      sandbox, // passed to VM2.NodeVM({sandbox})
      builtin, // passed to VM2.NodeVM({require:{builtin})
    },
  })
]

License

MIT