Gathering detailed insights and metrics for babel-plugin-macros
Gathering detailed insights and metrics for babel-plugin-macros
Gathering detailed insights and metrics for babel-plugin-macros
Gathering detailed insights and metrics for babel-plugin-macros
@types/babel-plugin-macros
TypeScript definitions for babel-plugin-macros
graphql.macro
Compile GraphQL AST at build-time with babel-plugin-macros.
graphql-let
A webpack loader/babel-plugin/babel-plugin-macros/CLI/generated file manager of GraphQL code generator.
styled-components.macro
A `babel-plugin-macros` macro for styled-components
π£ Allows you to build simple compile-time libraries
npm install babel-plugin-macros
89.1
Supply Chain
99
Quality
75.7
Maintenance
100
Vulnerability
99.6
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
2,626 Stars
175 Commits
135 Forks
17 Watching
1 Branches
51 Contributors
Updated on 24 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-6%
2,872,780
Compared to previous day
Last week
2%
16,150,074
Compared to previous week
Last month
5.5%
68,567,058
Compared to previous month
Last year
-1.5%
784,052,470
Compared to previous year
Allows you to build simple compile-time libraries
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.
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:
1const styles = css` 2 .red { 3 color: red; 4 } 5`
The function compiles your css into (for example) an object with generated class names for each of the classes you defined in your css:
1console.log(styles) // { red: "1f-d34j8rn43y587t" }
This class name can be generated at runtime (in the browser), but this has some disadvantages:
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:
1// Before running through babel: 2const styles = css` 3 .red { 4 color: red; 5 } 6` 7// After running through babel, with the library-specific plugin: 8const 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
.
Although css-in-js is the most common example, there are lots of other things
you could use babel-plugin-macros
for, like:
This module is distributed via npm which is bundled with node and
should be installed as one of your project's devDependencies
:
npm install --save-dev babel-plugin-macros
You may like to watch this YouTube video to get an idea of what macros is and how it can be used.
Are you trying to use babel-plugin-macros
? Go to
other/docs/user.md
.
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).
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:
1import macro from 'non-pure.macro'; 2 3-// Do some changes of your code or 4+// add a cache busting comment to force recompile. 5macro('parameters');
This problem is still being worked on and is not unique to
babel-plugin-macros
. For more details and workarounds, please check related
issues below:
You can write your own without publishing them to npm
, but if you'd like to
see existing macros you can add to your project, then take a look at the
Awesome babel macros
repository.
Please add any you don't see listed!
Let's use
babel-plugin-console
as
an example.
If we used babel-plugin-console
, it would look like this:
babel-plugin-console
to .babelrc
1function add100(a) { 2 const oneHundred = 100 3 console.scope('Add 100 to another number') 4 return add(a, oneHundred) 5} 6 7function add(a, b) { 8 return a + b 9}
When that code is run, the scope
function does some pretty nifty things:
Browser:
Node:
Instead, let's use the macro it's shipped with like this:
babel-plugin-macros
to .babelrc
(only once for all macros)1import scope from 'babel-plugin-console/scope.macro' 2function add100(a) { 3 const oneHundred = 100 4 scope('Add 100 to another number') 5 return add(a, oneHundred) 6} 7 8function add(a, b) { 9 return a + b 10}
The result is exactly the same, but this approach has a few advantages:
Advantages:
.babelrc
for all macros used in project. Add that
once and you can use all the macros you wantbabel-plugin-macros
, so no configuration is needed at allconsole.scope
people may be fooled that it's just a
normal console
API when there's really a babel transpilation going on. When
you import scope
, it's obvious that it's macro and does something with the
code at compile time. Some ESLint rules may also have issues with plugins that
look for "global" variablesbabel-plugin-console
you wont find out until you run the
code. If you misconfigure babel-plugin-macros
you'll get a compile-time
error.Drawbacks:
This is another advantage of babel-plugin-macros
over regular plugins. The
user of the macro is in control of the ordering! The order of execution is the
same order as imported. The order of execution is clear, explicit and in full
control of the user:
1import preval from 'preval.macro' 2import idx from 'idx.macro' 3 4// preval macro is evaluated first, then idx
This differs from the current situation with babel plugins where it's prohibitively difficult to control the order plugins run in a particular file.
No! Any AST node type is supported.
It can be tagged template literal:
1import eval from 'eval.macro' 2const val = eval`7 * 6`
A function:
1import eval from 'eval.macro' 2const val = eval('7 * 6')
JSX Element:
1import Eval from 'eval.macro' 2const val = <Eval>7 * 6</Eval>
Really, anything...
See the testing snapshot for more examples.
All examples above were explicit - a macro was imported and then evaluated with a specific AST node.
Completely different story are implicit babel plugins, like transform-react-constant-elements, which process whole AST tree.
Explicit is often a better pattern than implicit because it requires others to
understand how things are globally configured. This is in this spirit are
babel-plugin-macros
designed. However, some things do need to be implicit,
and those kinds of babel plugins can't be turned into macros.
Thank you to @phpnode for donating the npm package
babel-plugin-macros
.
Looking to contribute? Look for the Good First Issue label.
Please file an issue for bugs, missing documentation, or unexpected behavior.
Please file an issue to suggest new features. Vote on feature requests by adding a π. This helps maintainers prioritize what to work on.
Thanks goes to these people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
MIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
Found 17/22 approved changesets -- score normalized to 7
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More