-
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
Transpile in the main process #945
Closed
Closed
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d34451e
store a list of dependencies.
jamestalmage 6e04d1c
transpile in the main process
jamestalmage ccf2205
don't try to precompile core modules
jamestalmage 00ae6f3
don't precompile json
jamestalmage 93b79b0
allow missing sourcemaps
jamestalmage 8db1c04
bump babel-plugin-detective so we handle es2015 re-exports
jamestalmage 0633a68
Add some debugging code.
jamestalmage 161b939
document experimental features.
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,111 @@ | ||
var path = require('path'); | ||
var resolveFrom = require('resolve-from'); | ||
var resolve = require('resolve'); | ||
var md5hex = require('md5-hex'); | ||
var detective = require('babel-plugin-detective'); | ||
var fs = require('graceful-fs'); | ||
|
||
var base = path.join(__dirname, '..', 'index.js'); | ||
|
||
var _babelCore; | ||
|
||
function babelCore() { | ||
if (!_babelCore) { | ||
_babelCore = require('babel-core'); | ||
} | ||
return _babelCore; | ||
} | ||
|
||
function Precompiler(cacheDir) { | ||
this._cacheDir = cacheDir; | ||
this._cache = {}; | ||
} | ||
|
||
Precompiler.prototype.cachePath = function (filename) { | ||
return path.join(this._cacheDir, filename); | ||
}; | ||
|
||
Precompiler.prototype.buildEntry = function (filename) { | ||
if (this._cache[filename]) { | ||
return; | ||
} | ||
|
||
var entry = this._cache[filename] = {}; | ||
|
||
var result = babelCore().transformFileSync(filename, { | ||
plugins: [detective] | ||
}); | ||
|
||
var hash = md5hex(result.code); | ||
fs.writeFileSync(this.cachePath(hash + '.js'), result.code); | ||
|
||
var metadata = detective.metadata(result); | ||
|
||
var dependencies = this.normalizeDependencies(filename, metadata); | ||
|
||
entry.hash = hash; | ||
entry.dependencies = dependencies; | ||
|
||
dependencies.forEach(this.buildEntry, this); | ||
}; | ||
|
||
Precompiler.prototype.normalizeDependencies = function (filename, metadata) { | ||
if (metadata && metadata.expressions && (metadata.expressions === true || metadata.expressions.length)) { | ||
console.warn(filename + ' has a dynamic require - precompilation may not work'); | ||
} | ||
|
||
if (!metadata || !metadata.strings || !metadata.strings.length) { | ||
return []; | ||
} | ||
|
||
var dir = path.dirname(filename); | ||
|
||
return metadata.strings | ||
.filter(function (dep) { | ||
return !resolve.isCore(dep); | ||
}) | ||
.map(function (dep) { | ||
return resolveFrom(dir, dep); | ||
}) | ||
.filter(Boolean) | ||
.filter(this.shouldTranspile, this); | ||
}; | ||
|
||
Precompiler.prototype.shouldTranspile = function (filename) { | ||
return ( | ||
(filename !== base) && | ||
!/[\/\\]node_modules[\/\\]/.test(filename) && | ||
/\.js$/.test(filename) | ||
); | ||
}; | ||
|
||
Precompiler.prototype.createHash = function (filename, hash, metadata) { | ||
var hashMap = {}; | ||
hashMap[filename] = hash; | ||
|
||
var dependencies = this.normalizeDependencies(filename, metadata); | ||
|
||
dependencies.forEach(this.buildEntry, this); | ||
|
||
dependencies.forEach(function (filename) { | ||
this.attach(filename, hashMap); | ||
}, this); | ||
|
||
return hashMap; | ||
}; | ||
|
||
Precompiler.prototype.attach = function (filename, hashMap) { | ||
if (hashMap[filename] || !this._cache[filename]) { | ||
return; | ||
} | ||
|
||
var entry = this._cache[filename]; | ||
|
||
hashMap[filename] = entry.hash; | ||
|
||
entry.dependencies.forEach(function (filename) { | ||
this.attach(filename, hashMap); | ||
}, this); | ||
}; | ||
|
||
module.exports = Precompiler; |
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
Oops, something went wrong.
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.
I think if you don't merge this with the babelConfig already defined, this will implicitly be merged with the
.babelrc
.Which is totally cool if the babel option is set to "inherit". Otherwise, metadata generation might fail.
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.
See this repo failing with this config
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.
Yes. That's the idea. That is only precompiling your sources. We already precompile your tests in
caching-precompiler
.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.
@geowarin - see https://github.com/geowarin/repro-ava-precompile/issues/1 to fix yours.