diff --git a/lib/babel.js b/lib/babel.js index a792a85c34..ac1311bbd5 100644 --- a/lib/babel.js +++ b/lib/babel.js @@ -1,6 +1,12 @@ 'use strict'; +var cacheDir = require('xdg-basedir').cache; var debug = require('debug')('ava'); +var hasha = require('hasha'); +var cacha = require('cacha'); +var join = require('path').join; + +var cache = cacha(join(cacheDir, 'ava')); var opts = JSON.parse(process.argv[2]); @@ -36,27 +42,13 @@ sourceMapSupport.install({ } }); -var createEspowerPlugin = require('babel-plugin-espower/create'); var requireFromString = require('require-from-string'); var loudRejection = require('loud-rejection/api')(process); var serializeError = require('serialize-error'); -var babel = require('babel-core'); var send = require('./send'); var testPath = opts.file; -// initialize power-assert -var powerAssert = createEspowerPlugin(babel, { - patterns: require('./enhance-assert').PATTERNS -}); - -// if generators are not supported, use regenerator -var options = { - presets: ['stage-2', 'es2015'], - plugins: [powerAssert, 'transform-runtime'], - sourceMaps: true -}; - // check if test files required ava and show error, when they didn't exports.avaRequired = false; @@ -70,11 +62,48 @@ if (inputSourceMap) { } // include test file -var transpiled = babel.transformFileSync(testPath, options); -sourceMapCache[testPath] = transpiled.map; -requireFromString(transpiled.code, testPath, { - appendPaths: module.paths -}); +var cachePath = hasha(testPath); +var hashPath = hasha(testPath) + '_hash'; + +var prevHash = cache.getSync(hashPath).toString(); +var currHash = hasha.fromFileSync(testPath); + +if (prevHash === currHash) { + var cached = JSON.parse(cache.getSync(cachePath)); + + sourceMapCache[testPath] = cached.map; + requireFromString(cached.code, testPath, { + appendPaths: module.paths + }); +} else { + var createEspowerPlugin = require('babel-plugin-espower/create'); + var babel = require('babel-core'); + + // initialize power-assert + var powerAssert = createEspowerPlugin(babel, { + patterns: require('./enhance-assert').PATTERNS + }); + + // if generators are not supported, use regenerator + var options = { + presets: ['stage-2', 'es2015'], + plugins: [powerAssert, 'transform-runtime'], + sourceMaps: true + }; + + var transpiled = babel.transformFileSync(testPath, options); + + cache.setSync(hashPath, currHash); + cache.setSync(cachePath, JSON.stringify({ + code: transpiled.code, + map: transpiled.map + })); + + sourceMapCache[testPath] = transpiled.map; + requireFromString(transpiled.code, testPath, { + appendPaths: module.paths + }); +} process.on('uncaughtException', function (exception) { send('uncaughtException', {exception: serializeError(exception)}); diff --git a/package.json b/package.json index ed58a32cf5..e7e2c0d0a5 100644 --- a/package.json +++ b/package.json @@ -88,6 +88,7 @@ "babel-preset-stage-2": "^6.3.13", "babel-runtime": "^6.3.19", "bluebird": "^3.0.0", + "cacha": "^1.0.3", "chalk": "^1.0.0", "co-with-promise": "^4.6.0", "core-assert": "^0.1.0", @@ -97,6 +98,7 @@ "figures": "^1.4.0", "fn-name": "^2.0.0", "globby": "^4.0.0", + "hasha": "^2.0.2", "is-generator-fn": "^1.0.0", "is-observable": "^0.1.0", "is-promise": "^2.1.0", @@ -116,7 +118,8 @@ "source-map-support": "^0.4.0", "squeak": "^1.2.0", "time-require": "^0.1.2", - "update-notifier": "^0.5.0" + "update-notifier": "^0.5.0", + "xdg-basedir": "^2.0.0" }, "devDependencies": { "coveralls": "^2.11.4",