Created
December 24, 2015 00:21
-
-
Save kapitancho/15321bc580a3b2fcb835 to your computer and use it in GitHub Desktop.
RequireJS plugin for es6 + babel 6.
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
define(['module'], function (module) { | |
'use strict'; | |
var fetchText = function(url, callback) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', url, true); | |
xhr.onreadystatechange = function(evt) { | |
if (xhr.readyState === 4) { | |
callback(xhr.responseText); | |
} | |
}; | |
xhr.send(null); | |
}; | |
return { | |
load: function (name, req, onload, config) { | |
var url = req.toUrl(name + '.js'); | |
fetchText(url, function(text) { | |
try { | |
var code = Babel.transform(text, { | |
presets: ['es2015'], | |
plugins: ['transform-es2015-modules-amd'] | |
}).code; | |
} catch (err) { | |
onload.error(err); | |
} | |
onload.fromText(code); | |
}); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment