babel-plugin-macros weekly downloads statistics
By kentcdodds (2 listings) · npm · Babel plugins
Allows you to build compile-time libraries
babel-plugin-macros has 47,158,268 recorded weekly downloads on npm, an increase of 10.7% over the past 30 days, ranking #99 of 3,247 tracked listings.
About babel-plugin-macros
<!-- prettier-ignore-start -->
[![Build Status][build-badge]][build] [![Code Coverage][coverage-badge]][coverage] [![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends] [![MIT License][license-badge]][license] ![All Contributors][all-contributors-badge] [![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
<!-- prettier-ignore-end -->The problem
Check out this guest post on the Babel.js blog for a complete write up on the problem, motivation, and solution.
Currently, each babel plugin in the babel ecosystem requires that you configure it individually. This is fine for things like language features, but can be frustrating overhead for libraries that allow for compile-time code transformation as an optimization.
This solution
babel-plugin-macros defines a standard interface for libraries that want to use
compile-time code transformation without requiring the user to add a babel
plugin to their build system (other than babel-plugin-macros, which is ideally
already in place).
For instance, many css-in-js libraries have a css tagged template string function:
const styles = css`
.red {
color: red;
}
`
The function compiles your css into (for example) an object with generated class names for each of the classes you defined in your css:
console.log(styles) // { red: "1f-d34j8rn43y587t" }
This class name can be generated at runtime (in the browser), but this has some disadvantages:
- There is cpu usage/time overhead; the client needs to run the code to generate these classes every time the page loads
- There is code bundle size overhead; the client needs to receive a CSS parser in order to generate these class names, and shipping this makes the amount of js the client needs to parse larger.
To help solve those issues, many css-in-js libraries write their own babel plugin that generates the class names at compile-time instead of runtime:
// Before running through babel:
const styles = css`
.red {
color: red;
}
`
// After running through babel, with the library-specific plugin:
const styles = {red: '1f-d34j8rn43y587t'}
If the css-in-js library supported babel-plugin-macros instead, then they
wouldn't need their own babel plugin to compile these out; they could instead
rely on babel-plugin-macros to do it for them. So if a user already had
babel-plugin-macros installed and configured with babel, then they wouldn't
need to change their babel configuration to get the compile-time benefits of the
library. This would be most useful if the boilerplate they were using came with
babel-plugin-macros out of the box, which is true for
[create-react-app][cra].
Although css-in-js is the most common example, there are lots of other things
you could use babel-plugin-macros for, like:
- Compiling GraphQL fragments into objects so that the client doesn't need a GraphQL parser
- Eval-ing out code at compile time that will be baked into the runtime code, for instance to get a list of directories in the filesystem (see [preval][preval])
Table of Contents
<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> <!-- END doctoc generated TOC please keep comment here to allow auto update -->Installation
This module is distributed via [npm][npm] which is bundled with [node][node] and
should be installed as one of your project's devDependencies:
npm install --save-dev babel-plugin-macros
Usage
You may like to watch this YouTube video to get an idea of what macros is and how it can be used.
User docs
Are you trying to use babel-plugin-macros? Go to
other/docs/user.md.
Author docs
Are you trying to make your own macros that works with babel-plugin-macros? Go
to other/docs/author.md. (you should probably read the
user docs too).
Caveats
Babel cache problem
Note: This issue is not present when used in Create React App.
Most of the time you'll probably be using this with the babel cache enabled in webpack to rebuild faster. If your macro function is not pure which gets different output with same code (e.g., IO side effects) it will cause recompile mechanism fail. Unfortunately you'll also experience this problem while developing your macro as well. If there's not a change to the source code that's being transpiled, then babel will use the cache rather than running your macro again.
For now, to force recompile the code you can simply add a cache busting comment in the file:
import macro from 'non-pure.macro';…
Description from the npm listing.
- babel-plugin
- macros
- macro
- babel-macro
- babel-plugin-macro
- babel-macros
- babel-plugin-macros
README badge
Show your rank in a README or on your website. The badge updates automatically.
Charts
Images: 30d · 1y · all time · 1600×900 dark
Adoption over time
7/30/90-day growth
Ranking history
Listing placement
Listing placement
Filled points are observed values; hollow points come from Internet Archive snapshots of the listing page. Dashed segments span periods with no observations — values between points are not interpolated.
Each band is one page of 20. Weekly downloads on a linear scale. Placement is projected from weekly downloads, so it can differ from the marketplace's own sort order. Hover a dot to see the listing.
Recent observations
Full history (170)Similar npm Babel plugins plugins
All 20 babel-plugin-macros alternativesData source
Weekly downloads (trailing 7 complete days) come from the official npm downloads API. Downloads include CI and mirrors and are not equivalent to installs or users. npm publishes no ratings.
Listing: https://www.npmjs.com/package/babel-plugin-macros. First seen Oct 16, 2026; last successful collection Dec 6, 2026. See the methodology.