Skip to content

Commit

Permalink
Use Babel directly
Browse files Browse the repository at this point in the history
Simplify test and helper file precompilation. Remove alternate Babel
options resolution, at the cost of false cache positives.

Fixes #1732
Fixes #1667
Fixes #1789
  • Loading branch information
novemberborn committed May 27, 2018
1 parent ebb3948 commit a5de369
Show file tree
Hide file tree
Showing 16 changed files with 781 additions and 1,095 deletions.
49 changes: 22 additions & 27 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const debounce = require('lodash.debounce');
const Bluebird = require('bluebird');
const getPort = require('get-port');
const arrify = require('arrify');
const makeDir = require('make-dir');
const ms = require('ms');
const babelConfigHelper = require('./lib/babel-config');
const CachingPrecompiler = require('./lib/caching-precompiler');
const babelPipeline = require('./lib/babel-pipeline');
const Emittery = require('./lib/emittery');
const RunStatus = require('./lib/run-status');
const AvaFiles = require('./lib/ava-files');
Expand All @@ -37,6 +37,8 @@ class Api extends Emittery {

this.options = Object.assign({match: []}, options);
this.options.require = resolveModules(this.options.require);

this._precompiler = null;
}

run(files, runtimeOptions) {
Expand Down Expand Up @@ -117,11 +119,10 @@ class Api extends Emittery {
}
});

// Set up a fresh precompiler for each test run.
return emittedRun
.then(() => this._setupPrecompiler())
.then(precompilation => {
if (!precompilation) {
if (!precompilation.precompileFile) {
return null;
}

Expand All @@ -132,11 +133,13 @@ class Api extends Emittery {
return new AvaFiles({cwd: this.options.resolveTestsFrom}).findTestHelpers().then(helpers => {
return {
cacheDir: precompilation.cacheDir,
map: files.concat(helpers).reduce((acc, file) => {
map: [...files, ...helpers].reduce((acc, file) => {
try {
const realpath = fs.realpathSync(file);
const hash = precompilation.precompiler.precompileFile(realpath);
acc[realpath] = hash;
const cachePath = precompilation.precompileFile(realpath);
if (cachePath) {
acc[realpath] = cachePath;
}
} catch (err) {
throw Object.assign(err, {file});
}
Expand Down Expand Up @@ -204,31 +207,23 @@ class Api extends Emittery {
}

_setupPrecompiler() {
if (this._precompiler) {
return this._precompiler;
}

const cacheDir = this.options.cacheEnabled === false ?
uniqueTempDir() :
path.join(this.options.projectDir, 'node_modules', '.cache', 'ava');

return this._buildBabelConfig(cacheDir).then(result => {
return result ? {
cacheDir,
precompiler: new CachingPrecompiler({
path: cacheDir,
getBabelOptions: result.getOptions,
babelCacheKeys: result.cacheKeys
})
} : null;
});
}

_buildBabelConfig(cacheDir) {
if (this._babelConfigPromise) {
return this._babelConfigPromise;
}
// Ensure cacheDir exists
makeDir.sync(cacheDir);

const compileEnhancements = this.options.compileEnhancements !== false;
const promise = babelConfigHelper.build(this.options.projectDir, cacheDir, this.options.babelConfig, compileEnhancements);
this._babelConfigPromise = promise;
return promise;
const {projectDir, babelConfig, compileEnhancements} = this.options;
this._precompiler = {
cacheDir,
precompileFile: babelPipeline.build(projectDir, cacheDir, babelConfig, compileEnhancements !== false)
};
return this._precompiler;
}

_computeForkExecArgv() {
Expand Down
20 changes: 16 additions & 4 deletions docs/recipes/babel.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/docs/recipes/babel.md)

AVA uses [Babel 7](https://babeljs.io) so you can use the latest JavaScript syntax in your tests. We do this by compiling test and helper files using our [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) preset. We also use a [second preset](https://github.com/avajs/babel-preset-transform-test-files) to enable [enhanced assertion messages](../../readme#enhanced-assertion-messages) and detect improper use of `t.throws()` assertions.
AVA uses [Babel 7](https://babeljs.io) so you can use the latest JavaScript syntax in your tests. We do this by compiling test and helper files using our [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) preset. We also use a [second preset, `@ava/transform-test-files`](https://github.com/avajs/babel-preset-transform-test-files) to enable [enhanced assertion messages](../../readme#enhanced-assertion-messages) and detect improper use of `t.throws()` assertions.

By default our Babel pipeline is applied to test and helper files ending in `.js`. If your project uses Babel then we'll automatically compile these files using your project's Babel configuration.
By default our Babel pipeline is applied to test and helper files ending in `.js`. If your project uses Babel then we'll automatically compile these files using your project's Babel configuration. The `@ava/transform-helper-files` preset is applied first, and the `@ava/stage-4` last.

If you are using Babel for your source files then you must also [configure source compilation](#compile-sources).

AVA only looks for Babel configuration files in your project directory. That is, `.babelrc` or `.babelrc.js` files next to your `package.json` file, or the `package.json` file itself.

## Customize how AVA compiles your test files

You can override the default Babel configuration AVA uses for test file compilation in `package.json`. For example, the configuration below adds support for JSX syntax and stage 3 features:
Expand All @@ -29,6 +27,16 @@ You can override the default Babel configuration AVA uses for test file compilat

All `.babelrc` options are allowed inside the `testOptions` object.

## Reset AVA's cache

AVA caches the compiled test and helper files. It automatically recompiles these files when you change them, however it can't detect updates of your Babel plugins and presets, or changes to your Babel configuration files.

Instead run the following to reset AVA's cache when you change the configuration or update plugins or presets:

```console
$ npx ava --reset-cache
```

## Make AVA skip your project's Babel options

You may not want AVA to use your project's Babel options, for example if your project is relying on Babel 6. You can set the `babelrc` option to `false`:
Expand Down Expand Up @@ -65,6 +73,8 @@ You can disable AVA's stage-4 preset:

Note that this *does not* stop AVA from compiling your test files using Babel.

You **must** disable the preset by configuring it in the `testOptions`. AVA will still apply the preset if you configure it in other files (for instance a `.babelrc` file). This is [due to a Babel issue](https://github.com/babel/babel/issues/7920).

## Preserve ES module syntax

By default AVA's stage-4 preset will convert ES module syntax to CommonJS. This can be disabled:
Expand All @@ -83,6 +93,8 @@ By default AVA's stage-4 preset will convert ES module syntax to CommonJS. This
}
```

You **must** configure the preset in the `testOptions` in order to preserve the ES module syntax. AVA will still apply the preset if you configure it in other files (for instance a `.babelrc` file). This is [due to a Babel issue](https://github.com/babel/babel/issues/7920).

You'll have to use the [`esm`](https://github.com/standard-things/esm) module so that AVA can still load your test files. [See our recipe for details](./es-modules.md).

## Disable AVA's Babel pipeline
Expand Down
197 changes: 0 additions & 197 deletions lib/babel-config.js

This file was deleted.

Loading

0 comments on commit a5de369

Please sign in to comment.