What is babel-plugin-macros?
babel-plugin-macros allows you to build simple compile-time libraries that can be used to perform various transformations on your code during the build process. It enables you to write code that gets expanded or transformed at compile-time, which can be useful for a variety of tasks such as code generation, optimizations, or embedding additional information into your code.
What are babel-plugin-macros's main functionalities?
Tagged Template Literal Transformation
Transform tagged template literals using custom logic defined in a macro. This can be used to process or manipulate strings at compile time.
`import myMacro from './macros/myMacro';\nconst result = myMacro\
\`Some template literal content\
\`;`
Function Call Transformation
Transform function calls with a macro to inline computed values or perform compile-time checks.
`import myMacro from './macros/myMacro';\nconst result = myMacro({ some: 'data' });`
Import Statement Transformation
Customize how import statements are handled, potentially inlining code or generating additional code based on the import.
`import { myFeature } from './macros/myMacro';`
Other packages similar to babel-plugin-macros
babel-plugin-preval
babel-plugin-preval allows you to pre-evaluate code at build time and can be used to import server-side data into your client-side code. It is similar to babel-plugin-macros in that it performs transformations at compile time, but it is focused on evaluating code and inlining the results.
babel-plugin-codegen
babel-plugin-codegen is similar to babel-plugin-macros in that it allows you to generate code at build time. It's specifically designed for generating code snippets that can be inserted into your build based on comments or template strings.
babel-plugin-inline-react-svg
babel-plugin-inline-react-svg transforms SVG imports into React components at build time, similar to how babel-plugin-macros can transform code. It's more specialized as it focuses solely on inlining SVGs as React components.
Babel Macros
This is a Babel plugin adds support for hygienic, non-syntactic macros for JavaScript.
Note: Now requires Babel 6.
What?
Turns code like this:
DEFINE_MACRO(MAP, (input, visitor) => {
const length = input.length;
const result = new Array(length);
for (let i = 0; i < length; i++) {
result[i] = visitor(input[i], i, input);
}
return result;
});
function demo () {
return MAP([1,2,3,4], item => item + 1);
}
Into code like this:
function demo() {
var _input = [1, 2, 3, 4];
var _map = undefined;
var _length = _input.length;
var _result = new Array(_length);
for (var _i2 = 0; _i2 < _length; _i2++) {
_result[_i2] = _input[_i2] + 1;
}
_map = _result;
return _map;
}
Also you may use more native syntax to define macro
macro: function MACRO() {
console.log('MACRO called');
}
MACRO();
Macro calls can also be chained, so if you declare a new macro called, e.g. FILTER
:
DEFINE_MACRO(FILTER, (input, visitor) => {
const filtered = [];
for (let i = 0; i < input.length; i++) {
if (visitor(input[i], i, input)) {
filtered.push(input[i]);
}
}
return filtered;
});
You'll then be able to combine the two macros in one statement, without needing to nest:
MAP([1, 2, 3], item => item + 1).FILTER(item => item < 4).length;
Why?
Because macros are incredibly useful! Also because they make it easy to write extremely fast code without sacrificing readability. When compiled with closure elimination the above code is 10x faster than native Array.prototype.map()
.
Note: This is super experimental, use at your own risk!
Todo List
Installation
First, install via npm.
npm install --save-dev babel-plugin-macros
Then, in your babel configuration (usually in your .babelrc
file), add "macros"
to your list of plugins:
{
"plugins": ["macros"]
}
ChangeLog
- 0.0.1 base implementation
- 1.0.0 update for babel@6 API
- 1.0.1 fix npm package
- 1.0.2 fix crash when missed some arguments
- 1.0.3 fix behavior of same-name macros in different scopes.
before this change same-name macros are re-declared.
now macros in different scopes - are different macros. - 1.0.4 fix wrong scoping for equal-named macros
fix combining more than 2 macros - 1.0.5 optimization. Replacement
traverse
to get
to find the desired node - 1.0.6 honest exception for infinite recursion in macros
- 1.0.7 add checking types in runtime
- 1.0.8 block using
this
and arguments
in macros - 1.0.9 fix multiple
return
& return
without value - 1.0.10 optimize performance of compile. x10 at tests
- 1.0.11 depedency for lodash no more needed
- 1.0.12 new syntax for define macro, using label
- 1.0.13 Implement function inlining for macro arguments
- 1.0.14 fix for different inlined function types in macro-argument
License
Published by codemix under a permissive MIT License, see LICENSE.md.