kentcdodds / babel-plugin-macros

🎣 Allows you to build simple compile-time libraries
https://npm.im/babel-plugin-macros
MIT License
2.62k stars 135 forks source link

Add SWC Plugin #144

Closed heygrady closed 2 years ago

heygrady commented 4 years ago

https://swc-project.github.io/docs/usage-plugin

Proposal: create an swc-plugin-macros that is similar to babel-plugin-macros

SWC is increasing in popularity. It is aiming to be a drop-in replacement for babel with some significant speed improvements. It already has a webpack loader.

There is some early interest in how it might integrate with create-react-app (https://github.com/facebook/create-react-app/issues/8060). Looking at which babel plugins create-react-app uses it seems like most of those things are supported by swc (with only a few exceptions).

SWC appears to be missing support for:

  1. babel-plugin-macros (important for some of my projects)
  2. babel-plugin-transform-react-remove-prop-types (less important in TypeScript projects)
  3. babel-plugin-transform-flow-strip-types (generally SWC has no Flow support 🤷🏻‍♂️)

Generally everything else babel does for me is handled by swc preset-env (or isn't particularly vital to my projects).

kentcdodds commented 4 years ago

Hi @heygrady!

That sounds cool. I don't have the bandwidth to work on it myself, but if you want to do it then you're welcome to! 👍

c-rick commented 2 years ago

try this code, it work for me next-twin

conartist6 commented 2 years ago

What language are SWC plugins written in? Rust?

conartist6 commented 2 years ago

Indeed, they are written in Rust. So macros would have to be themselves written in Rust. This may be possible, but it won't share any code or architecture with babel-plugin-macros and would require maintainers with a completely different skill set. I'm going to close this issue as it will never be actionable here. Best of luck to those looking for this functionality.

renchap commented 2 years ago

Some update on the SWC front here: https://github.com/swc-project/swc/issues/3167

TL;DR: they are integrating WASM plugin support in SWC. This means you can write your plugins in any language that can be compiled to WASM, for example Typescript with https://www.assemblyscript.org

Would this make it easier to have a macros plugin for SWC ?

conartist6 commented 2 years ago

That might make it possible! May I suggest that you make a repo or chat or something for people who are interested in using or building that functionality to talk and collaborate, and you should link to it here.

renchap commented 2 years ago

The plugin support in SWC is still an experimental work in progress. If it lives up to expectations and goes into a release, then I will see what I can do!

But I suspect that it will need to be a separate project from this one, as SWC uses a different AST than babel, so macros will probably need to be rewritten entirely

conartist6 commented 2 years ago

I agree that it would be a separate project that would not be intercompatible with this one. Hence the suggestion to make a new space for interested contributors.

matthewmatician commented 2 years ago

Maybe this is a noob question, but wouldn't this PR have made this possible?

conartist6 commented 2 years ago

@matthewmatician I mean you could always do a two-step process. Parse with babel, transpile macros, send the program to SWC, then have SWC do the rest of the transpilation.

The only question is: how you would send the program to SWC? Obviously you could print it from babel and parse in SWC. But perhaps there's a more efficient way? I can't say I understand the PR you linked well enough to know quite what its API would be. If it can do a copy in a way that's faster than a parse and print, then perhaps the perf numbers would still be favorable at the end of the day.

arlyon commented 2 years ago

I realise this isn't directly related, however I am working on stailwc, a swc-native version of twin.macro which serves as both