Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
leafac opened this issue Dec 28, 2022 · 13 comments
Open

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

leafac opened this issue Dec 28, 2022 · 13 comments

Comments

@leafac
Copy link

leafac commented Dec 28, 2022

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

@conartist6
Copy link
Collaborator

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

@conartist6
Copy link
Collaborator

conartist6 commented Dec 28, 2022

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
Copy link
Author

leafac commented Dec 28, 2022

@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
Copy link
Collaborator

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
Copy link
Author

leafac commented Jan 1, 2023

@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.

@leafac leafac closed this as completed Jan 1, 2023
@conartist6 conartist6 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 1, 2023
@conartist6
Copy link
Collaborator

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
Copy link

Want to breathe a new life into this issue. babel/babel#16860

@timofei-iatsenko
Copy link

Update, async pre/post is merged to the babel-core and available from 7.26.0 i'm going to prepare PoC with ESM macros support

@conartist6
Copy link
Collaborator

What will it look like when plugin-macros is used with an older version of Babel that lacks that feature?

@timofei-iatsenko
Copy link

Until user will not use any mjs macro it will work, so no breaking changes. If user tries to use such macro and use older babel version, babel itself will throw a useful error message about the need to update to the latest version.

@conartist6 conartist6 reopened this Nov 13, 2024
@conartist6
Copy link
Collaborator

Reopening the issue since we have a way forward now.

@timofei-iatsenko
Copy link

I actually played with that a little bit and unfortunately i could not process without modernizing the tools in this repo. The old tools simply not work with ESM and brings a lot of complications.

I could do first modernizing the tooling in the separate PRs, but want to know what you are thinking about that first.

Also implement this support for the syntax ESM syntax is quite easy, since it's static and could be only in the Program Statements, but for the require() macro support it's a bit more tricky because they could be anywhere in the AST.

@conartist6 feel free to contact me directly in discord or email, so we can discuss a plan to move it forward.

@conartist6
Copy link
Collaborator

Ah yeah. I'm already so heavily invested in making the next generation of tools that at that point I'd rather spend my time on that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants