Skip to content

Commit d4d4377

Browse files
author
vdemedes
committed
cache babel-transpiled code
1 parent 52d2a7a commit d4d4377

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

lib/babel.js

+45-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
'use strict';
22

3+
var cacheDir = require('xdg-basedir').cache;
4+
var tmpDir = require('os-tmpdir');
35
var debug = require('debug')('ava');
6+
var hasha = require('hasha');
7+
var cacha = require('cacha');
8+
var join = require('path').join;
9+
10+
var cache = cacha(join(cacheDir || tmpdir(), 'ava'));
411

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

@@ -36,24 +43,17 @@ sourceMapSupport.install({
3643
}
3744
});
3845

39-
var createEspowerPlugin = require('babel-plugin-espower/create');
4046
var requireFromString = require('require-from-string');
4147
var loudRejection = require('loud-rejection/api')(process);
4248
var serializeError = require('serialize-error');
43-
var babel = require('babel-core');
4449
var send = require('./send');
4550

4651
var testPath = opts.file;
4752

48-
// initialize power-assert
49-
var powerAssert = createEspowerPlugin(babel, {
50-
patterns: require('./enhance-assert').PATTERNS
51-
});
52-
5353
// if generators are not supported, use regenerator
5454
var options = {
5555
presets: ['stage-2', 'es2015'],
56-
plugins: [powerAssert, 'transform-runtime'],
56+
plugins: ['transform-runtime'],
5757
sourceMaps: true
5858
};
5959

@@ -70,11 +70,43 @@ if (inputSourceMap) {
7070
}
7171

7272
// include test file
73-
var transpiled = babel.transformFileSync(testPath, options);
74-
sourceMapCache[testPath] = transpiled.map;
75-
requireFromString(transpiled.code, testPath, {
76-
appendPaths: module.paths
77-
});
73+
var cachePath = hasha(testPath);
74+
var hashPath = hasha(testPath) + '_hash';
75+
76+
var prevHash = cache.getSync(hashPath, {encoding: 'utf8'});
77+
var currHash = hasha.fromFileSync(testPath);
78+
79+
if (prevHash === currHash) {
80+
var cached = JSON.parse(cache.getSync(cachePath));
81+
82+
sourceMapCache[testPath] = cached.map;
83+
requireFromString(cached.code, testPath, {
84+
appendPaths: module.paths
85+
});
86+
} else {
87+
var createEspowerPlugin = require('babel-plugin-espower/create');
88+
var babel = require('babel-core');
89+
90+
// initialize power-assert
91+
var powerAssert = createEspowerPlugin(babel, {
92+
patterns: require('./enhance-assert').PATTERNS
93+
});
94+
95+
options.plugins.push(powerAssert);
96+
97+
var transpiled = babel.transformFileSync(testPath, options);
98+
99+
cache.setSync(hashPath, currHash);
100+
cache.setSync(cachePath, JSON.stringify({
101+
code: transpiled.code,
102+
map: transpiled.map
103+
}));
104+
105+
sourceMapCache[testPath] = transpiled.map;
106+
requireFromString(transpiled.code, testPath, {
107+
appendPaths: module.paths
108+
});
109+
}
78110

79111
process.on('uncaughtException', function (exception) {
80112
send('uncaughtException', {exception: serializeError(exception)});

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
"babel-preset-stage-2": "^6.3.13",
8989
"babel-runtime": "^6.3.19",
9090
"bluebird": "^3.0.0",
91+
"cacha": "^1.0.3",
9192
"chalk": "^1.0.0",
9293
"co-with-promise": "^4.6.0",
9394
"core-assert": "^0.1.0",
@@ -97,6 +98,7 @@
9798
"figures": "^1.4.0",
9899
"fn-name": "^2.0.0",
99100
"globby": "^4.0.0",
101+
"hasha": "^2.0.2",
100102
"is-generator-fn": "^1.0.0",
101103
"is-observable": "^0.1.0",
102104
"is-promise": "^2.1.0",
@@ -105,6 +107,7 @@
105107
"meow": "^3.6.0",
106108
"object-assign": "^4.0.1",
107109
"observable-to-promise": "^0.1.0",
110+
"os-tmpdir": "^1.0.1",
108111
"plur": "^2.0.0",
109112
"power-assert-formatter": "^1.3.0",
110113
"power-assert-renderers": "^0.1.0",
@@ -116,7 +119,8 @@
116119
"source-map-support": "^0.4.0",
117120
"squeak": "^1.2.0",
118121
"time-require": "^0.1.2",
119-
"update-notifier": "^0.5.0"
122+
"update-notifier": "^0.5.0",
123+
"xdg-basedir": "^2.0.0"
120124
},
121125
"devDependencies": {
122126
"coveralls": "^2.11.4",

0 commit comments

Comments
 (0)