-
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
Move Babel transforms to the main process. #349
Closed
jamestalmage
wants to merge
24
commits into
avajs:master
from
jamestalmage:move-babel-to-main-thread
Closed
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
5a66e9d
move babel to main thread
jamestalmage 5507a7d
WIP - fix tests. one still skipped. not the prettiest solution either
jamestalmage 8cb67d1
use latest nyc for proper require extension.
jamestalmage ce33e39
travis.yml: simplify piping coverage to coveralls
jamestalmage 9abb688
add caching
jamestalmage 0d88ba8
use `rimraf` so pretest is cross-platform
jamestalmage 07f27e4
don't re-require babel plugins every time buildOptions is called.
jamestalmage 4970e6b
swap `rimraf` for `del-cli`
jamestalmage be4497f
remove unnecessary nyc config
jamestalmage 748acf4
rename `assign` -> `objectAssign` to match dependency name.
jamestalmage 4db22da
delete pointless test
jamestalmage 54a8051
remove unused dependencies
jamestalmage ef4730f
compute best location for `.cache` dir.
jamestalmage 6c8f11d
test-transformer -> caching-precompiler
jamestalmage c710b64
move cacheDir logic to `caching-precompiler`
jamestalmage ca00ce9
add `--no-cache` CLI flag (squash this - always true)
jamestalmage 9c893e8
actually enable `--no-cache` (squash with above).
jamestalmage d6a8f7a
use `classic` reporter in `test-win`
jamestalmage 32df154
salt the hash
jamestalmage a3174cf
bump caching-transform
jamestalmage e80f8d3
fix npm@2: append ava's node_modules directory to _nodeModulePaths()
jamestalmage 200d194
use md5hex now that is supports arrays of values.
jamestalmage fc48e56
`noCache` -> `cache`
jamestalmage ca9f7a4
bump caching-precompiler to 1.0.0, and strip-bom sources.
jamestalmage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,117 @@ | ||
var cachingTransform = require('caching-transform'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var crypto = require('crypto'); | ||
var commonDir = require('commondir'); | ||
var pkgDir = require('pkg-dir'); | ||
var osTmpdir = require('os-tmpdir'); | ||
|
||
module.exports = CachingPrecompiler; | ||
module.exports.findCacheDir = findCacheDir; | ||
module.exports.findUniqueTempDir = findUniqueTempDir; | ||
|
||
function findCacheDir(files) { | ||
var projectBaseDirectory = pkgDir.sync(commonDir(files)); | ||
return path.join(projectBaseDirectory, 'node_modules', '.cache', 'ava'); | ||
} | ||
|
||
function findUniqueTempDir() { | ||
return path.join( | ||
osTmpdir(), | ||
Math.random().toString(35).substr(2, 30) | ||
); | ||
} | ||
|
||
function CachingPrecompiler(cacheDir) { | ||
if (!(this instanceof CachingPrecompiler)) { | ||
throw new Error('CachingPrecompiler must be called with new'); | ||
} | ||
this.cacheDir = cacheDir; | ||
this.filenameToHash = {}; | ||
this.transform = this._createTransform(); | ||
} | ||
|
||
CachingPrecompiler.prototype._factory = function (cacheDir) { | ||
// This factory method is only called once per process, and only as needed, to defer loading expensive dependencies. | ||
var babel = require('babel-core'); | ||
var convertSourceMap = require('convert-source-map'); | ||
var presetStage2 = require('babel-preset-stage-2'); | ||
var presetES2015 = require('babel-preset-es2015'); | ||
var transformRuntime = require('babel-plugin-transform-runtime'); | ||
|
||
var powerAssert = this._createEspowerPlugin(babel); | ||
|
||
function buildOptions(filename, code) { | ||
// Extract existing source maps from the code. | ||
var sourceMap = convertSourceMap.fromSource(code) || convertSourceMap.fromMapFileSource(code, path.dirname(filename)); | ||
|
||
return { | ||
presets: [presetStage2, presetES2015], | ||
plugins: [powerAssert, transformRuntime], | ||
filename: filename, | ||
sourceMaps: true, | ||
ast: false, | ||
inputSourceMap: sourceMap && sourceMap.toObject() | ||
}; | ||
} | ||
|
||
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; | ||
}; | ||
}; | ||
|
||
CachingPrecompiler.prototype._createEspowerPlugin = function (babel) { | ||
var createEspowerPlugin = require('babel-plugin-espower/create'); | ||
var enhanceAssert = require('./enhance-assert'); | ||
|
||
// initialize power-assert | ||
return createEspowerPlugin(babel, { | ||
patterns: enhanceAssert.PATTERNS | ||
}); | ||
}; | ||
|
||
CachingPrecompiler.prototype._createTransform = function () { | ||
var self = this; | ||
|
||
return cachingTransform({ | ||
factory: this._factory.bind(this), | ||
cacheDir: self.cacheDir, | ||
salt: JSON.stringify({ | ||
'babel-plugin-espower': require('babel-plugin-espower/package.json').version, | ||
'ava': require('../package.json').version, | ||
'babel-core': require('babel-core/package.json').version | ||
}), | ||
ext: '.js', | ||
hash: this._hash.bind(this) | ||
}); | ||
}; | ||
|
||
CachingPrecompiler.prototype._hash = function (code, filename, salt) { | ||
var hash = crypto | ||
.createHash('md5') | ||
.update(code, 'utf8') | ||
.update(filename || 'unknown file', 'utf8') | ||
.update(salt || '', 'utf8') | ||
.digest('hex'); | ||
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. This could use https://github.com/sindresorhus/md5-hex now. |
||
|
||
this.filenameToHash[filename] = hash; | ||
|
||
return hash; | ||
}; | ||
|
||
CachingPrecompiler.prototype.precompileFile = function (filename) { | ||
if (!this.filenameToHash[filename]) { | ||
this.transform(fs.readFileSync(filename, 'utf8'), filename); | ||
} | ||
return this.filenameToHash[filename]; | ||
}; | ||
|
||
CachingPrecompiler.prototype.generateHashForFile = function (filename) { | ||
var hash = {}; | ||
hash[filename] = this.precompileFile(filename); | ||
return hash; | ||
}; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
noCache
=>cache
. I don't like double negatives.