What's the use case for babel-plugin-macros vs. Sweet.js?

1.7k views Asked by At

I'm curious about macros in JavaScript and am a little confused about how babel-plugin-macros and Sweet.js compare to each other. Do they both solve the same problem? Do they both fit into the same toolchain?

In particular, I'm hoping to use one of these tools with ES2015 code that will be transpiled to ES5 using Webpack and Babel, and am not sure about how either of these tools interact with transpilation.

1

There are 1 answers

0
djskinner On

Yes they solve the same general problem of 'adding' macros to JavaScript but they do approach it in different ways. For a start they each have a different syntax for defining macros. It's not clear to me whether either is following some kind of standard. Sweet.js is standalone whilst babel-plugin-macros is a Babel plugin. I would imagine it is possible to use them as part of the same toolchain and people might do just that if they prefer the Sweet.js. However, it's probably much easier to use the Babel plugin if already using Babel.

I'd be cautious about going too deep into this as there doesn't seem to be a standard for JavaScript yet. It's probably going to create headaches in terms of getting source mapping working properly, particularly if combining Sweet.js with Babel.

Having said that macros (especially with the Sweet.js syntax) looks like a very powerful enhancement to JavaScript. If only they were included as a standard in ECMAScript.

UPDATE

I haven't looked into this is any detail since my original answer but it seems like Sweet.js now uses Babel as a backend so there might be a better answer to the question to be had than this one. See http://sweetjs.org/doc/1.0/tutorial.html#_babel_backend