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

Proposal: Allow macros to be defined in `.macro.mjs` files #188

Closed leafac closed 1 year ago

leafac commented 1 year ago

Currently babel-plugin-macros requires macros to be defined in files that match /[./]macro(\.c?js)?$/. What if that was /[./]macro(\.[cm]?js)?$/?

conartist6 commented 1 year ago

.mjs files can't be imported without an await, which is a problem since babel visitors are sync.

conartist6 commented 1 year ago

I'm working on a project called cst-tokens, a native-JS virtual machine for syntax and semantics that is heavily inspired by the babel and eslint internals but uses a generator-based visitor architecture. I expect it to be able to solve problems like this.

leafac commented 1 year ago

@conartist6 Thank you for the information.

Would it be feasible for babel-plugin-macros itself to work around this with the trick that it recommends to macro authors?

conartist6 commented 1 year ago

I don't think so? Kind of the point of a macro is that it exposes babel's APIs, but those objects and functions wouldn't be available in a child process.

leafac commented 1 year ago

@conartist6

I don't think so? Kind of the point of a macro is that it exposes babel's APIs, but those objects and functions wouldn't be available in a child process.

Hmmm, yeah, right. I suppose it could still work by serializing the whole AST to JSON. But that sounds very clunky. It’s too bad, really, given that the ecosystem is moving from CommonJS to ESM… 🤷

Thank you again for the information.

I’m closing this issue for now. If anything changes or someone comes up with a better idea we can reopen it.

conartist6 commented 1 year ago

Sounds good. To reiterate: I am fairly confident that I have the solution to this and other critical problems with babel. I am working full time on developing that solution.

timofei-iatsenko commented 1 month ago

Want to breathe a new life into this issue. https://github.com/babel/babel/issues/16860