Macro system for JavaScript and TypeScript.
Have you ever used Rust? Rust has an impressive macro system. I'm wondering why the JavaScript world can't have one. Now you got it.
It uses swc's parser and printer, and does not make any conversions other than macors to your project by default. Of course, it can also be used as a swc plugin, so we can transform typescript and jsx.
- A new walker for macros, inspired by estree-walker
- Built-in variable tracker plugin, which makes it easier to convert variables.
- Very simple yet powerful APIs for creating various types of macros.
- Factory runtime and macros for simplify creating swc AST.
- Support automatic generation of macro declarations file. (macros.d.ts)
- Many common built-in macros.
- Support all web framework intergrations.
The project is still in the beta stage, and there are more macros under development.
Expression macros may be a very familiar macro type. It converts a CallExpression
into any other Expression.
compile time eval.
convert anything into an ast.
read env and replace with result.
convert any expression or type into string
get a span like [line, column]
get current line number
get current column number
generate unique id based on [line, column, filename]
include another js, ts
file into current script.
include a string from any path
include a json, also support include a json value with key.
compile time write to file
compile time concat string
turn code into an expression
a template macro for create macro
mark code as unImplemented, log error when compile, throw error in client side.
mark code as todo, log warning when compile, throw error in client side.
mark code as unreachable, throw error in client side.
...
LabeledMacros are very interesting feature. It even allows you to invent your own grammar. For example:
debug: {
console.log("something")
}
or creating states:
state: {
var count = 0
var name = "macro"
}
or using function decorator:
function myFunc() {
decorator: [yourFunctionDecorator]
doSomething()
}
you can even create macro with it.
macro: {
var __DEV__ = true
var add = $ExprMacro<(a: number, b: number) => number>(function(args) {
const a = this.printExpr(args[0])
const b = this.printExpr(args[1])
if (+a < 0) return args[1]
return this.parseExpr(`${a} + ${b}`)
})
}
if (__DEV__) {
const a = add(1, 2)
}
transform to
if (true) {
const a = 1 + 2
}
To use Labeled Macros, you may need some extra config for typescript and eslint.
{
"compilerOptions": {
// ...
"allowUnusedLabels": true,
},
}
module.exports = {
rules: {
// ...
"no-labels": 0,
}
}
transform code with macro plugins.
use as swc plugin.
walk an ast node or node list
print ast to code
parse code to ast
define config with types
create a macro
create a literal macro
create an expression macro
create a type macro
create a template macro
create a labeled macro
check if input is a macro plugin
Some people may argue about labeldMacros, confusing the original semantics of JavaScript. But in fact, labeledStatement
and var
are both forgotten or discarded syntax of JavaScript. So we bring something new to it. Personally, I don't think this is a bad thing.
In fact, the beginning of this project based on babel's parser and generator. When I switched to swc, I did a simple test and found that the speed was at least tripled, so it must be faster.
This project is based on the AST type of swc and is not compatible with Babel's AST. And the project focuses on transforming macros. If you need the support of something like browserlist, you need to use it with swc.
You can regard it as an enhanced version of swc with macro support, as well as better aapi. However, we do not use the .swcrc
configuration file. The json configuration is not friendly for macros, and you should define all your swc configuration in macros.config.js
or macros.config.ts
.
Copyright (c) 2023, Raven Satir