2016-05-04 03:37:32 +08:00
|
|
|
// Copied from https://github.com/babel/grunt-babel to ensure we always
|
|
|
|
// use the `babel-core` defined in our own package.json as opposed to the
|
|
|
|
// grunt-babel dependency's
|
2016-05-07 07:46:15 +08:00
|
|
|
module.exports = function (grunt) {
|
|
|
|
grunt.registerMultiTask('babel', 'Use next generation JavaScript, today', function () {
|
2016-05-11 10:08:41 +08:00
|
|
|
var path = require('path');
|
|
|
|
var babel = require('babel-core');
|
2016-05-07 07:46:15 +08:00
|
|
|
var options = this.options();
|
2016-05-04 03:37:32 +08:00
|
|
|
|
2016-05-07 07:46:15 +08:00
|
|
|
this.files.forEach(function (el) {
|
2016-05-04 03:37:32 +08:00
|
|
|
delete options.filename;
|
|
|
|
delete options.filenameRelative;
|
|
|
|
|
|
|
|
options.sourceFileName = path.relative(path.dirname(el.dest), el.src[0]);
|
|
|
|
|
|
|
|
if (process.platform === 'win32') {
|
|
|
|
options.sourceFileName = options.sourceFileName.replace(/\\/g, '/');
|
|
|
|
}
|
|
|
|
|
|
|
|
options.sourceMapTarget = path.basename(el.dest);
|
|
|
|
|
2016-05-07 07:46:15 +08:00
|
|
|
var res = babel.transformFileSync(el.src[0], options);
|
|
|
|
var sourceMappingURL = '';
|
2016-05-04 03:37:32 +08:00
|
|
|
|
|
|
|
if (res.map) {
|
2016-05-07 07:46:15 +08:00
|
|
|
sourceMappingURL = '\n//# sourceMappingURL=' + path.basename(el.dest) + '.map';
|
2016-05-04 03:37:32 +08:00
|
|
|
}
|
|
|
|
|
2016-05-07 07:46:15 +08:00
|
|
|
grunt.file.write(el.dest, res.code + sourceMappingURL + '\n');
|
2016-05-04 03:37:32 +08:00
|
|
|
|
|
|
|
if (res.map) {
|
2016-05-07 07:46:15 +08:00
|
|
|
grunt.file.write(el.dest + '.map', JSON.stringify(res.map));
|
2016-05-04 03:37:32 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|