Skip to content

Commit

Permalink
docs: document using babel, jest and eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
swashata committed Apr 16, 2019
1 parent 8367cb5 commit 2569aba
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"license": "MIT",
"private": false,
"dependencies": {
"@types/shelljs": "0.8.5",
"chalk": "2.4.2",
"commander": "2.20.0",
"shelljs": "0.8.3",
"@types/shelljs": "0.8.4"
"shelljs": "0.8.3"
},
"engines": {
"node": ">=8.9.0"
Expand Down
6 changes: 3 additions & 3 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"browser-sync",
"wordpress-bundler"
],
"main": "lib/index.js",
"types": "lib/index.d.ts",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"bin": {
"wpackio-scripts": "lib/bin/index.js"
},
Expand Down Expand Up @@ -58,7 +58,7 @@
"find-up": "^3.0.0",
"gradient-string": "^1.2.0",
"handlebars": "^4.1.2",
"inquirer": "^6.2.2",
"inquirer": "^6.3.1",
"log-symbols": "^2.2.0",
"make-dir": "^3.0.0",
"mini-css-extract-plugin": "^0.6.0",
Expand Down
3 changes: 3 additions & 0 deletions site/docs/tutorials/adding-custom-babel-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ Your `babel.config.js` file may look like this.
import { getBabelPresets } from '@wpackio/scripts';

module.exports = api => {
// create cache based on NODE_ENV
api.cache.using(() => process.env.NODE_ENV);

const presetOptions = {
noDynamicImport: false,
noImportMeta: false,
Expand Down
88 changes: 88 additions & 0 deletions site/docs/tutorials/using-jest-babel.md
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.
31 changes: 25 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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" "*"
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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==
Expand Down

0 comments on commit 2569aba

Please sign in to comment.