1
1
'use strict' ;
2
2
3
+ var pkgDir = require ( 'pkg-dir' ) . sync ;
3
4
var debug = require ( 'debug' ) ( 'ava' ) ;
5
+ var hasha = require ( 'hasha' ) ;
6
+ var cacha = require ( 'cacha' ) ;
7
+ var path = require ( 'path' ) ;
4
8
5
9
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' ) ) ;
6
13
7
14
if ( debug . enabled ) {
8
15
// Forward the `time-require` `--sorted` flag.
@@ -33,24 +40,13 @@ sourceMapSupport.install({
33
40
}
34
41
} ) ;
35
42
36
- var createEspowerPlugin = require ( 'babel-plugin-espower/create' ) ;
37
43
var requireFromString = require ( 'require-from-string' ) ;
38
44
var loudRejection = require ( 'loud-rejection/api' ) ( process ) ;
39
45
var serializeError = require ( 'serialize-error' ) ;
40
- var babel = require ( 'babel-core' ) ;
41
46
var send = require ( './send' ) ;
42
47
43
- var testPath = opts . file ;
44
-
45
- // initialize power-assert
46
- var powerAssert = createEspowerPlugin ( babel , {
47
- patterns : require ( './enhance-assert' ) . PATTERNS
48
- } ) ;
49
-
50
48
// if generators are not supported, use regenerator
51
49
var options = {
52
- presets : [ require ( 'babel-preset-stage-2' ) , require ( 'babel-preset-es2015' ) ] ,
53
- plugins : [ powerAssert , require ( 'babel-plugin-transform-runtime' ) ] ,
54
50
sourceMaps : true
55
51
} ;
56
52
@@ -67,11 +63,44 @@ if (inputSourceMap) {
67
63
}
68
64
69
65
// 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
+ }
75
104
76
105
process . on ( 'uncaughtException' , function ( exception ) {
77
106
send ( 'uncaughtException' , { exception : serializeError ( exception ) } ) ;
@@ -118,3 +147,16 @@ process.on('ava-teardown', function () {
118
147
function exit ( ) {
119
148
send ( 'teardown' ) ;
120
149
}
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
+ }
0 commit comments