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

Working babel-plugin-macros transformer #1

Closed
wants to merge 1 commit into from

Conversation

FWeinb
Copy link
Owner

@FWeinb FWeinb commented Mar 2, 2018

This transformer is based in babel7 and makes it possible to develop and show off babel-plugin-macros. Had to modify the source of babel-plugin-macros quite heavily to get this to work. That is the reason this is not an actual PR against astexplorer, this is a proof of concept and I would like to use this as a foundation to show of what would be possible if the changes I made to babel-plugin-macros would get merged into the main project. I feel like there should be a better way to handle this from within webpack.

I will add some comments to the relevant changes to explain what needs to change in babel plugin macro.
You can give this a try https://astexplorer.dev.weinberg.me/. (Select Transform -> babel-macros)


function interopRequire(path) {
// eslint-disable-next-line import/no-dynamic-require
const o = _require ? _require(path) : require(path);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional use the passed in require to overwrite the default behaviour of require

state,
babel,
interopRequire,
})
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass the interopRequire down

if (isRelative) {
requirePath = p.join(p.dirname(getFullFilename(filename)), source)
}
const macro = interopRequire(requirePath)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use it like before

generatorOpts: {
generator: recast.print,
},
plugins: [macro(babel, {require: () => transform})],
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass in a custom require that always evaluates to the custom macro.

Copy link

@kentcdodds kentcdodds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is absolutely amazing 😮

@@ -0,0 +1,263 @@
/**
* Just for the proof of concept.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So would this ultimately be replaced with a require to babel-plugin-macros itself in the future? I'm assuming we'd need to make the same changes to babel-plugin-macros you made here. What changes would those be?

Copy link
Owner Author

@FWeinb FWeinb Mar 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly! If you are okay with these changes I will prepare a PR for babel-plugin-macros

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm.... It looks like the only difference is moving the interopRequire function. Why was that necessary? Does it clash with the transpiled one? If so, could we just rename ours to internalInteropRequire?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's all it takes then I'd welcome a PR for it 👍

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a little bit more to it. I need to pass in my own fake require to babel-plugin-macros because I did not find a way to hook into the require and change it using webpack. I will make the PR and we can discuss it there.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it. If that's what it takes to get this working then I'm fine with it. Maybe it could use defaults and be:

function macrosPlugin(babel, { require: _require = require }) {
  function interopRequire(path) {
    const o = _require(path);
    return o && o.__esModule && o.default ? o.default : o
  }
  // ... etc
}

Looking forward to it!

Copy link
Owner Author

@FWeinb FWeinb Mar 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had exactly the same idea just now. PR will be there in just a minute.
Here it is kentcdodds/babel-plugin-macros#60

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged!

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

Successfully merging this pull request may close these issues.

2 participants