-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache babel-transpiled code #352
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
'use strict'; | ||
|
||
var pkgDir = require('pkg-dir').sync; | ||
var debug = require('debug')('ava'); | ||
var hasha = require('hasha'); | ||
var cacha = require('cacha'); | ||
var path = require('path'); | ||
|
||
var opts = JSON.parse(process.argv[2]); | ||
var testPath = opts.file; | ||
|
||
var cache = cacha(path.join(pkgDir(path.dirname(testPath)), 'node_modules', '.cache', 'ava')); | ||
|
||
if (debug.enabled) { | ||
// Forward the `time-require` `--sorted` flag. | ||
|
@@ -33,24 +40,13 @@ sourceMapSupport.install({ | |
} | ||
}); | ||
|
||
var createEspowerPlugin = require('babel-plugin-espower/create'); | ||
var requireFromString = require('require-from-string'); | ||
var loudRejection = require('loud-rejection/api')(process); | ||
var serializeError = require('serialize-error'); | ||
var babel = require('babel-core'); | ||
var send = require('./send'); | ||
|
||
var testPath = opts.file; | ||
|
||
// initialize power-assert | ||
var powerAssert = createEspowerPlugin(babel, { | ||
patterns: require('./enhance-assert').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 | ||
}; | ||
|
||
|
@@ -67,11 +63,44 @@ if (inputSourceMap) { | |
} | ||
|
||
// include test file | ||
var transpiled = babel.transformFileSync(testPath, options); | ||
sourceMapCache[testPath] = transpiled.map; | ||
requireFromString(transpiled.code, testPath, { | ||
appendPaths: module.paths | ||
}); | ||
var cachePath = hasha(cacheKey(testPath)); | ||
var hashPath = cachePath + '_hash'; | ||
|
||
var prevHash = cache.getSync(hashPath, {encoding: 'utf8'}); | ||
var currHash = hasha.fromFileSync(testPath); | ||
|
||
if (prevHash === currHash) { | ||
var cached = JSON.parse(cache.getSync(cachePath)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure it is even necessary to make the filename part of the hash. If two files have identical content, it doesn't matter that they are located at different points on the file system - they will transpile identically right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually scratch that. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jamestalmage Yes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. Thanks. |
||
|
||
sourceMapCache[testPath] = cached.map; | ||
requireFromString(cached.code, testPath, { | ||
appendPaths: module.paths | ||
}); | ||
} else { | ||
var createEspowerPlugin = require('babel-plugin-espower/create'); | ||
var babel = require('babel-core'); | ||
|
||
// initialize power-assert | ||
var powerAssert = createEspowerPlugin(babel, { | ||
patterns: require('./enhance-assert').PATTERNS | ||
}); | ||
|
||
options.presets = [require('babel-preset-stage-2'), require('babel-preset-es2015')]; | ||
options.plugins = [powerAssert, require('babel-plugin-transform-runtime')]; | ||
|
||
var transpiled = babel.transformFileSync(testPath, options); | ||
|
||
cache.setSync(hashPath, currHash); | ||
cache.setSync(cachePath, JSON.stringify({ | ||
code: transpiled.code, | ||
map: transpiled.map | ||
})); | ||
|
||
sourceMapCache[testPath] = transpiled.map; | ||
requireFromString(transpiled.code, testPath, { | ||
appendPaths: module.paths | ||
}); | ||
} | ||
|
||
process.on('uncaughtException', function (exception) { | ||
send('uncaughtException', {exception: serializeError(exception)}); | ||
|
@@ -118,3 +147,16 @@ process.on('ava-teardown', function () { | |
function exit() { | ||
send('teardown'); | ||
} | ||
|
||
function cacheKey(path) { | ||
var key = path; | ||
|
||
key += require('../package.json').version; | ||
key += require('babel-core/package.json').version; | ||
key += require('babel-plugin-espower/package.json').version; | ||
key += require('babel-plugin-transform-runtime/package.json').version; | ||
key += require('babel-preset-stage-2/package.json').version; | ||
key += require('babel-preset-es2015/package.json').version; | ||
|
||
return hasha(key); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, the cache is being stored in
node_modules/.cache/ava
, but later down this file versions from ava & babel-* packages are being included in the cache key.That way, when users
rm -rf node_modules/.cache/ava
, they can be sure they removed all the cache. And they won't need to get the AVA version to removeava-0.8.0
(for example).