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

Allow macro config to come from plugin options in babel config. #113

Merged
merged 5 commits into from
May 30, 2019

Conversation

conartist6
Copy link
Collaborator

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.

* [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)
Copy link
Collaborator Author

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?

@conartist6
Copy link
Collaborator Author

Never mind I figured out how to write the tests. Should be ready to go.

@conartist6
Copy link
Collaborator Author

Whoops, forgot to git add a folder. Should really be ready to go.

src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated
callOptions = options[configName]
}
}

try {
// lazy-loading it here to avoid perf issues of loading it up front.
Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is done now.

@conartist6
Copy link
Collaborator Author

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.
I also found that failing objectContaining assertions where the actual object contains babel were printing output so long that they were obscuring useful output by pushing it completely out of my 1000 line terminal scrollback.

src/index.js Outdated Show resolved Hide resolved

```javascript
// babel-plugin-macros.config.js
// .babel.config.js
Copy link
Owner

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

other/docs/author.md Outdated Show resolved Hide resolved
other/docs/author.md Outdated Show resolved Hide resolved
Copy link
Collaborator Author

@conartist6 conartist6 left a 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.

Copy link
Owner

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

Awesome. Thanks for this :)

@kentcdodds kentcdodds merged commit 796c189 into kentcdodds:master May 30, 2019
@kentcdodds
Copy link
Owner

@all-contributors please add @conartist6 for code, tests, and docs

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @conartist6! 🎉

@kentcdodds
Copy link
Owner

🎉 This PR is included in version 2.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@conartist6
Copy link
Collaborator Author

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.

@conartist6 conartist6 deleted the babel-config-options branch May 30, 2019 18:28
@@ -88,6 +108,7 @@ function macrosPlugin(
babel,
interopRequire,
resolvePath,
options,
Copy link
Contributor

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?

const result = applyMacros({
path: call,
imports,
source,
state,
babel,
interopRequire,
resolvePath,
})

Copy link
Owner

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!

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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.

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

Successfully merging this pull request may close these issues.

Feature: Support configuration passed through the macro plugin
3 participants