|
1 |
| -var createEspowerPlugin = require('babel-plugin-espower/create'); |
2 |
| -var sourceMapSupport = require('source-map-support'); |
3 |
| -var babel = require('babel-core'); |
4 |
| -var Promise = require('bluebird'); |
5 |
| -var tempWrite = require('temp-write'); |
6 |
| -var transformFile = Promise.promisify(babel.transformFile); |
7 |
| -var enhanceAssert = require('./enhance-assert'); |
| 1 | +var cachingTransform = require('caching-transform'); |
| 2 | +var fs = require('fs'); |
| 3 | +var path = require('path'); |
| 4 | +var crypto = require('crypto'); |
| 5 | +var cacheDir = path.join(module.paths[1], '.cache', 'ava'); |
| 6 | +var filenameToHash = {}; |
8 | 7 |
|
9 |
| -var cache = {}; |
| 8 | +function factory(cacheDir) { |
| 9 | + var createEspowerPlugin = require('babel-plugin-espower/create'); |
| 10 | + var babel = require('babel-core'); |
| 11 | + var enhanceAssert = require('./enhance-assert'); |
| 12 | + var convertSourceMap = require('convert-source-map'); |
10 | 13 |
|
11 |
| -module.exports = precompile; |
12 |
| -module.exports.sync = sync; |
13 |
| - |
14 |
| -function precompile(testPath) { |
15 |
| - return cache[testPath] || (cache[testPath] = _precompile(testPath)); |
16 |
| -} |
17 |
| - |
18 |
| -function buildOptions(testPath) { |
19 |
| - // initialize power-assert |
20 |
| - var powerAssert = createEspowerPlugin(babel, { |
21 |
| - patterns: enhanceAssert.PATTERNS |
22 |
| - }); |
| 14 | + function buildOptions(filename, code) { |
| 15 | + // initialize power-assert |
| 16 | + var powerAssert = createEspowerPlugin(babel, { |
| 17 | + patterns: enhanceAssert.PATTERNS |
| 18 | + }); |
23 | 19 |
|
24 |
| - // if generators are not supported, use regenerator |
25 |
| - var options = { |
26 |
| - presets: [require('babel-preset-stage-2'), require('babel-preset-es2015')], |
27 |
| - plugins: [powerAssert, require('babel-plugin-transform-runtime')], |
28 |
| - sourceMaps: true, |
29 |
| - ast: false, |
30 |
| - inputSourceMap: null |
31 |
| - }; |
| 20 | + var sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename)); |
32 | 21 |
|
33 |
| - // try to load an input source map for the test file, in case the file was |
34 |
| - // already compiled once by the user |
35 |
| - var inputSourceMap = sourceMapSupport.retrieveSourceMap(testPath); |
36 |
| - if (inputSourceMap) { |
37 |
| - // source-map-support returns the source map as a json-encoded string, but |
38 |
| - // babel requires an actual object |
39 |
| - options.inputSourceMap = JSON.parse(inputSourceMap.map); |
| 22 | + return { |
| 23 | + presets: [require('babel-preset-stage-2'), require('babel-preset-es2015')], |
| 24 | + plugins: [powerAssert, require('babel-plugin-transform-runtime')], |
| 25 | + filename: filename, |
| 26 | + sourceMaps: true, |
| 27 | + ast: false, |
| 28 | + inputSourceMap: sourceMap && sourceMap.toObject() |
| 29 | + }; |
40 | 30 | }
|
41 | 31 |
|
42 |
| - return options; |
| 32 | + return function (code, filename, hash) { |
| 33 | + var options = buildOptions(filename, code); |
| 34 | + var result = babel.transform(code, options); |
| 35 | + var mapFile = path.join(cacheDir, hash + '.map'); |
| 36 | + fs.writeFileSync(mapFile, JSON.stringify(result.map)); |
| 37 | + return result.code; |
| 38 | + }; |
43 | 39 | }
|
44 | 40 |
|
45 |
| -function _precompile(testPath) { |
46 |
| - return transformFile(testPath, buildOptions(testPath)) |
47 |
| - .then(function (result) { |
48 |
| - return Promise.all([ |
49 |
| - tempWrite(result.code, testPath), |
50 |
| - tempWrite(JSON.stringify(result.map), testPath + '.map') |
51 |
| - ]) |
52 |
| - .spread(function (tempPath, mapPath) { |
53 |
| - result.mapPath = mapPath; |
54 |
| - result.tempPath = tempPath; |
55 |
| - result.testPath = testPath; |
56 |
| - return result; |
57 |
| - }); |
58 |
| - }); |
59 |
| -} |
| 41 | +var transform = cachingTransform({ |
| 42 | + factory: factory, |
| 43 | + cacheDir: cacheDir, |
| 44 | + ext: '.js', |
| 45 | + hash: function (code, filename, salt) { |
| 46 | + var hash = crypto |
| 47 | + .createHash('md5') |
| 48 | + .update(code, 'utf8') |
| 49 | + .update(filename || 'unknown file', 'utf8') |
| 50 | + .update(salt || '', 'utf8') |
| 51 | + .digest('hex'); |
60 | 52 |
|
61 |
| -function sync(testPath) { |
62 |
| - var result = babel.transformFileSync(testPath, buildOptions(testPath)); |
63 |
| - result.tempPath = tempWrite.sync(result.code, testPath); |
64 |
| - result.mapPath = tempWrite.sync(JSON.stringify(result.map), testPath + '.map'); |
65 |
| - result.testPath = testPath; |
66 |
| - return result; |
67 |
| -} |
| 53 | + filenameToHash[filename] = hash; |
68 | 54 |
|
| 55 | + return hash; |
| 56 | + } |
| 57 | +}); |
| 58 | + |
| 59 | +module.exports = function (filename) { |
| 60 | + if (!filenameToHash[filename]) { |
| 61 | + transform(fs.readFileSync(filename, 'utf8'), filename); |
| 62 | + } |
| 63 | + return filenameToHash[filename]; |
| 64 | +}; |
0 commit comments