-
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
Merged
kentcdodds
merged 5 commits into
kentcdodds:master
from
conartist6:babel-config-options
May 30, 2019
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
65ed764
Allow macro config to come from plugin options in babel config.
conartist6 f5f8958
Respond to review feedback
conartist6 071eafe
Ensure failing test output is readable
conartist6 69b15a6
Ensure cosmiconfig conf is cached
conartist6 d11217e
Prioritize file config over plugin options config
conartist6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,9 @@ | |
|
||
Is this your first time working with ASTs? Here are some resources: | ||
|
||
* [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) | ||
- [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 a macro | ||
|
||
|
@@ -164,43 +164,53 @@ This is a string used as import declaration's source - i.e. `'./my.macro'`. | |
|
||
#### config (EXPERIMENTAL!) | ||
|
||
There is an experimental feature that allows users to configure your macro. We | ||
use [`cosmiconfig`][cosmiconfig] to read a `babel-plugin-macros` configuration which | ||
There is an experimental feature that allows users to configure your macro. | ||
|
||
To specify that your plugin is configurable, you pass a `configName` to `createMacro`. | ||
|
||
A configuration is created from data combined from two sources: | ||
We use [`cosmiconfig`][cosmiconfig] to read a `babel-plugin-macros` configuration which | ||
can be located in any of the following files up the directories from the | ||
importing file: | ||
|
||
* `.babel-plugin-macrosrc` | ||
* `.babel-plugin-macrosrc.json` | ||
* `.babel-plugin-macrosrc.yaml` | ||
* `.babel-plugin-macrosrc.yml` | ||
* `.babel-plugin-macrosrc.js` | ||
* `babel-plugin-macros.config.js` | ||
* `babelMacros` in `package.json` | ||
- `.babel-plugin-macrosrc` | ||
- `.babel-plugin-macrosrc.json` | ||
- `.babel-plugin-macrosrc.yaml` | ||
- `.babel-plugin-macrosrc.yml` | ||
- `.babel-plugin-macrosrc.js` | ||
- `babel-plugin-macros.config.js` | ||
- `babelMacros` in `package.json` | ||
|
||
To specify that your plugin is configurable, you pass a `configName` to | ||
`createMacro`: | ||
The content of the config will be merged with the content of the babel macros plugin | ||
options. Config options take priority. | ||
|
||
All together specifying and using the config might look like this: | ||
|
||
```javascript | ||
const {createMacro} = require('babel-plugin-macros') | ||
const configName = 'taggedTranslations' | ||
module.exports = createMacro(taggedTranslationsMacro, {configName}) | ||
function taggedTranslationsMacro({references, state, babel, config}) { | ||
// config would be taggedTranslations portion of the config as loaded from `cosmiconfig` | ||
// .babel-plugin-macros.config.js | ||
module.exports = { | ||
taggedTranslations: { locale: 'en_US' } | ||
} | ||
``` | ||
|
||
Then to configure this, users would do something like this: | ||
|
||
```javascript | ||
// babel-plugin-macros.config.js | ||
// .babel.config.js | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer |
||
module.exports = { | ||
taggedTranslations: { | ||
someConfig: {}, | ||
}, | ||
plugins: [ | ||
['macros': { | ||
taggedTranslations: { locale: 'en_GB' } | ||
}] | ||
] | ||
} | ||
|
||
// taggedTranslations.macro.js | ||
const {createMacro} = require('babel-plugin-macros') | ||
module.exports = createMacro(taggedTranslationsMacro, {configName: 'taggedTranslations'}) | ||
function taggedTranslationsMacro({references, state, babel, config}) { | ||
const { locale = 'en' } = config; | ||
} | ||
``` | ||
|
||
And the `config` object you would receive would be: `{someConfig: {}}`. | ||
Note that in the above example if both files were sepcified the final locale value would | ||
be `en_US`, since that is the value in the plugin config file. | ||
|
||
### Keeping imports | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module.exports = { | ||
configurableMacro: { | ||
fileConfig: true, | ||
someConfig: true, | ||
}, | ||
} |
3 changes: 3 additions & 0 deletions
3
src/__tests__/fixtures/primitive-config/babel-plugin-macros.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
configurableMacro: 4, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import configured from './configurable.macro' | ||
|
||
// eslint-disable-next-line babel/no-unused-expressions | ||
configured`stuff` |
10 changes: 10 additions & 0 deletions
10
src/__tests__/fixtures/primitive-config/configurable.macro.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const {createMacro} = require('../../../../') | ||
|
||
const configName = 'configurableMacro' | ||
const realMacro = jest.fn() | ||
module.exports = createMacro(realMacro, {configName}) | ||
// for testing purposes only | ||
Object.assign(module.exports, { | ||
realMacro, | ||
configName, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?