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 22, 2015
1 parent 52d2a7a commit 8add554
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
56 changes: 43 additions & 13 deletions lib/babel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
'use strict';

var debug = require('debug')('ava');
var hasha = require('hasha');
var cacha = require('cacha');
var join = require('path').join;

var cache = cacha(join(module.paths[1], '.cache', 'ava'));

This comment has been minimized.

Copy link
@jamestalmage

jamestalmage Dec 22, 2015

Contributor

module.paths[1] - Is that a documented value? Might it be better to use find-up('package.json')?

This comment has been minimized.

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Dec 22, 2015

Member

I don't see module.paths documented there.

This comment has been minimized.

Copy link
@vadimdemedes

vadimdemedes Dec 22, 2015

Contributor

It describes the way require() looks for modules.

Node.js starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location.
If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.

So for lib/babel.js here's the list of directories, where require() would search:

  • 0 - ava/lib/node_modules
  • 1 - ava/node_modules
  • ...

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Dec 22, 2015

Member

@vdemedes Yes, I know how it works, just pointing out it might not be the best idea to rely on an undocumented property that might disappear any time, but I guess it's ok in this case since the module system is locked.

You should open an issue on Node.js getting it documented, though ;)


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

Expand Down Expand Up @@ -36,24 +41,17 @@ 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'],
plugins: ['transform-runtime'],
sourceMaps: true
};

Expand All @@ -70,11 +68,43 @@ 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, {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.plugins.push(powerAssert);

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
2 changes: 2 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 Down

0 comments on commit 8add554

Please sign in to comment.