-
Notifications
You must be signed in to change notification settings - Fork 135
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
Allow macro config to come from plugin options in babel config. #113
Allow macro config to come from plugin options in babel config. #113
Conversation
* [Writing custom Babel and ESLint plugins with ASTs](https://youtu.be/VBscbcm2Mok?list=PLV5CVI1eNcJgNqzNwcs4UKrlJdhfDjshf): A 53 minute talk by [@kentcdodds](https://twitter.com/kentcdodds) | ||
* [babel-handbook](https://github.com/thejameskyle/babel-handbook): A guided handbook on how to use Babel and how to create plugins for Babel by [@thejameskyle](https://twitter.com/thejameskyle) | ||
* [Code Transformation and Linting](https://kentcdodds.com/workshops/#code-transformation-and-linting): A workshop (recording available on Frontend Masters) with exercises of making custom Babel and ESLint plugins | ||
- [Writing custom Babel and ESLint plugins with ASTs](https://youtu.be/VBscbcm2Mok?list=PLV5CVI1eNcJgNqzNwcs4UKrlJdhfDjshf): A 53 minute talk by [@kentcdodds](https://twitter.com/kentcdodds) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know why it did this. Maybe you hadn't run the newest version of your kcd-scripts
formatter on this file?
2c3520d
to
0d5220a
Compare
Never mind I figured out how to write the tests. Should be ready to go. |
0d5220a
to
65ed764
Compare
Whoops, forgot to git add a folder. Should really be ready to go. |
src/index.js
Outdated
callOptions = options[configName] | ||
} | ||
} | ||
|
||
try { | ||
// lazy-loading it here to avoid perf issues of loading it up front. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I appreciate the comment, but maybe phobia was the right word for what influenced this decision. First, if you must synchronously load cosmiconfig
no matter what I don't see how you can gain anything useful by doing it later, unless of course it turns out that babel-plugin-macros is loaded but no macros are used (which hardly seems like the case to optimize for).
Second, in optimizing for that case, you've actually hurt performance for the common case significantly. If you read the cosmiconfig docs you'll see that its caches are internal to the explorer
object. You create a new explorer every time you process a file, so you are also doing a full config search every time you process a file, which is far more costly in the common case than what you were trying to avoid.
It's easy to refactor the code, but it seems like it'll be a bit tricker to fix the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually I suppose we can have the best of both worlds if we lazy load and cache the explorer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is done now.
1a6cae1
to
c8b46a7
Compare
I've added some more or less unrelated changes, but I've split them out into separate commits. When trying to debug the tests I discovered that most tests assertion output was silently discarded. |
c8b46a7
to
69b15a6
Compare
|
||
```javascript | ||
// babel-plugin-macros.config.js | ||
// .babel.config.js |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer babel.config.js
and babel-plugin-macros.config.js
rather than .babel.config.js
and .babel-plugin-macros.config.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure we want the plugin option to override the config version. It feels more intuitive to me that it would be the other way around.
That makes sense to me too now that I think about it since the file config can be more granular -- there could be different files for different directories. I've added that change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome. Thanks for this :)
@all-contributors please add @conartist6 for code, tests, and docs |
I've put up a pull request to add @conartist6! 🎉 |
🎉 This PR is included in version 2.6.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Awesome! Thanks! By the way, I realized that something I said was wrong. It definitely does make sense for macros to optimize for the case where it isn't being used, because one of its goals is to allow people who don't have control over their transpilation environment access to the power of transformations. That means it is a reasonable expectation that it will be added to quite a few projects that won't use it at all. |
@@ -88,6 +108,7 @@ function macrosPlugin( | |||
babel, | |||
interopRequire, | |||
resolvePath, | |||
options, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@conartist6 @kentcdodds
Shouldn't the options
also be passed in another applyMacros
invocation?
babel-plugin-macros/src/index.js
Lines 146 to 154 in d11217e
const result = applyMacros({ | |
path: call, | |
imports, | |
source, | |
state, | |
babel, | |
interopRequire, | |
resolvePath, | |
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you open a new issue and/or a pull request with a failing test? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@InvictusMB You are right. It would affect macros meant to be configured via plugin options which are imported with const { sym } = require('path/to/macro')
. Nice catch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have created #120 with the repro and fix for this issue.
Fixes #112
I'm missing test coverage, but I'm also not familiar with the way this test environment is set up. Some tips on how to test this well would be appreciated.