Skip to content

Commit

Permalink
cache babel-transpiled code
Browse files Browse the repository at this point in the history
  • Loading branch information
vdemedes committed Dec 24, 2015
1 parent 65ae07c commit 4dc5c7d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
74 changes: 58 additions & 16 deletions lib/babel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
'use strict';

var pkgDir = require('pkg-dir').sync;
var debug = require('debug')('ava');
var hasha = require('hasha');
var cacha = require('cacha');
var path = require('path');

var opts = JSON.parse(process.argv[2]);
var testPath = opts.file;

var cache = cacha(path.join(pkgDir(path.dirname(testPath)), 'node_modules', '.cache', 'ava'));

if (debug.enabled) {
// Forward the `time-require` `--sorted` flag.
Expand Down Expand Up @@ -33,24 +40,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: [require('babel-preset-stage-2'), require('babel-preset-es2015')],
plugins: [powerAssert, require('babel-plugin-transform-runtime')],
sourceMaps: true
};

Expand All @@ -67,11 +63,44 @@ 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(cacheKey(testPath));
var hashPath = cachePath + '_hash';

var prevHash = cache.getSync(hashPath, {encoding: 'utf8'});
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
});

options.presets = [require('babel-preset-stage-2'), require('babel-preset-es2015')];
options.plugins = [powerAssert, require('babel-plugin-transform-runtime')];

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)});
Expand Down Expand Up @@ -118,3 +147,16 @@ process.on('ava-teardown', function () {
function exit() {
send('teardown');
}

function cacheKey (path) {
var key = path;

key += require('../package.json').version;
key += require('babel-core/package.json').version;
key += require('babel-plugin-espower/package.json').version;
key += require('babel-plugin-transform-runtime/package.json').version;
key += require('babel-preset-stage-2/package.json').version;
key += require('babel-preset-es2015/package.json').version;

return hasha(key);
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -105,6 +107,7 @@
"meow": "^3.6.0",
"object-assign": "^4.0.1",
"observable-to-promise": "^0.1.0",
"pkg-dir": "^1.0.0",
"plur": "^2.0.0",
"power-assert-formatter": "^1.3.0",
"power-assert-renderers": "^0.1.0",
Expand Down

0 comments on commit 4dc5c7d

Please sign in to comment.