1
1
'use strict' ;
2
2
3
+ var cacheDir = require ( 'xdg-basedir' ) . cache ;
4
+ var tmpDir = require ( 'os-tmpdir' ) ;
3
5
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' ) ) ;
4
11
5
12
var opts = JSON . parse ( process . argv [ 2 ] ) ;
6
13
@@ -36,24 +43,17 @@ sourceMapSupport.install({
36
43
}
37
44
} ) ;
38
45
39
- var createEspowerPlugin = require ( 'babel-plugin-espower/create' ) ;
40
46
var requireFromString = require ( 'require-from-string' ) ;
41
47
var loudRejection = require ( 'loud-rejection/api' ) ( process ) ;
42
48
var serializeError = require ( 'serialize-error' ) ;
43
- var babel = require ( 'babel-core' ) ;
44
49
var send = require ( './send' ) ;
45
50
46
51
var testPath = opts . file ;
47
52
48
- // initialize power-assert
49
- var powerAssert = createEspowerPlugin ( babel , {
50
- patterns : require ( './enhance-assert' ) . PATTERNS
51
- } ) ;
52
-
53
53
// if generators are not supported, use regenerator
54
54
var options = {
55
55
presets : [ 'stage-2' , 'es2015' ] ,
56
- plugins : [ powerAssert , 'transform-runtime' ] ,
56
+ plugins : [ 'transform-runtime' ] ,
57
57
sourceMaps : true
58
58
} ;
59
59
@@ -70,11 +70,43 @@ if (inputSourceMap) {
70
70
}
71
71
72
72
// 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
+ }
78
110
79
111
process . on ( 'uncaughtException' , function ( exception ) {
80
112
send ( 'uncaughtException' , { exception : serializeError ( exception ) } ) ;
0 commit comments