Skip to content

Commit

Permalink
Use Babel's async APIs for transformation.
Browse files Browse the repository at this point in the history
  • Loading branch information
loganfsmyth committed Sep 5, 2018
1 parent 3bfff92 commit 52517a0
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions tasks/babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,44 @@ module.exports = function(grunt) {
"babel",
"Use next generation JavaScript, today",
function() {
const options = this.options();
const done = this.async();

this.files.forEach(function(el) {
delete options.filename;
delete options.filenameRelative;
const options = Object.assign({}, this.options());
delete options.filename;
delete options.filenameRelative;

options.sourceFileName = path.relative(
path.dirname(el.dest),
el.src[0]
);
// Async reduce so that the files are written and logged in order.
this.files
.reduce((acc, el) => {
const filename = el.src[0];

if (process.platform === "win32") {
options.sourceFileName = options.sourceFileName.replace(/\\/g, "/");
}
const opts = Object.assign({}, options);

const res = babel.transformFileSync(el.src[0], options);
let sourceMappingURL = "";
opts.sourceFileName = path.relative(path.dirname(el.dest), el.src[0]);

if (res.map) {
sourceMappingURL =
"\n//# sourceMappingURL=" + path.basename(el.dest) + ".map";
}
if (process.platform === "win32") {
opts.sourceFileName = opts.sourceFileName.replace(/\\/g, "/");
}

grunt.file.write(el.dest, res.code + sourceMappingURL + "\n");
return acc
.then(() => babel.transformFileAsync(filename, opts))
.then(res => {
let sourceMappingURL = "";

if (res.map) {
res.map.file = path.basename(el.dest);
grunt.file.write(el.dest + ".map", JSON.stringify(res.map));
}
});
if (res.map) {
sourceMappingURL =
"\n//# sourceMappingURL=" + path.basename(el.dest) + ".map";
}

grunt.file.write(el.dest, res.code + sourceMappingURL + "\n");

if (res.map) {
res.map.file = path.basename(el.dest);
grunt.file.write(el.dest + ".map", JSON.stringify(res.map));
}
});
}, Promise.resolve())
.then(() => done(), err => done(err));
}
);
};

0 comments on commit 52517a0

Please sign in to comment.