diff --git a/api.js b/api.js index 0b1075136..ac2667a95 100644 --- a/api.js +++ b/api.js @@ -14,6 +14,7 @@ var AvaFiles = require('ava-files'); var AvaError = require('./lib/ava-error'); var fork = require('./lib/fork'); var CachingPrecompiler = require('./lib/caching-precompiler'); +var Precompiler = require('./lib/precompiler'); var RunStatus = require('./lib/run-status'); function Api(options) { @@ -48,8 +49,15 @@ module.exports = Api; Api.prototype._runFile = function (file, runStatus) { var hash = this.precompiler.precompileFile(file); - var precompiled = {}; - precompiled[file] = hash; + + var precompiled; + + if (this.options.precompile) { + precompiled = runStatus.precompiler.createHash(file, hash, this.precompiler.getDetectiveMetadata(hash)); + } else { + precompiled = {}; + precompiled[file] = hash; + } var options = objectAssign({}, this.options, { precompiled: precompiled @@ -122,6 +130,10 @@ Api.prototype._run = function (files, _options) { self.precompiler = new CachingPrecompiler(cacheDir, self.options.babelConfig); self.fileCount = files.length; + if (self.options.precompile) { + runStatus.precompiler = new Precompiler(cacheDir); + } + var overwatch; if (this.options.concurrency > 0) { overwatch = this._runLimitedPool(files, runStatus, self.options.serial ? 1 : this.options.concurrency); diff --git a/cli.js b/cli.js index 94cb33ab8..26e8c0642 100755 --- a/cli.js +++ b/cli.js @@ -66,6 +66,7 @@ var cli = meow([ ' --source, -S Pattern to match source files so tests can be re-run (Can be repeated)', ' --timeout, -T Set global timeout', ' --concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL)', + ' --precompile, -p Precompile sources in the main thread (EXPERIMENTAL)', '', 'Examples', ' ava', @@ -91,7 +92,8 @@ var cli = meow([ 'verbose', 'serial', 'tap', - 'watch' + 'watch', + 'precompile' ], default: conf, alias: { @@ -103,7 +105,8 @@ var cli = meow([ w: 'watch', S: 'source', T: 'timeout', - c: 'concurrency' + c: 'concurrency', + p: 'precompile' } }); @@ -125,6 +128,7 @@ if ( var api = new Api({ failFast: cli.flags.failFast, serial: cli.flags.serial, + precompile: cli.flags.precompile, require: arrify(cli.flags.require), cacheEnabled: cli.flags.cache !== false, explicitTitles: cli.flags.watch, diff --git a/docs/experimental-features.md b/docs/experimental-features.md new file mode 100644 index 000000000..26b32480c --- /dev/null +++ b/docs/experimental-features.md @@ -0,0 +1,36 @@ +The following features are considered "Experimental", in that their exact behavior is not yet considered locked. In general, we want to keep these features around long term (they are unlikely to disappear completely), but significant breaking changes are fair game until we are confident in the implementation. For core features, we generally try to accompany any breaking changes with codemods to ease the pain of upgrading, but users should not expect the same for experimental features. + +Our recommendation is to go ahead and use these features *if* they provide significant benefits to your build. + +# Limited Concurrency + +Enabled via a CLI flag (`--concurrency=5`) or via the `ava` section of `package.json` (`concurrency:5`). + +This will limit the number of test files spawned concurrently during the test run. The default is unlimited, and will cause all test files to be spawned at once. This can overload some systems if there are many, many test files. Especially on CI machines. Use the concurrency config to limit the number of processes. You may need to experiment to determine the optimal number of processes. + +## Caveats + +Use of `test.only` does not work correctly when concurrency is limited. We think this is a solvable problem. + +## Stability + +Fixing the `test.only` bug is the main barrier to this becoming a stable core feature. It's proven invaluable to many users, so we aren't likely to remove it unless some significantly better solution is discovered. + +We believe limited concurrency is valuable enough to eventually become the default. We would love your help determining what the default concurrency limit should be (See [#966](https://github.com/avajs/ava/issues/966)). + + +# Precompiling Sources + +AVA already precompiles your *test* files in the main process. This removes the need for loading `babel-register` in every child process just to compile the test. This has significant performance benefits. Unfortunately, this does not help users who want to write their project *source* files using the latest language features, as they often still need to use the `--require=babel-register` flag. + +Enabled via a CLI flag (`--precompile`) or via the `ava` section of `packages.json` (`precompile:true`), this feature will cause AVA to precompile all required sources via Babel in the main process before launching the child process. If you use this feature, you **must** remove `--require=babel-register` from your config to see any benefits. + +## Caveats + +It does not work with relative requires. You must use strings in your require statements, expressions will not work. This is very similar to the restrictions imposed by packagers like `browserify`. If you need to make relative requires, you must still use `babel-register`, so this is unlikely to be helpful. + +It only works with Babel 6. We still need to find a solution for typescript users. + +## Stability + +This is a fairly new feature, and the details may change significantly. Initial tests indicate some very significant performance increases over `babel-register`. It is possible that `babel-register` could be further optimized with more a more aggressive caching mechanism that would obviate the need for this feature. As long as it proves to provide significant performance benefits, it is likely this feature will stick around in one way or another. diff --git a/lib/babel-config.js b/lib/babel-config.js index 0f3a138a0..6a939e295 100644 --- a/lib/babel-config.js +++ b/lib/babel-config.js @@ -80,7 +80,8 @@ var defaultPlugins = lazy(function () { espowerPlugin(), require('babel-plugin-ava-throws-helper'), rewritePlugin(), - require('babel-plugin-transform-runtime') + require('babel-plugin-transform-runtime'), + require('babel-plugin-detective') ]; }); diff --git a/lib/caching-precompiler.js b/lib/caching-precompiler.js index fd26edc1d..6b9051f47 100644 --- a/lib/caching-precompiler.js +++ b/lib/caching-precompiler.js @@ -16,6 +16,7 @@ function CachingPrecompiler(cacheDirPath, babelConfig) { this.babelConfig = babelConfigHelper.validate(babelConfig); this.cacheDirPath = cacheDirPath; this.fileHashes = {}; + this.detectiveMetadataResults = {}; Object.keys(CachingPrecompiler.prototype).forEach(function (name) { this[name] = this[name].bind(this); @@ -45,6 +46,7 @@ CachingPrecompiler.prototype._factory = function () { CachingPrecompiler.prototype._init = function () { this.babel = require('babel-core'); + this.detective = require('babel-plugin-detective'); }; CachingPrecompiler.prototype._transform = function (code, filePath, hash) { @@ -54,9 +56,19 @@ CachingPrecompiler.prototype._transform = function (code, filePath, hash) { var result = this.babel.transform(code, options); // save source map - var mapPath = path.join(this.cacheDirPath, hash + '.js.map'); + var mapPath = this.cachePath(hash + '.js.map'); fs.writeFileSync(mapPath, JSON.stringify(result.map)); + // save detective results + var detectiveMetadata = this.detective.metadata(result) || {}; + var serializableDetectiveMetadata = { + strings: detectiveMetadata.strings || [], + expressions: Boolean(detectiveMetadata.expressions && detectiveMetadata.expressions.length) + }; + var detectiveResultsPath = this.cachePath(hash + '.detective.json'); + fs.writeFileSync(detectiveResultsPath, JSON.stringify(serializableDetectiveMetadata)); + this.detectiveMetadataResults[hash] = serializableDetectiveMetadata; + // When loading the test file, test workers intercept the require call and // load the cached code instead. Libraries like nyc may also be intercepting // require calls, however they won't know that different code was loaded. @@ -73,6 +85,10 @@ CachingPrecompiler.prototype._transform = function (code, filePath, hash) { return result.code + '\n' + comment; }; +CachingPrecompiler.prototype.cachePath = function (filename) { + return path.join(this.cacheDirPath, filename); +}; + CachingPrecompiler.prototype._createTransform = function () { var salt = packageHash.sync( [require.resolve('../package.json')].concat(babelConfigHelper.pluginPackages), @@ -94,3 +110,14 @@ CachingPrecompiler.prototype._generateHash = function (code, filePath, salt) { return hash; }; + +CachingPrecompiler.prototype.getDetectiveMetadata = function (hash) { + var metadata = this.detectiveMetadataResults[hash]; + + if (!metadata) { + metadata = JSON.parse(fs.readFileSync(this.cachePath(hash + '.detective.json'), 'utf8')); + this.detectiveMetadataResults[hash] = metadata; + } + + return metadata; +}; diff --git a/lib/precompiler.js b/lib/precompiler.js new file mode 100644 index 000000000..1fc95bfa6 --- /dev/null +++ b/lib/precompiler.js @@ -0,0 +1,116 @@ +var path = require('path'); +var resolveFrom = require('resolve-from'); +var resolve = require('resolve'); +var md5hex = require('md5-hex'); +var detective = require('babel-plugin-detective'); +var fs = require('graceful-fs'); + +var base = path.join(__dirname, '..', 'index.js'); + +var _babelCore; + +function babelCore() { + if (!_babelCore) { + _babelCore = require('babel-core'); + } + return _babelCore; +} + +function Precompiler(cacheDir) { + this._cacheDir = cacheDir; + this._cache = {}; +} + +Precompiler.prototype.cachePath = function (filename) { + return path.join(this._cacheDir, filename); +}; + +Precompiler.prototype.buildEntry = function (filename) { + if (this._cache[filename]) { + return; + } + + var entry = this._cache[filename] = {}; + + var result = babelCore().transformFileSync(filename, { + plugins: [detective] + }); + + var hash = md5hex(result.code); + fs.writeFileSync(this.cachePath(hash + '.js'), result.code); + + var metadata = detective.metadata(result); + + var dependencies = this.normalizeDependencies(filename, metadata); + + entry.hash = hash; + entry.dependencies = dependencies; + + dependencies.forEach(this.buildEntry, this); +}; + +Precompiler.prototype.normalizeDependencies = function (filename, metadata) { + if (metadata && metadata.expressions && (metadata.expressions === true || metadata.expressions.length)) { + console.warn(filename + ' has a dynamic require - precompilation may not work'); + } + + if (!metadata || !metadata.strings || !metadata.strings.length) { + return []; + } + + var dir = path.dirname(filename); + + return metadata.strings + .filter(function (dep) { + return !resolve.isCore(dep); + }) + .map(function (dep) { + try { + return resolveFrom(dir, dep); + } catch (err) { + err.message = 'Failed to resolve dependency ' + JSON.stringify(dep) + ' for file ' + JSON.stringify(filename) + ' ' + err.message; + throw err; + } + }) + .filter(Boolean) + .filter(this.shouldTranspile, this); +}; + +Precompiler.prototype.shouldTranspile = function (filename) { + return ( + (filename !== base) && + !/[\/\\]node_modules[\/\\]/.test(filename) && + /\.js$/.test(filename) + ); +}; + +Precompiler.prototype.createHash = function (filename, hash, metadata) { + var hashMap = {}; + hashMap[filename] = hash; + + var dependencies = this.normalizeDependencies(filename, metadata); + + dependencies.forEach(this.buildEntry, this); + + dependencies.forEach(function (filename) { + this.attach(filename, hashMap); + }, this); + + return hashMap; +}; + +Precompiler.prototype.attach = function (filename, hashMap) { + if (hashMap[filename] || !this._cache[filename]) { + return; + } + + var entry = this._cache[filename]; + + hashMap[filename] = entry.hash; + + entry.dependencies.forEach(function (filename) { + this.attach(filename, hashMap); + }, this); +}; + +module.exports = Precompiler; diff --git a/lib/test-worker.js b/lib/test-worker.js index 37abb70fc..0f992ab5b 100644 --- a/lib/test-worker.js +++ b/lib/test-worker.js @@ -52,10 +52,14 @@ sourceMapSupport.install({ handleUncaughtExceptions: false, retrieveSourceMap: function (source) { if (sourceMapCache[source]) { - return { - url: source, - map: fs.readFileSync(sourceMapCache[source], 'utf8') - }; + try { + return { + url: source, + map: fs.readFileSync(sourceMapCache[source], 'utf8') + }; + } catch (err) { + return null; + } } } }); diff --git a/package.json b/package.json index 2acc191bc..7c0534794 100644 --- a/package.json +++ b/package.json @@ -86,7 +86,7 @@ "babel-code-frame": "^6.7.5", "babel-core": "^6.3.21", "babel-plugin-ava-throws-helper": "^0.1.0", - "babel-plugin-detective": "^1.0.2", + "babel-plugin-detective": "^2.0.0", "babel-plugin-espower": "^2.2.0", "babel-plugin-transform-runtime": "^6.3.13", "babel-preset-es2015": "^6.3.13", @@ -111,6 +111,7 @@ "find-cache-dir": "^0.1.1", "fn-name": "^2.0.0", "globby": "^5.0.0", + "graceful-fs": "^4.1.4", "has-flag": "^2.0.0", "ignore-by-default": "^1.0.0", "is-ci": "^1.0.7", @@ -141,7 +142,9 @@ "pretty-ms": "^2.0.0", "repeating": "^2.0.0", "require-precompiled": "^0.1.0", + "resolve": "^1.1.7", "resolve-cwd": "^1.0.0", + "resolve-from": "^2.0.0", "set-immediate-shim": "^1.0.1", "slash": "^1.0.0", "source-map-support": "^0.4.0", @@ -182,7 +185,9 @@ }, "overrides": [ { - "files": ["test/**/*.js"], + "files": [ + "test/**/*.js" + ], "rules": { "max-lines": 0 } diff --git a/readme.md b/readme.md index 9fa5d5372..31ee32ce9 100644 --- a/readme.md +++ b/readme.md @@ -27,6 +27,7 @@ Translations: [EspaƱol](https://github.com/avajs/ava-docs/blob/master/es_ES/rea - [Related](#related) - [Links](#links) - [Team](#team) +- [Experimental Features](docs/experimental-features.md) ## Why AVA? @@ -150,6 +151,7 @@ $ ava --help --source, -S Pattern to match source files so tests can be re-run (Can be repeated) --timeout, -T Set global timeout --concurrency, -c Maximum number of test files running at the same time (EXPERIMENTAL) + --precompile, -p Precompile sources in the main thread (EXPERIMENTAL) Examples ava @@ -189,6 +191,7 @@ All of the CLI options can be configured in the `ava` section of your `package.j "!foo" ], "concurrency": 5, + "precompile": false, "failFast": true, "tap": true, "require": [ diff --git a/test/api.js b/test/api.js index d084a8350..1a786d153 100644 --- a/test/api.js +++ b/test/api.js @@ -683,16 +683,17 @@ function generateTests(prefix, apiCreator) { }); test(prefix + 'caching is enabled by default', function (t) { - t.plan(3); + t.plan(4); rimraf.sync(path.join(__dirname, 'fixture/caching/node_modules')); var api = apiCreator(); api.run([path.join(__dirname, 'fixture/caching/test.js')]) .then(function () { var files = fs.readdirSync(path.join(__dirname, 'fixture/caching/node_modules/.cache/ava')); - t.is(files.length, 2); + t.is(files.length, 3); t.is(files.filter(endsWithJs).length, 1); t.is(files.filter(endsWithMap).length, 1); + t.is(files.filter(endsWithJson).length, 1); t.end(); }); @@ -703,6 +704,10 @@ function generateTests(prefix, apiCreator) { function endsWithMap(filename) { return /\.map$/.test(filename); } + + function endsWithJson(filename) { + return /\.json/.test(filename); + } }); test(prefix + 'caching can be disabled', function (t) { diff --git a/test/babel-config.js b/test/babel-config.js index 1e9d86a60..507d44f25 100644 --- a/test/babel-config.js +++ b/test/babel-config.js @@ -6,6 +6,7 @@ var sinon = require('sinon'); var proxyquire = require('proxyquire').noCallThru(); var throwsHelper = require('babel-plugin-ava-throws-helper'); var transformRuntime = require('babel-plugin-transform-runtime'); +var detective = require('babel-plugin-detective'); function fixture(name) { return path.join(__dirname, 'fixture', name); @@ -45,6 +46,6 @@ test('uses babelConfig for babel options when babelConfig is an object', functio t.true('inputSourceMap' in options); t.false(options.babelrc); t.strictDeepEqual(options.presets, ['stage-2', 'es2015']); - t.strictDeepEqual(options.plugins, [customPlugin, powerAssert, throwsHelper, rewrite, transformRuntime]); + t.strictDeepEqual(options.plugins, [customPlugin, powerAssert, throwsHelper, rewrite, transformRuntime, detective]); t.end(); }); diff --git a/test/caching-precompiler.js b/test/caching-precompiler.js index 502e3d320..b3ed6ce58 100644 --- a/test/caching-precompiler.js +++ b/test/caching-precompiler.js @@ -9,8 +9,10 @@ var fromMapFileSource = require('convert-source-map').fromMapFileSource; var CachingPrecompiler = require('../lib/caching-precompiler'); -function fixture(name) { - return path.join(__dirname, 'fixture', name); +function fixture() { + var args = Array.prototype.slice.call(arguments); + args.unshift(__dirname, 'fixture'); + return path.join.apply(path, args); } function endsWithJs(filename) { @@ -21,6 +23,10 @@ function endsWithMap(filename) { return /\.js\.map$/.test(filename); } +function endsWithJson(filename) { + return /\.json$/.test(filename); +} + sinon.spy(babel, 'transform'); test('creation with new', function (t) { @@ -48,9 +54,10 @@ test('adds files and source maps to the cache directory as needed', function (t) t.true(fs.existsSync(tempDir), 'cache directory is lazily created'); var files = fs.readdirSync(tempDir); - t.is(files.length, 2); + t.is(files.length, 3); t.is(files.filter(endsWithJs).length, 1, 'one .js file is saved to the cache'); t.is(files.filter(endsWithMap).length, 1, 'one .js.map file is saved to the cache'); + t.is(files.filter(endsWithJson).length, 1, 'one .json file is saved to the cache'); t.end(); }); @@ -130,3 +137,45 @@ test('does not modify plugins array in babelConfig', function (t) { t.strictDeepEqual(plugins, []); t.end(); }); + +test('prepares a list of dependencies', function (t) { + var precompiler = new CachingPrecompiler(uniqueTempDir(), 'default'); + + var hash = precompiler.precompileFile(fixture('with-dependencies', 'test.js')); + + var metadata = precompiler.getDetectiveMetadata(hash); + + t.strictDeepEqual(metadata, { + expressions: false, + strings: [ + '../../../', + './dep-1', + './dep-2', + './dep-3.custom' + ] + }); + + t.end(); +}); + +test('caches the list of dependencies', function (t) { + var cacheDirPath = uniqueTempDir(); + var precompiler1 = new CachingPrecompiler(cacheDirPath, 'default'); + var hash = precompiler1.precompileFile(fixture('with-dependencies', 'test.js')); + + var precompiler2 = new CachingPrecompiler(cacheDirPath, 'default'); + + var metadata = precompiler2.getDetectiveMetadata(hash); + + t.strictDeepEqual(metadata, { + expressions: false, + strings: [ + '../../../', + './dep-1', + './dep-2', + './dep-3.custom' + ] + }); + + t.end(); +}); diff --git a/test/cli.js b/test/cli.js index dc7f54c15..bc832b7e1 100644 --- a/test/cli.js +++ b/test/cli.js @@ -326,3 +326,17 @@ test('should warn ava is required without the cli', function (t) { t.end(); }); }); + +test('precompiler fixture passes with --precompile flag', function (t) { + execCli(['--precompile', 'fixture/precompiler/test.js'], function (error) { + t.ifError(error); + t.end(); + }); +}); + +test('precompiler fixture fails without --precompile flag', function (t) { + execCli(['fixture/precompiler/test.js'], function (error) { + t.ok(error); + t.end(); + }); +}); diff --git a/test/fixture/precompiler/.babelrc b/test/fixture/precompiler/.babelrc new file mode 100644 index 000000000..9e3d2741f --- /dev/null +++ b/test/fixture/precompiler/.babelrc @@ -0,0 +1,6 @@ +{ + "presets": [ + "es2015", + "stage-2" + ] +} diff --git a/test/fixture/precompiler/dep.json b/test/fixture/precompiler/dep.json new file mode 100644 index 000000000..5c294f87c --- /dev/null +++ b/test/fixture/precompiler/dep.json @@ -0,0 +1,4 @@ +{ + "foo": "foo", + "bar": "bar" +} diff --git a/test/fixture/precompiler/dep1.js b/test/fixture/precompiler/dep1.js new file mode 100644 index 000000000..b31119f45 --- /dev/null +++ b/test/fixture/precompiler/dep1.js @@ -0,0 +1,4 @@ +import dep2 from './dep2'; +import dep3 from './dep3'; + +export default dep2 + dep3; diff --git a/test/fixture/precompiler/dep2.js b/test/fixture/precompiler/dep2.js new file mode 100644 index 000000000..d02ba545b --- /dev/null +++ b/test/fixture/precompiler/dep2.js @@ -0,0 +1 @@ +export default 'foo'; diff --git a/test/fixture/precompiler/dep3.js b/test/fixture/precompiler/dep3.js new file mode 100644 index 000000000..d9a5cffda --- /dev/null +++ b/test/fixture/precompiler/dep3.js @@ -0,0 +1 @@ +export default 'bar'; diff --git a/test/fixture/precompiler/test.js b/test/fixture/precompiler/test.js new file mode 100644 index 000000000..86b5a54fb --- /dev/null +++ b/test/fixture/precompiler/test.js @@ -0,0 +1,9 @@ +import path from 'path'; //ensure we don't try to precompile core modules +import test from '../../../'; +import dep1 from './dep1'; + +var json = require('./dep.json'); + +test(t => { + t.is(dep1, 'foobar'); +});