Babel 中文文档 Babel 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • 指南
    • Babel 是什么?
    • 使用指南
    • 配置 Babel
    • 学习 ES2015
    • 升级到 Babel 7
  • 配置

  • 预设环境

  • 杂项

  • 集成

  • 工具

  • 辅助

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:

  1. ``` js
  2. DEFINE_MACRO(MAP, (input, visitor) => {
  3.   const length = input.length;
  4.   const result = new Array(length);
  5.   for (let i = 0; i < length; i++) {
  6.     result[i] = visitor(input[i], i, input);
  7.   }
  8.   return result;
  9. });

  10. function demo () {
  11.   return MAP([1,2,3,4], item => item + 1);
  12. }
  13. ```

Into code like this:

  1. ``` js
  2. function demo() {
  3.   var _input = [1, 2, 3, 4];
  4.   var _map = undefined;

  5.   var _length = _input.length;
  6.   var _result = new Array(_length);
  7.   for (var _i2 = 0; _i2 < _length; _i2++) {
  8.     _result[_i2] = _input[_i2] + 1;
  9.   }
  10.   _map = _result;

  11.   return _map;
  12. }
  13. ```

Also you may use more native syntax to define macro

  1. ``` js
  2. macro: function MACRO() {
  3.   console.log('MACRO called');
  4. }
  5. MACRO();
  6. ```

Macro calls can also be chained, so if you declare a new macro called, e.g. FILTER :

  1. ``` js
  2. DEFINE_MACRO(FILTER, (input, visitor) => {
  3.   const filtered = [];
  4.   for (let i = 0; i < input.length; i++) {
  5.     if (visitor(input[i], i, input)) {
  6.       filtered.push(input[i]);
  7.     }
  8.   }
  9.   return filtered;
  10. });
  11. ```

You'll then be able to combine the two macros in one statement, without needing to nest:

  1. ``` js
  2. MAP([1, 2, 3], item => item + 1).FILTER(item => item < 4).length; // 2
  3. ```

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


Allow macros in macro definitions (currently causes infinite loop)
Allow macros to be imported and exported across files.
Add DEFINE_TRANSFORM which is similar to DEFINE_MACRO but allows direct AST manipulation, not merely replacement.

Installation


First, install via npm.

  1. ``` shell
  2. npm install --save-dev babel-plugin-macros
  3. ```

Then, in your babel configuration (usually in your .babelrc file), add "macros" to your list of plugins:

  1. ``` json
  2. {
  3.   "plugins": ["macros"]
  4. }
  5. ```

ChangeLog


0.0.1base implementation
1.0.0update for babel@6 API
1.0.1fix npm package
1.0.2fix crash when missed some arguments
1.0.3fix 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.4fix wrong scoping for equal-named macrosfix combining more than 2 macros
1.0.5optimization. Replacement traverse to get to find the desired node
1.0.6honest exception for infinite recursion in macros
1.0.7add checking types in runtime
1.0.8block using this and arguments in macros
1.0.9fix multiple return & return without value
1.0.10optimize performance of compile. x10 at tests
1.0.11depedency for lodash no more needed
1.0.12new syntax for define macro, using label
1.0.13Implement function inlining for macro arguments
1.0.14fix for different inlined function types in macro-argument
1.0.15fix crash for case if babel-core use same babel-traverse, but in own sub-directory (without npm dedupe)

License


Published by codemix under a permissive MIT License, see LICENSE.md.
Last Updated: 2023-09-03 17:10:52