Skip to content

Commit 865cc05

Browse files
committed
Merge pull request #352 from sindresorhus/cache-babel
Cache babel-transpiled code
2 parents daca89d + 0daec3e commit 865cc05

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

lib/babel.js

+58-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
'use strict';
22

3+
var pkgDir = require('pkg-dir').sync;
34
var debug = require('debug')('ava');
5+
var hasha = require('hasha');
6+
var cacha = require('cacha');
7+
var path = require('path');
48

59
var opts = JSON.parse(process.argv[2]);
10+
var testPath = opts.file;
11+
12+
var cache = cacha(path.join(pkgDir(path.dirname(testPath)), 'node_modules', '.cache', 'ava'));
613

714
if (debug.enabled) {
815
// Forward the `time-require` `--sorted` flag.
@@ -33,24 +40,13 @@ sourceMapSupport.install({
3340
}
3441
});
3542

36-
var createEspowerPlugin = require('babel-plugin-espower/create');
3743
var requireFromString = require('require-from-string');
3844
var loudRejection = require('loud-rejection/api')(process);
3945
var serializeError = require('serialize-error');
40-
var babel = require('babel-core');
4146
var send = require('./send');
4247

43-
var testPath = opts.file;
44-
45-
// initialize power-assert
46-
var powerAssert = createEspowerPlugin(babel, {
47-
patterns: require('./enhance-assert').PATTERNS
48-
});
49-
5048
// if generators are not supported, use regenerator
5149
var options = {
52-
presets: [require('babel-preset-stage-2'), require('babel-preset-es2015')],
53-
plugins: [powerAssert, require('babel-plugin-transform-runtime')],
5450
sourceMaps: true
5551
};
5652

@@ -67,11 +63,44 @@ if (inputSourceMap) {
6763
}
6864

6965
// include test file
70-
var transpiled = babel.transformFileSync(testPath, options);
71-
sourceMapCache[testPath] = transpiled.map;
72-
requireFromString(transpiled.code, testPath, {
73-
appendPaths: module.paths
74-
});
66+
var cachePath = hasha(cacheKey(testPath));
67+
var hashPath = cachePath + '_hash';
68+
69+
var prevHash = cache.getSync(hashPath, {encoding: 'utf8'});
70+
var currHash = hasha.fromFileSync(testPath);
71+
72+
if (prevHash === currHash) {
73+
var cached = JSON.parse(cache.getSync(cachePath));
74+
75+
sourceMapCache[testPath] = cached.map;
76+
requireFromString(cached.code, testPath, {
77+
appendPaths: module.paths
78+
});
79+
} else {
80+
var createEspowerPlugin = require('babel-plugin-espower/create');
81+
var babel = require('babel-core');
82+
83+
// initialize power-assert
84+
var powerAssert = createEspowerPlugin(babel, {
85+
patterns: require('./enhance-assert').PATTERNS
86+
});
87+
88+
options.presets = [require('babel-preset-stage-2'), require('babel-preset-es2015')];
89+
options.plugins = [powerAssert, require('babel-plugin-transform-runtime')];
90+
91+
var transpiled = babel.transformFileSync(testPath, options);
92+
93+
cache.setSync(hashPath, currHash);
94+
cache.setSync(cachePath, JSON.stringify({
95+
code: transpiled.code,
96+
map: transpiled.map
97+
}));
98+
99+
sourceMapCache[testPath] = transpiled.map;
100+
requireFromString(transpiled.code, testPath, {
101+
appendPaths: module.paths
102+
});
103+
}
75104

76105
process.on('uncaughtException', function (exception) {
77106
send('uncaughtException', {exception: serializeError(exception)});
@@ -118,3 +147,16 @@ process.on('ava-teardown', function () {
118147
function exit() {
119148
send('teardown');
120149
}
150+
151+
function cacheKey(path) {
152+
var key = path;
153+
154+
key += require('../package.json').version;
155+
key += require('babel-core/package.json').version;
156+
key += require('babel-plugin-espower/package.json').version;
157+
key += require('babel-plugin-transform-runtime/package.json').version;
158+
key += require('babel-preset-stage-2/package.json').version;
159+
key += require('babel-preset-es2015/package.json').version;
160+
161+
return hasha(key);
162+
}

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"babel-preset-stage-2": "^6.3.13",
8787
"babel-runtime": "^6.3.19",
8888
"bluebird": "^3.0.0",
89+
"cacha": "^1.0.3",
8990
"chalk": "^1.0.0",
9091
"co-with-promise": "^4.6.0",
9192
"core-assert": "^0.1.0",
@@ -95,6 +96,7 @@
9596
"figures": "^1.4.0",
9697
"fn-name": "^2.0.0",
9798
"globby": "^4.0.0",
99+
"hasha": "^2.0.2",
98100
"is-generator-fn": "^1.0.0",
99101
"is-observable": "^0.1.0",
100102
"is-promise": "^2.1.0",
@@ -103,6 +105,7 @@
103105
"meow": "^3.6.0",
104106
"object-assign": "^4.0.1",
105107
"observable-to-promise": "^0.1.0",
108+
"pkg-dir": "^1.0.0",
106109
"plur": "^2.0.0",
107110
"power-assert-formatter": "^1.3.0",
108111
"power-assert-renderers": "^0.1.0",

0 commit comments

Comments
 (0)