Use the async version of transform in babel-cli (#6826)

* use the async version of transform in babel-cli

* Modify compile to use async version of transformFile

* Babel-CLI: transform files sequentially

* Remove useless res in callback
This commit is contained in:
Antoine Prieëls 2018-01-10 04:17:07 +01:00 committed by Logan Smyth
parent aa888e666a
commit 26e4911eb2
3 changed files with 128 additions and 67 deletions

View File

@ -7,9 +7,13 @@ import fs from "fs";
import * as util from "./util"; import * as util from "./util";
export default function(commander, filenames, opts) { export default function(commander, filenames, opts) {
function write(src, relative, base) { function write(src, relative, base, callback) {
if (typeof base === "function") {
callback = base;
base = undefined;
}
if (!util.isCompilableExtension(relative, commander.extensions)) { if (!util.isCompilableExtension(relative, commander.extensions)) {
return false; return callback();
} }
// remove extension and then append back on .js // remove extension and then append back on .js
@ -17,7 +21,7 @@ export default function(commander, filenames, opts) {
const dest = getDest(commander, relative, base); const dest = getDest(commander, relative, base);
const data = util.compile( util.compile(
src, src,
defaults( defaults(
{ {
@ -26,23 +30,28 @@ export default function(commander, filenames, opts) {
}, },
opts, opts,
), ),
function(err, res) {
if (err) return callback(err);
if (!res) return callback();
// we've requested explicit sourcemaps to be written to disk
if (
res.map &&
commander.sourceMaps &&
commander.sourceMaps !== "inline"
) {
const mapLoc = dest + ".map";
res.code = util.addSourceMappingUrl(res.code, mapLoc);
outputFileSync(mapLoc, JSON.stringify(res.map));
}
outputFileSync(dest, res.code);
util.chmod(src, dest);
util.log(src + " -> " + dest);
return callback(null, true);
},
); );
if (!data) return false;
// we've requested explicit sourcemaps to be written to disk
if (data.map && commander.sourceMaps && commander.sourceMaps !== "inline") {
const mapLoc = dest + ".map";
data.code = util.addSourceMappingUrl(data.code, mapLoc);
outputFileSync(mapLoc, JSON.stringify(data.map));
}
outputFileSync(dest, data.code);
util.chmod(src, dest);
util.log(src + " -> " + dest);
return true;
} }
function getDest(commander, filename, base) { function getDest(commander, filename, base) {
@ -50,17 +59,45 @@ export default function(commander, filenames, opts) {
return path.join(commander.outDir, filename); return path.join(commander.outDir, filename);
} }
function handleFile(src, filename, base) { function handleFile(src, filename, base, callback) {
const didWrite = write(src, filename, base); if (typeof base === "function") {
callback = base;
if (!didWrite && commander.copyFiles) { base = undefined;
const dest = getDest(commander, filename, base);
outputFileSync(dest, fs.readFileSync(src));
util.chmod(src, dest);
} }
write(src, filename, base, function(err, res) {
if (err) return callback(err);
if (!res && commander.copyFiles) {
const dest = getDest(commander, filename, base);
outputFileSync(dest, fs.readFileSync(src));
util.chmod(src, dest);
}
return callback();
});
} }
function handle(filename) { function sequentialHandleFile(files, dirname, index, callback) {
if (typeof index === "function") {
callback = index;
index = 0;
}
const filename = files[index];
const src = path.join(dirname, filename);
handleFile(src, filename, dirname, function(err) {
if (err) return callback(err);
index++;
if (index !== files.length) {
sequentialHandleFile(files, dirname, index, callback);
} else {
callback();
}
});
}
function handle(filename, callback) {
if (!fs.existsSync(filename)) return; if (!fs.existsSync(filename)) return;
const stat = fs.statSync(filename); const stat = fs.statSync(filename);
@ -72,19 +109,32 @@ export default function(commander, filenames, opts) {
util.deleteDir(commander.outDir); util.deleteDir(commander.outDir);
} }
util const files = util.readdir(dirname, commander.includeDotfiles);
.readdir(dirname, commander.includeDotfiles) sequentialHandleFile(files, dirname, callback);
.forEach(function(filename) {
const src = path.join(dirname, filename);
handleFile(src, filename, dirname);
});
} else { } else {
write(filename, path.basename(filename), path.dirname(filename)); write(
filename,
path.basename(filename),
path.dirname(filename),
callback,
);
} }
} }
function sequentialHandle(filenames, index = 0) {
const filename = filenames[index];
handle(filename, function(err) {
if (err) throw err;
index++;
if (index !== filenames.length) {
sequentialHandle(filenames, index);
}
});
}
if (!commander.skipInitialBuild) { if (!commander.skipInitialBuild) {
filenames.forEach(handle); sequentialHandle(filenames);
} }
if (commander.watch) { if (commander.watch) {
@ -104,7 +154,9 @@ export default function(commander, filenames, opts) {
watcher.on(type, function(filename) { watcher.on(type, function(filename) {
const relative = path.relative(dirname, filename) || filename; const relative = path.relative(dirname, filename) || filename;
try { try {
handleFile(filename, relative); handleFile(filename, relative, function(err) {
if (err) throw err;
});
} catch (err) { } catch (err) {
console.error(err.stack); console.error(err.stack);
} }

View File

@ -103,19 +103,21 @@ export default function(commander, filenames, opts) {
}); });
process.stdin.on("end", function() { process.stdin.on("end", function() {
results.push( util.transform(
util.transform( commander.filename,
commander.filename, code,
code, defaults(
defaults( {
{ sourceFileName: "stdin",
sourceFileName: "stdin", },
}, opts,
opts,
),
), ),
function(err, res) {
if (err) throw err;
results.push(res);
output();
},
); );
output();
}); });
}; };
@ -140,7 +142,9 @@ export default function(commander, filenames, opts) {
} }
}); });
_filenames.forEach(function(filename) { let filesProcessed = 0;
_filenames.forEach(function(filename, index) {
let sourceFilename = filename; let sourceFilename = filename;
if (commander.outFile) { if (commander.outFile) {
sourceFilename = path.relative( sourceFilename = path.relative(
@ -150,7 +154,7 @@ export default function(commander, filenames, opts) {
} }
sourceFilename = slash(sourceFilename); sourceFilename = slash(sourceFilename);
const data = util.compile( util.compile(
filename, filename,
defaults( defaults(
{ {
@ -158,14 +162,18 @@ export default function(commander, filenames, opts) {
}, },
opts, opts,
), ),
function(err, res) {
if (err) throw err;
filesProcessed++;
if (res) results[index] = res;
if (filesProcessed === _filenames.length) {
output();
}
},
); );
if (!data) return;
results.push(data);
}); });
output();
}; };
const files = function() { const files = function() {

View File

@ -50,25 +50,26 @@ export function log(msg) {
if (!commander.quiet) console.log(msg); if (!commander.quiet) console.log(msg);
} }
export function transform(filename, code, opts) { export function transform(filename, code, opts, callback) {
opts = Object.assign({}, opts, { opts = Object.assign({}, opts, {
filename, filename,
}); });
return babel.transform(code, opts); babel.transform(code, opts, callback);
} }
export function compile(filename, opts) { export function compile(filename, opts, callback) {
try { babel.transformFile(filename, opts, function(err, res) {
return babel.transformFileSync(filename, opts); if (err) {
} catch (err) { if (commander.watch) {
if (commander.watch) { console.error(err);
console.error(err); return callback(null, { ignored: true });
return { ignored: true }; } else {
} else { return callback(err);
throw err; }
} }
} return callback(null, res);
});
} }
export function deleteDir(path) { export function deleteDir(path) {