Skip to content

Commit

Permalink
Use new Babel presets
Browse files Browse the repository at this point in the history
Fixes #1193, #1195.

Closes #947 and #1089, since babel-runtime is removed.
  • Loading branch information
novemberborn committed Jan 20, 2017
1 parent 95ff309 commit ad5122d
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 135 deletions.
16 changes: 8 additions & 8 deletions docs/recipes/babelrc.md
Expand Up @@ -14,18 +14,18 @@ There are multiple options for configuring how AVA transpiles your tests using B

## AVA's default transpiler behavior

By default, AVA transpiles your tests (and only your tests) using the [`es2015-node4`](https://github.com/jbach/babel-preset-es2015-node4) on Node.js 4 or [`node6`](https://github.com/salakar/babel-preset-node6) on Node.js 6 and higher, and [`stage-2`](http://babeljs.io/docs/plugins/preset-stage-2/) Babel presets. This is a great option for small modules where you do not desire a build step to transpile your source before deploying to `npm`.
By default, AVA transpiles your tests and helper files using the [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) Babel preset. This is a great option for small modules where you do not desire a build step to transpile your source before deploying to `npm`.

## Customizing how AVA transpiles your tests

You can override the default Babel configuration AVA uses for test transpilation in `package.json`. For example, the configuration below adds the Babel `rewire` plugin, and opts to only use the Babel [`stage-3`](http://babeljs.io/docs/plugins/preset-stage-3/) preset (which is a subset of [`stage-2`](http://babeljs.io/docs/plugins/preset-stage-2/)).
You can override the default Babel configuration AVA uses for test transpilation in `package.json`. For example, the configuration below adds the Babel `rewire` plugin, and adds the Babel [`stage-3`](http://babeljs.io/docs/plugins/preset-stage-3/) preset.

```json
{
"ava": {
"babel": {
"plugins": ["rewire"],
"presets": ["es2015-node4", "stage-3"]
"presets": ["@ava/stage-4", "stage-3"]
}
}
}
Expand All @@ -43,7 +43,7 @@ To transpile your sources, you will need to define a [`babel config` ](http://ba
"require": ["babel-register"]
},
"babel": {
"presets": ["es2015-node4"]
"presets": ["@ava/stage-4"]
}
}
```
Expand All @@ -63,12 +63,12 @@ Using the `"inherit"` shortcut will cause your tests to be transpiled the same a
"babel": "inherit"
},
"babel": {
"presets": ["es2015-node4", "react"]
"presets": ["@ava/stage-4", "react"]
}
}
```

In the above example, both tests and sources will be transpiled using the [`es2015-node4`](https://github.com/jbach/babel-preset-es2015-node4) and [`react`](http://babeljs.io/docs/plugins/preset-react/) presets.
In the above example, both tests and sources will be transpiled using the [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) and [`react`](http://babeljs.io/docs/plugins/preset-react/) presets.

## Extend your source transpilation configuration

Expand All @@ -87,12 +87,12 @@ When specifying the Babel config for your tests, you can set the `babelrc` optio
}
},
"babel": {
"presets": ["es2015-node4", "react"]
"presets": ["@ava/stage-4", "react"]
}
}
```

In the above example, *sources* are compiled use [`es2015-node4`](https://github.com/jbach/babel-preset-es2015-node4) and [`react`](http://babeljs.io/docs/plugins/preset-react/), *tests* use those same plugins, plus the additional `custom` plugins specified.
In the above example, *sources* are compiled use [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) and [`react`](http://babeljs.io/docs/plugins/preset-react/), *tests* use those same plugins, plus the additional `custom` plugins specified.

## Extend an alternate config file.

Expand Down
78 changes: 18 additions & 60 deletions lib/babel-config.js
Expand Up @@ -3,7 +3,6 @@ const path = require('path');
const chalk = require('chalk');
const figures = require('figures');
const convertSourceMap = require('convert-source-map');
const semver = require('semver');
const colors = require('./colors');

function validate(conf) {
Expand All @@ -25,68 +24,26 @@ function validate(conf) {
return conf;
}

function lazy(initFn) {
let initialized = false;
let value;
function lazy(buildPreset) {
let preset;

return () => {
if (!initialized) {
initialized = true;
value = initFn();
return babel => {
if (!preset) {
preset = buildPreset(babel);
}

return value;
return preset;
};
}

const defaultPresets = lazy(() => {
const esPreset = semver.satisfies(process.version, '>=6') ?
'babel-preset-node6' :
'babel-preset-es2015-node4';
const stage4 = lazy(() => require('@ava/babel-preset-stage-4')());

return [
require('babel-preset-stage-2'),
require(esPreset) // eslint-disable-line import/no-dynamic-require
];
});

const rewritePlugin = lazy(() => {
const wrapListener = require('babel-plugin-detective/wrap-listener');

return wrapListener(rewriteBabelRuntimePaths, 'rewrite-runtime', {
generated: true,
require: true,
import: true
function makeTransformTestFiles(powerAssert) {
return lazy(babel => {
return require('@ava/babel-preset-transform-test-files')(babel, {powerAssert});
});
});

function rewriteBabelRuntimePaths(path) {
const isBabelPath = /^babel-runtime[\\/]?/.test(path.node.value);

if (path.isLiteral() && isBabelPath) {
path.node.value = require.resolve(path.node.value);
}
}

const espowerPlugin = lazy(() => {
const babel = require('babel-core');
const createEspowerPlugin = require('babel-plugin-espower/create');

// Initialize power-assert
return createEspowerPlugin(babel, {
embedAst: true,
patterns: require('./enhance-assert').PATTERNS
});
});

const defaultPlugins = lazy(() => {
return [
require('babel-plugin-ava-throws-helper'),
rewritePlugin(),
require('babel-plugin-transform-runtime')
];
});

function build(babelConfig, powerAssert, filePath, code) {
babelConfig = validate(babelConfig);

Expand All @@ -95,7 +52,7 @@ function build(babelConfig, powerAssert, filePath, code) {
if (babelConfig === 'default') {
options = {
babelrc: false,
presets: defaultPresets()
presets: [stage4]
};
} else if (babelConfig === 'inherit') {
options = {
Expand All @@ -118,9 +75,10 @@ function build(babelConfig, powerAssert, filePath, code) {
ast: false
});

options.plugins = (options.plugins || [])
.concat(powerAssert ? espowerPlugin() : [])
.concat(defaultPlugins());
if (!options.presets) {
options.presets = [];
}
options.presets.push(makeTransformTestFiles(powerAssert));

return options;
}
Expand All @@ -143,8 +101,8 @@ function getSourceMap(filePath, code) {
module.exports = {
validate,
build,
pluginPackages: [
require.resolve('babel-core/package.json'),
require.resolve('babel-plugin-espower/package.json')
presetHashes: [
require('@ava/babel-preset-stage-4/package-hash'),
require('@ava/babel-preset-transform-test-files/package-hash')
]
};
16 changes: 9 additions & 7 deletions lib/caching-precompiler.js
Expand Up @@ -53,13 +53,15 @@ class CachingPrecompiler {
return `${result.code}\n${comment}`;
}
_createTransform() {
const pluginPackages = babelConfigHelper.pluginPackages;
const avaPackage = require.resolve('../package.json');
const packages = [avaPackage].concat(pluginPackages);
const majorNodeVersion = process.version.split('.')[0];
const babelConfig = JSON.stringify(this.babelConfig);
const packageSalt = babelConfig + majorNodeVersion + this.powerAssert;
const salt = packageHash.sync(packages, packageSalt);
const salt = packageHash.sync([
require.resolve('../package.json'),
require.resolve('babel-core/package.json')
], {
babelConfig: this.babelConfig,
majorNodeVersion: process.version.split('.')[0],
powerAssert: this.powerAssert,
presetHashes: babelConfigHelper.presetHashes
});

return cachingTransform({
factory: this._init,
Expand Down
3 changes: 3 additions & 0 deletions lib/enhance-assert.js
Expand Up @@ -3,6 +3,9 @@
module.exports = enhanceAssert;
module.exports.formatter = formatter;

// When adding patterns, don't forget to add to
// https://github.com/avajs/babel-preset-transform-test-files/blob/master/espower-patterns.json
// Then release a new version of that preset and bump the SemVer range here.
module.exports.PATTERNS = [
't.truthy(value, [message])',
't.falsy(value, [message])',
Expand Down
13 changes: 3 additions & 10 deletions package.json
Expand Up @@ -87,6 +87,8 @@
"observables"
],
"dependencies": {
"@ava/babel-preset-stage-4": "^1.0.0",
"@ava/babel-preset-transform-test-files": "^1.0.0",
"arr-flatten": "^1.0.1",
"array-union": "^1.0.1",
"array-uniq": "^1.0.2",
Expand All @@ -95,14 +97,6 @@
"ava-init": "^0.2.0",
"babel-code-frame": "^6.16.0",
"babel-core": "^6.17.0",
"babel-plugin-ava-throws-helper": "^0.1.0",
"babel-plugin-detective": "^2.0.0",
"babel-plugin-espower": "^2.3.1",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-preset-es2015-node4": "^2.1.0",
"babel-preset-node6": "^11.0.0",
"babel-preset-stage-2": "^6.17.0",
"babel-runtime": "^6.11.6",
"bluebird": "^3.0.0",
"caching-transform": "^1.0.0",
"chalk": "^1.0.0",
Expand Down Expand Up @@ -147,7 +141,7 @@
"multimatch": "^2.1.0",
"observable-to-promise": "^0.4.0",
"option-chain": "^0.1.0",
"package-hash": "^1.1.0",
"package-hash": "^1.2.0",
"pkg-conf": "^2.0.0",
"plur": "^2.0.0",
"power-assert-context-formatter": "^1.0.4",
Expand All @@ -157,7 +151,6 @@
"repeating": "^2.0.0",
"require-precompiled": "^0.1.0",
"resolve-cwd": "^1.0.0",
"semver": "^5.3.0",
"slash": "^1.0.0",
"source-map-support": "^0.4.0",
"stack-utils": "^0.4.0",
Expand Down
16 changes: 6 additions & 10 deletions readme.md
Expand Up @@ -40,7 +40,7 @@ Translations: [Espa帽ol](https://github.com/avajs/ava-docs/blob/master/es_ES/rea
- Enforces writing atomic tests
- No implicit globals
- [Isolated environment for each test file](#process-isolation)
- [Write your tests in ES2015](#es2015-support)
- [Write your tests in ES2017](#es2017-support)
- [Promise support](#promise-support)
- [Generator function support](#generator-function-support)
- [Async function support](#async-function-support)
Expand Down Expand Up @@ -266,7 +266,7 @@ All of the CLI options can be configured in the `ava` section of your `package.j

Arguments passed to the CLI will always take precedence over the configuration in `package.json`.

See the [ES2015 support](#es2015-support) section for details on the `babel` option.
See the [ES2017 support](#es2017-support) section for details on the `babel` option.

## Documentation

Expand Down Expand Up @@ -671,21 +671,17 @@ test(t => {
});
```

### ES2015 support
### ES2017 support

AVA comes with built-in support for ES2015 through [Babel 6](https://babeljs.io). Just write your tests in ES2015. No extra setup needed. You can use any Babel version in your project. We use our own bundled Babel with the [`es2015`](https://babeljs.io/docs/plugins/preset-es2015/) and [`stage-2`](https://babeljs.io/docs/plugins/preset-stage-2/) presets, as well as the [`espower`](https://github.com/power-assert-js/babel-plugin-espower) and [`transform-runtime`](https://babeljs.io/docs/plugins/transform-runtime/) plugins.
AVA comes with built-in support for ES2017 through [Babel 6](https://babeljs.io). Just write your tests in ES2017. No extra setup needed. You can use any Babel version in your project. We use our own bundled Babel with our [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) and [`stage-2`](https://babeljs.io/docs/plugins/preset-stage-2/) preset, as well as [custom transforms](https://github.com/avajs/babel-preset-transform-test-files) for test and helper files.

The corresponding Babel config for AVA's setup is as follows:

```json
{
"presets": [
"es2015",
"stage-2"
],
"plugins": [
"espower",
"transform-runtime"
"@ava/stage-4",
"@ava/transform-test-files"
]
}
```
Expand Down
11 changes: 3 additions & 8 deletions test/api.js
Expand Up @@ -693,11 +693,11 @@ function generateTests(prefix, apiCreator) {
});

test(`${prefix} power-assert support`, t => {
t.plan(4);
t.plan(3);

const api = apiCreator({
babelConfig: {
presets: ['react', 'es2015-node4', 'stage-2']
presets: ['react', '@ava/stage-4']
}
});

Expand All @@ -715,11 +715,6 @@ function generateTests(prefix, apiCreator) {

t.match(
result.errors[2].error.message,
/t\.true\(o === \{ ...o \}\)\s*\n\s+\|\s*\n\s+Object\{\}/m
);

t.match(
result.errors[3].error.message,
/t\.true\(<div \/> === <span \/>\)/m
);
});
Expand Down Expand Up @@ -878,7 +873,7 @@ function generateTests(prefix, apiCreator) {

const api = apiCreator({
babelConfig: {
presets: ['es2015-node4', 'stage-2'],
presets: ['@ava/stage-4'],
plugins: [testCapitalizerPlugin]
},
cacheEnabled: false
Expand Down

0 comments on commit ad5122d

Please sign in to comment.