{"id":13740127,"url":"https://github.com/codemix/babel-plugin-macros","last_synced_at":"2025-04-13T04:09:23.739Z","repository":{"id":32036830,"uuid":"35608249","full_name":"codemix/babel-plugin-macros","owner":"codemix","description":"Hygienic, non-syntactic macros for JavaScript via a Babel plugin.","archived":false,"fork":false,"pushed_at":"2016-03-12T17:39:57.000Z","size":93,"stargazers_count":262,"open_issues_count":0,"forks_count":15,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-01-06T20:12:23.195Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codemix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-14T11:46:11.000Z","updated_at":"2024-11-02T03:44:26.000Z","dependencies_parsed_at":"2022-08-24T14:23:03.132Z","dependency_job_id":null,"html_url":"https://github.com/codemix/babel-plugin-macros","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fbabel-plugin-macros","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fbabel-plugin-macros/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fbabel-plugin-macros/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fbabel-plugin-macros/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codemix","download_url":"https://codeload.github.com/codemix/babel-plugin-macros/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248661704,"owners_count":21141450,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T04:00:43.007Z","updated_at":"2025-04-13T04:09:23.708Z","avatar_url":"https://github.com/codemix.png","language":"JavaScript","funding_links":[],"categories":["Plugins"],"sub_categories":["Alternative Programming Paradigms"],"readme":"# Babel Macros\n\nThis is a [Babel](https://babeljs.io/) plugin adds support for hygienic, non-syntactic macros for JavaScript.\n\n[![Build Status](https://travis-ci.org/codemix/babel-plugin-macros.svg)](https://travis-ci.org/codemix/babel-plugin-macros)\n\n[![NPM](https://nodei.co/npm-dl/babel-plugin-macros.png?months=1)](https://nodei.co/npm/babel-plugin-macros/)\n\n\u003e Note: Now requires Babel 6.\n\n# What?\n\nTurns code like this:\n```js\nDEFINE_MACRO(MAP, (input, visitor) =\u003e {\n  const length = input.length;\n  const result = new Array(length);\n  for (let i = 0; i \u003c length; i++) {\n    result[i] = visitor(input[i], i, input);\n  }\n  return result;\n});\n\nfunction demo () {\n  return MAP([1,2,3,4], item =\u003e item + 1);\n}\n```\nInto code like this:\n```js\nfunction demo() {\n  var _input = [1, 2, 3, 4];\n  var _map = undefined;\n\n  var _length = _input.length;\n  var _result = new Array(_length);\n  for (var _i2 = 0; _i2 \u003c _length; _i2++) {\n    _result[_i2] = _input[_i2] + 1;\n  }\n  _map = _result;\n\n  return _map;\n}\n```\n\nAlso you may use more native syntax to define macro\n```js\nmacro: function MACRO() {\n  console.log('MACRO called');\n}\nMACRO();\n```\n\nMacro calls can also be chained, so if you declare a new macro called, e.g. `FILTER`:\n\n```js\nDEFINE_MACRO(FILTER, (input, visitor) =\u003e {\n  const filtered = [];\n  for (let i = 0; i \u003c input.length; i++) {\n    if (visitor(input[i], i, input)) {\n      filtered.push(input[i]);\n    }\n  }\n  return filtered;\n});\n```\n\nYou'll then be able to combine the two macros in one statement, without needing to nest:\n```js\nMAP([1, 2, 3], item =\u003e item + 1).FILTER(item =\u003e item \u003c 4).length; // 2\n```\n\n# Why?\n\nBecause macros are incredibly useful! Also because they make it easy to write extremely fast code without sacrificing readability. When compiled with [closure elimination](https://github.com/codemix/babel-plugin-closure-elimination) the above code is [10x faster](http://jsperf.com/macros-vs-functions) than native `Array.prototype.map()`.\n\n# Note: This is super experimental, use at your own risk!\n\n# Todo List\n\n- [ ] Allow macros in macro definitions (currently causes infinite loop)\n- [ ] Allow macros to be imported and exported across files.\n- [ ] Add `DEFINE_TRANSFORM` which is similar to `DEFINE_MACRO` but allows direct AST manipulation, not merely replacement.\n\n# Installation\n\nFirst, install via [npm](https://npmjs.org/package/babel-plugin-macros).\n```sh\nnpm install --save-dev babel-plugin-macros\n```\nThen, in your babel configuration (usually in your `.babelrc` file), add `\"macros\"` to your list of plugins:\n```json\n{\n  \"plugins\": [\"macros\"]\n}\n```\n\n# ChangeLog\n- **0.0.1** base implementation\n- **1.0.0** update for babel@6 API\n- **1.0.1** fix npm package\n- **1.0.2** fix crash when missed some arguments\n- **1.0.3** fix behavior of same-name macros in different scopes.  \n   before this change same-name macros are re-declared.  \n   now macros in different scopes - are different macros.\n- **1.0.4** fix wrong scoping for equal-named macros  \nfix combining more than 2 macros\n- **1.0.5** optimization. Replacement `traverse` to `get` to find the desired node\n- **1.0.6** honest exception for infinite recursion in macros\n- **1.0.7** add checking types in runtime\n- **1.0.8** block using `this` and `arguments` in macros\n- **1.0.9** fix multiple `return` \u0026 `return` without value\n- **1.0.10** optimize performance of compile. x10 at tests \n- **1.0.11** depedency for lodash no more needed \n- **1.0.12** new syntax for define macro, using label \n- **1.0.13** Implement function inlining for macro arguments \n- **1.0.14** fix for different inlined function types in macro-argument \n- **1.0.15** fix crash for case if babel-core use same babel-traverse, but in own sub-directory (without npm dedupe) \n\n# License\n\nPublished by [codemix](http://codemix.com/) under a permissive MIT License, see [LICENSE.md](./LICENSE.md).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Fbabel-plugin-macros","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodemix%2Fbabel-plugin-macros","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Fbabel-plugin-macros/lists"}