-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: document using babel, jest and eslint
- Loading branch information
Showing
5 changed files
with
121 additions
and
11 deletions.
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
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
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,88 @@ | ||
--- | ||
title: Using Jest and ESlint with babel | ||
order: 4 | ||
shortTitle: Use Jest & ESLint | ||
--- | ||
|
||
If you are using [jest](https://jestjs.io) to test your project, and [ESLint](https://eslint.org) | ||
for linting then you might want to add a `babel.config.js` file for uniform | ||
babel rules. | ||
|
||
## Using Same rule without customization | ||
|
||
By default, `@wpackio/scripts` ignores any `babel.config.js` and `.babelrc` files. | ||
So even if you do put such files in your project root, those will be picked up | ||
by Jest or ESLint, but not by `@wpackio/scripts`. In small use cases, this is | ||
perhaps the intended feature. In this case, create a `babel.config.js` file | ||
in the root of your project and put the following code. | ||
|
||
**babel.config.js** | ||
|
||
```js | ||
import { | ||
getBabelPresets, | ||
getDefaultBabelPresetOptions, | ||
} from '@wpackio/scripts'; | ||
|
||
module.exports = api => { | ||
// create cache based on NODE_ENV | ||
api.cache.using(() => process.env.NODE_ENV); | ||
|
||
// make use of some configuration | ||
const isDev = api.env('development'); | ||
const isTest = api.env('test'); | ||
const hasReact = true; | ||
|
||
const presetOptions = getDefaultBabelPresetOptions(hasReact, isDev); | ||
|
||
// If test, then change modules and targets | ||
// since jest works in node and understands only cjs modules | ||
if (isTest) { | ||
presetOptions.presetEnv.targets = { | ||
node: 'current', | ||
}; | ||
presetOptions.presetEnv.modules = 'commonjs'; | ||
} | ||
|
||
// Now get the presets and create babel config | ||
// assuming we are using typescript | ||
// otherwise don't pass anything to the second parameter of getBabelPresets | ||
// or pass 'flow` for flowtype | ||
const babelConfig = { | ||
presets: getBabelPresets(presetOptions, 'typescript'), | ||
}; | ||
|
||
// return it | ||
return babelConfig; | ||
}; | ||
``` | ||
|
||
The above will create same babel rule for jest and eslint as used internally by | ||
`@wpackio/scripts`. | ||
|
||
## Override `@wpackio/scripts` rules | ||
|
||
If you want to completely remove any babel rule set by `@wpackio/scripts` | ||
and would only want to use one single `babel.config.js` instead, do the followings. | ||
|
||
#### Instruct `@wpackio/scripts` to not pass any rule to babel-loader | ||
|
||
Edit your `wpackio.project.js` file and put | ||
|
||
```js | ||
module.exports = { | ||
// config | ||
// this tells wpackio-scripts to use your own babel.config.js file | ||
useBabelConfig: true, | ||
}; | ||
``` | ||
|
||
Now this will remove all rules from `babel-loader` for both javascript and | ||
typescript files. So it will pick your own `babel.config.js` file. | ||
|
||
Now go ahead and write your custom `babel.config.js` file making sure it matches | ||
your project requirements. From now on, all tools including Jest, ESLint and | ||
`@wpackio/scripts` will follow the same config. | ||
|
||
For more information, do read the [custom babel configuration](/tutorials/adding-custom-babel-config/) | ||
tutorial. |
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 |
---|---|---|
|
@@ -1978,10 +1978,10 @@ | |
"@types/express-serve-static-core" "*" | ||
"@types/mime" "*" | ||
|
||
"@types/[email protected].4": | ||
version "0.8.4" | ||
resolved "https://registry.yarnpkg.com/@types/shelljs/-/shelljs-0.8.4.tgz#b903e41ad5e5195b7d5e34d3cd33c94bdbd07895" | ||
integrity sha512-UNACC6scKFVljWEvO3rHBkbbKXu3QkKPBOMCisxI7au9cnFK7tjOGPsKh5OjedAPLmtsKSarmk+YeehKTQSKtg== | ||
"@types/[email protected].5": | ||
version "0.8.5" | ||
resolved "https://registry.yarnpkg.com/@types/shelljs/-/shelljs-0.8.5.tgz#1e507b2f6d1f893269bd3e851ec24419ef9beeea" | ||
integrity sha512-bZgjwIWu9gHCjirKJoOlLzGi5N0QgZ5t7EXEuoqyWCHTuSddURXo3FOBYDyRPNOWzZ6NbkLvZnVkn483Y/tvcQ== | ||
dependencies: | ||
"@types/glob" "*" | ||
"@types/node" "*" | ||
|
@@ -2051,7 +2051,7 @@ | |
"@types/connect" "*" | ||
"@types/webpack" "*" | ||
|
||
"@types/webpack@*", "@types/webpack@^4.4.24", "@types/webpack@^4.4.27": | ||
"@types/webpack@*", "@types/webpack@^4.4.27": | ||
version "4.4.27" | ||
resolved "https://registry.yarnpkg.com/@types/webpack/-/webpack-4.4.27.tgz#8bb9429185977a6b3b9e6e6132f561066aa7e7c2" | ||
integrity sha512-xSll/4UXnLQ0xjdAoTRIFxA6NPC2abJ8nHxRH6SqTymHrfGCc8er7qH0npwCP8q3VFoJh2Hjz1wH8oTjwx9/jQ== | ||
|
@@ -6750,6 +6750,25 @@ inquirer@^6.2.2: | |
strip-ansi "^5.0.0" | ||
through "^2.3.6" | ||
|
||
inquirer@^6.3.1: | ||
version "6.3.1" | ||
resolved "https://registry.yarnpkg.com/inquirer/-/inquirer-6.3.1.tgz#7a413b5e7950811013a3db491c61d1f3b776e8e7" | ||
integrity sha512-MmL624rfkFt4TG9y/Jvmt8vdmOo836U7Y0Hxr2aFk3RelZEGX4Igk0KabWrcaaZaTv9uzglOqWh1Vly+FAWAXA== | ||
dependencies: | ||
ansi-escapes "^3.2.0" | ||
chalk "^2.4.2" | ||
cli-cursor "^2.1.0" | ||
cli-width "^2.0.0" | ||
external-editor "^3.0.3" | ||
figures "^2.0.0" | ||
lodash "^4.17.11" | ||
mute-stream "0.0.7" | ||
run-async "^2.2.0" | ||
rxjs "^6.4.0" | ||
string-width "^2.1.0" | ||
strip-ansi "^5.1.0" | ||
through "^2.3.6" | ||
|
||
interpret@^1.0.0: | ||
version "1.1.0" | ||
resolved "https://registry.yarnpkg.com/interpret/-/interpret-1.1.0.tgz#7ed1b1410c6a0e0f78cf95d3b8440c63f78b8614" | ||
|
@@ -11718,7 +11737,7 @@ strip-ansi@^5.0.0: | |
dependencies: | ||
ansi-regex "^4.0.0" | ||
|
||
strip-ansi@^5.2.0: | ||
strip-ansi@^5.1.0, strip-ansi@^5.2.0: | ||
version "5.2.0" | ||
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-5.2.0.tgz#8c9a536feb6afc962bdfa5b104a5091c1ad9c0ae" | ||
integrity sha512-DuRs1gKbBqsMKIZlrffwlug8MHkcnpjs5VPmL1PAh+mA30U0DTotfDZ0d2UUsXpPmPmMMJ6W773MaA3J+lbiWA== | ||
|