Created
August 12, 2014 03:50
-
-
Save ericclemmons/04fdff2ee96297a9dff4 to your computer and use it in GitHub Desktop.
JSTransform require.extension to auto-magically convert ES6 to ES5 during runtime.
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
var fs = require('fs'); | |
var jstransform = require('jstransform'); | |
var path = require('path'); | |
var js = require.extensions['.js']; | |
module.exports = function(visitors) { | |
visitors = visitors.reduce(function(visitors, visitor) { | |
return visitors.concat(visitor.visitorList); | |
}, []); | |
require.extensions['.js'] = function(module, filename) { | |
if (filename.match(/node_modules/)) { | |
return js(module, filename); | |
} | |
var before = fs.readFileSync(filename, 'utf8'); | |
var after = jstransform.transform(visitors, before).code; | |
return module._compile(after, 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
require('./lib/jstransform-require')([ | |
require('jstransform/visitors/es6-arrow-function-visitors'), | |
require('jstransform/visitors/es6-class-visitors'), | |
require('jstransform/visitors/es6-destructuring-visitors'), | |
require('jstransform/visitors/es6-object-short-notation-visitors'), | |
require('jstransform/visitors/es6-rest-param-visitors'), | |
require('jstransform/visitors/es7-spread-property-visitors'), | |
require('jstransform/visitors/es6-template-visitors'), | |
]); | |
var debug = require('debug')('app'); | |
var app = require('./app'); | |
app.set('port', process.env.PORT || 3000); | |
var server = app.listen(app.get('port'), function() { | |
debug('Express server listening on port ' + server.address().port); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Aw yeah, that's awesome.