-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30831a8
commit c738c78
Showing
6 changed files
with
66 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,64 @@ | ||
var createEspowerPlugin = require('babel-plugin-espower/create'); | ||
var sourceMapSupport = require('source-map-support'); | ||
var babel = require('babel-core'); | ||
var Promise = require('bluebird'); | ||
var tempWrite = require('temp-write'); | ||
var transformFile = Promise.promisify(babel.transformFile); | ||
var enhanceAssert = require('./enhance-assert'); | ||
var cachingTransform = require('caching-transform'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var crypto = require('crypto'); | ||
var cacheDir = path.join(module.paths[1], '.cache', 'ava'); | ||
var filenameToHash = {}; | ||
|
||
var cache = {}; | ||
function factory(cacheDir) { | ||
var createEspowerPlugin = require('babel-plugin-espower/create'); | ||
var babel = require('babel-core'); | ||
var enhanceAssert = require('./enhance-assert'); | ||
var convertSourceMap = require('convert-source-map'); | ||
|
||
module.exports = precompile; | ||
module.exports.sync = sync; | ||
|
||
function precompile(testPath) { | ||
return cache[testPath] || (cache[testPath] = _precompile(testPath)); | ||
} | ||
|
||
function buildOptions(testPath) { | ||
// initialize power-assert | ||
var powerAssert = createEspowerPlugin(babel, { | ||
patterns: enhanceAssert.PATTERNS | ||
}); | ||
function buildOptions(filename, code) { | ||
// initialize power-assert | ||
var powerAssert = createEspowerPlugin(babel, { | ||
patterns: enhanceAssert.PATTERNS | ||
}); | ||
|
||
// if generators are not supported, use regenerator | ||
var options = { | ||
presets: [require('babel-preset-stage-2'), require('babel-preset-es2015')], | ||
plugins: [powerAssert, require('babel-plugin-transform-runtime')], | ||
sourceMaps: true, | ||
ast: false, | ||
inputSourceMap: null | ||
}; | ||
var sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename)); | ||
|
||
// try to load an input source map for the test file, in case the file was | ||
// already compiled once by the user | ||
var inputSourceMap = sourceMapSupport.retrieveSourceMap(testPath); | ||
if (inputSourceMap) { | ||
// source-map-support returns the source map as a json-encoded string, but | ||
// babel requires an actual object | ||
options.inputSourceMap = JSON.parse(inputSourceMap.map); | ||
return { | ||
presets: [require('babel-preset-stage-2'), require('babel-preset-es2015')], | ||
plugins: [powerAssert, require('babel-plugin-transform-runtime')], | ||
filename: filename, | ||
sourceMaps: true, | ||
ast: false, | ||
inputSourceMap: sourceMap && sourceMap.toObject() | ||
}; | ||
} | ||
|
||
return options; | ||
return function (code, filename, hash) { | ||
var options = buildOptions(filename, code); | ||
var result = babel.transform(code, options); | ||
var mapFile = path.join(cacheDir, hash + '.map'); | ||
fs.writeFileSync(mapFile, JSON.stringify(result.map)); | ||
return result.code; | ||
}; | ||
} | ||
|
||
function _precompile(testPath) { | ||
return transformFile(testPath, buildOptions(testPath)) | ||
.then(function (result) { | ||
return Promise.all([ | ||
tempWrite(result.code, testPath), | ||
tempWrite(JSON.stringify(result.map), testPath + '.map') | ||
]) | ||
.spread(function (tempPath, mapPath) { | ||
result.mapPath = mapPath; | ||
result.tempPath = tempPath; | ||
result.testPath = testPath; | ||
return result; | ||
}); | ||
}); | ||
} | ||
var transform = cachingTransform({ | ||
factory: factory, | ||
cacheDir: cacheDir, | ||
ext: '.js', | ||
hash: function (code, filename, salt) { | ||
var hash = crypto | ||
.createHash('md5') | ||
.update(code, 'utf8') | ||
.update(filename || 'unknown file', 'utf8') | ||
.update(salt || '', 'utf8') | ||
.digest('hex'); | ||
|
||
function sync(testPath) { | ||
var result = babel.transformFileSync(testPath, buildOptions(testPath)); | ||
result.tempPath = tempWrite.sync(result.code, testPath); | ||
result.mapPath = tempWrite.sync(JSON.stringify(result.map), testPath + '.map'); | ||
result.testPath = testPath; | ||
return result; | ||
} | ||
filenameToHash[filename] = hash; | ||
|
||
return hash; | ||
} | ||
}); | ||
|
||
module.exports = function (filename) { | ||
if (!filenameToHash[filename]) { | ||
transform(fs.readFileSync(filename, 'utf8'), filename); | ||
} | ||
return filenameToHash[filename]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters