Skip to content

Commit bcda753

Browse files
committed
Add a recipe for properly configuring babel when using .babelrc.
1 parent 9288d78 commit bcda753

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

docs/recipes/babelrc.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Configuring Babel
2+
3+
There are multiple options for configuring how AVA transpiles your tests using Babel.
4+
5+
- [Specify a complete config in `package.json`](#specify-a-complete-config-in-packagejson)
6+
- [Extend existing `.babelrc` without modification](#extend-existing-babelrc-without-modification)
7+
- [Extend existing `.babelrc` with additional plugins or presets](#extend-existing-babelrc-with-additional-plugins-or-presets)
8+
- [Extend an alternate config file (i.e. not `.babelrc`)](#extend-alternate-config-file)
9+
- [Notes](#notes)
10+
11+
## Specify a complete config in `package.json`
12+
13+
The `babelrc` option defaults to `false`, meaning `.babelrc` files are not considered when transpiling tests. This means you must specify your complete config in `package.json`.
14+
15+
```json
16+
{
17+
"ava": {
18+
"babel": {
19+
"plugins": ["rewire"],
20+
"presets": ["es2015"]
21+
}
22+
}
23+
}
24+
```
25+
26+
## Extend existing `.babelrc` without modification
27+
28+
Use the `"inherit"` shortcut if you want your tests transpiled the same as your sources. This will use your `.babelrc` directly (with a few additional [internal plugins](#notes)).
29+
30+
`package.json`:
31+
32+
```json
33+
{
34+
"ava": {
35+
"babel": "inherit"
36+
}
37+
}
38+
```
39+
40+
## Extend existing `.babelrc` with additional plugins or presets
41+
42+
Set `babelrc` to `true`. This will use your `.babelrc` and extend it with any additional plugins specified.
43+
44+
`package.json`:
45+
46+
```json
47+
{
48+
"ava": {
49+
"babel": {
50+
"babelrc": true,
51+
"plugins": ["custom-plugin-name"],
52+
"presets": ["custom-preset"]
53+
}
54+
}
55+
}
56+
```
57+
58+
## Extend alternate config file.
59+
60+
61+
If, for some reason, you do not want to extend `.babelrc`, set the `extends` option to the alternate config you want to use during testing.
62+
63+
`package.json`:
64+
65+
```json
66+
{
67+
"ava": {
68+
"babel": {
69+
"extends": "./babel-test-config.json",
70+
"plugins": ["custom-plugin-name"],
71+
"presets": ["custom-preset"]
72+
}
73+
}
74+
}
75+
```
76+
77+
The above uses `babel-test-config.json` as the base config, and extends it with the custom plugins and presets specified.
78+
79+
## Notes
80+
81+
AVA *always* adds a few custom Babel plugins when transpiling your plugins. They serve a variety of functions:
82+
83+
* Enable `power-assert` support.
84+
* Rewrite require paths internal AVA dependencies like `babel-runtime` (important if you are still using `npm@2`).
85+
* Generate test metadata to determine which files should be run first (*future*).
86+
* Static analysis of dependencies for precompilation (*future*).

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,8 @@ You can also use the special `"inherit"` keyword. This makes AVA defer to the Ba
581581
}
582582
```
583583

584+
See AVA's [`.babelrc` recipe](docs/recipes/babelrc.md) for further examples and a more detailed explanation of configuration options.
585+
584586
Note that AVA will *always* apply the [`espower`](https://github.com/power-assert-js/babel-plugin-espower) and [`transform-runtime`](https://babeljs.io/docs/plugins/transform-runtime/) plugins.
585587

586588
### TypeScript support
@@ -916,6 +918,7 @@ It's the [Andromeda galaxy](https://simple.wikipedia.org/wiki/Andromeda_galaxy).
916918
- [When to use `t.plan()`](docs/recipes/when-to-use-plan.md)
917919
- [Browser testing](docs/recipes/browser-testing.md)
918920
- [TypeScript](docs/recipes/typescript.md)
921+
- [Configuring Babel](docs/recipes/babelrc.md)
919922

920923
## Support
921924

0 commit comments

Comments
 (0)