|
1 | 1 | 'use strict'; |
2 | 2 | var path = require('path'); |
| 3 | +var fs = require('fs'); |
3 | 4 | var debug = require('debug')('ava'); |
4 | | -var pkgDir = require('pkg-dir').sync; |
5 | | -var hasha = require('hasha'); |
6 | | -var cacha = require('cacha'); |
7 | 5 | var sourceMapSupport = require('source-map-support'); |
8 | 6 |
|
9 | 7 | var opts = JSON.parse(process.argv[2]); |
10 | 8 | var testPath = opts.file; |
11 | 9 |
|
12 | | -var cache = cacha(path.join(pkgDir(path.dirname(testPath)), 'node_modules', '.cache', 'ava')); |
13 | | - |
14 | 10 | if (debug.enabled) { |
15 | 11 | // Forward the `time-require` `--sorted` flag. |
16 | 12 | // Intended for internal optimization tests only. |
@@ -38,73 +34,40 @@ sourceMapSupport.install({ |
38 | 34 | if (sourceMapCache[source]) { |
39 | 35 | return { |
40 | 36 | url: source, |
41 | | - map: sourceMapCache[source] |
| 37 | + map: fs.readFileSync(sourceMapCache[source], 'utf8') |
42 | 38 | }; |
43 | 39 | } |
44 | 40 | } |
45 | 41 | }); |
46 | 42 |
|
47 | | -var requireFromString = require('require-from-string'); |
48 | 43 | var loudRejection = require('loud-rejection/api')(process); |
49 | 44 | var serializeError = require('serialize-error'); |
50 | 45 | var send = require('./send'); |
51 | | - |
52 | | -// if generators are not supported, use regenerator |
53 | | -var options = { |
54 | | - sourceMaps: true |
55 | | -}; |
| 46 | +var installPrecompiler = require('require-precompiled'); |
| 47 | +var cacheDir = opts.cacheDir; |
56 | 48 |
|
57 | 49 | // check if test files required ava and show error, when they didn't |
58 | 50 | exports.avaRequired = false; |
59 | 51 |
|
60 | | -// try to load an input source map for the test file, in case the file was |
61 | | -// already compiled once by the user |
62 | | -var inputSourceMap = sourceMapSupport.retrieveSourceMap(testPath); |
63 | | -if (inputSourceMap) { |
64 | | - // source-map-support returns the source map as a json-encoded string, but |
65 | | - // babel requires an actual object |
66 | | - options.inputSourceMap = JSON.parse(inputSourceMap.map); |
67 | | -} |
68 | | - |
69 | | -// include test file |
70 | | -var cachePath = hasha(cacheKey(testPath)); |
71 | | -var hashPath = cachePath + '_hash'; |
72 | | - |
73 | | -var prevHash = cache.getSync(hashPath, {encoding: 'utf8'}); |
74 | | -var currHash = hasha.fromFileSync(testPath); |
75 | | - |
76 | | -if (prevHash === currHash) { |
77 | | - var cached = JSON.parse(cache.getSync(cachePath)); |
78 | | - |
79 | | - sourceMapCache[testPath] = cached.map; |
80 | | - requireFromString(cached.code, testPath, { |
81 | | - appendPaths: module.paths |
82 | | - }); |
83 | | -} else { |
84 | | - var createEspowerPlugin = require('babel-plugin-espower/create'); |
85 | | - var babel = require('babel-core'); |
86 | | - |
87 | | - // initialize power-assert |
88 | | - var powerAssert = createEspowerPlugin(babel, { |
89 | | - patterns: require('./enhance-assert').PATTERNS |
90 | | - }); |
91 | | - |
92 | | - options.presets = [require('babel-preset-stage-2'), require('babel-preset-es2015')]; |
93 | | - options.plugins = [powerAssert, require('babel-plugin-transform-runtime')]; |
| 52 | +installPrecompiler(function (filename) { |
| 53 | + var precompiled = opts.precompiled[filename]; |
| 54 | + if (precompiled) { |
| 55 | + sourceMapCache[filename] = path.join(cacheDir, precompiled + '.map'); |
| 56 | + return fs.readFileSync(path.join(cacheDir, precompiled + '.js'), 'utf8'); |
| 57 | + } |
| 58 | + return null; |
| 59 | +}); |
94 | 60 |
|
95 | | - var transpiled = babel.transformFileSync(testPath, options); |
| 61 | +// Modules need to be able to find `babel-runtime`, which is nested in our node_modules w/ npm@2 |
| 62 | +var nodeModulesDir = path.join(__dirname, '../node_modules'); |
| 63 | +var oldNodeModulesPaths = module.constructor._nodeModulePaths; |
| 64 | +module.constructor._nodeModulePaths = function () { |
| 65 | + var ret = oldNodeModulesPaths.apply(this, arguments); |
| 66 | + ret.push(nodeModulesDir); |
| 67 | + return ret; |
| 68 | +}; |
96 | 69 |
|
97 | | - cache.setSync(hashPath, currHash); |
98 | | - cache.setSync(cachePath, JSON.stringify({ |
99 | | - code: transpiled.code, |
100 | | - map: transpiled.map |
101 | | - })); |
102 | | - |
103 | | - sourceMapCache[testPath] = transpiled.map; |
104 | | - requireFromString(transpiled.code, testPath, { |
105 | | - appendPaths: module.paths |
106 | | - }); |
107 | | -} |
| 70 | +require(testPath); |
108 | 71 |
|
109 | 72 | process.on('uncaughtException', function (exception) { |
110 | 73 | send('uncaughtException', {exception: serializeError(exception)}); |
@@ -151,16 +114,3 @@ process.on('ava-teardown', function () { |
151 | 114 | function exit() { |
152 | 115 | send('teardown'); |
153 | 116 | } |
154 | | - |
155 | | -function cacheKey(path) { |
156 | | - var key = path; |
157 | | - |
158 | | - key += require('../package.json').version; |
159 | | - key += require('babel-core/package.json').version; |
160 | | - key += require('babel-plugin-espower/package.json').version; |
161 | | - key += require('babel-plugin-transform-runtime/package.json').version; |
162 | | - key += require('babel-preset-stage-2/package.json').version; |
163 | | - key += require('babel-preset-es2015/package.json').version; |
164 | | - |
165 | | - return hasha(key); |
166 | | -} |
0 commit comments