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:
parent
aa888e666a
commit
26e4911eb2
@ -7,9 +7,13 @@ import fs from "fs";
|
||||
import * as util from "./util";
|
||||
|
||||
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)) {
|
||||
return false;
|
||||
return callback();
|
||||
}
|
||||
|
||||
// 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 data = util.compile(
|
||||
util.compile(
|
||||
src,
|
||||
defaults(
|
||||
{
|
||||
@ -26,23 +30,28 @@ export default function(commander, filenames, opts) {
|
||||
},
|
||||
opts,
|
||||
),
|
||||
);
|
||||
|
||||
if (!data) return false;
|
||||
function(err, res) {
|
||||
if (err) return callback(err);
|
||||
if (!res) return callback();
|
||||
|
||||
// we've requested explicit sourcemaps to be written to disk
|
||||
if (data.map && commander.sourceMaps && commander.sourceMaps !== "inline") {
|
||||
if (
|
||||
res.map &&
|
||||
commander.sourceMaps &&
|
||||
commander.sourceMaps !== "inline"
|
||||
) {
|
||||
const mapLoc = dest + ".map";
|
||||
data.code = util.addSourceMappingUrl(data.code, mapLoc);
|
||||
outputFileSync(mapLoc, JSON.stringify(data.map));
|
||||
res.code = util.addSourceMappingUrl(res.code, mapLoc);
|
||||
outputFileSync(mapLoc, JSON.stringify(res.map));
|
||||
}
|
||||
|
||||
outputFileSync(dest, data.code);
|
||||
outputFileSync(dest, res.code);
|
||||
util.chmod(src, dest);
|
||||
|
||||
util.log(src + " -> " + dest);
|
||||
|
||||
return true;
|
||||
return callback(null, true);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function getDest(commander, filename, base) {
|
||||
@ -50,17 +59,45 @@ export default function(commander, filenames, opts) {
|
||||
return path.join(commander.outDir, filename);
|
||||
}
|
||||
|
||||
function handleFile(src, filename, base) {
|
||||
const didWrite = write(src, filename, base);
|
||||
function handleFile(src, filename, base, callback) {
|
||||
if (typeof base === "function") {
|
||||
callback = base;
|
||||
base = undefined;
|
||||
}
|
||||
|
||||
if (!didWrite && commander.copyFiles) {
|
||||
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;
|
||||
|
||||
const stat = fs.statSync(filename);
|
||||
@ -72,19 +109,32 @@ export default function(commander, filenames, opts) {
|
||||
util.deleteDir(commander.outDir);
|
||||
}
|
||||
|
||||
util
|
||||
.readdir(dirname, commander.includeDotfiles)
|
||||
.forEach(function(filename) {
|
||||
const src = path.join(dirname, filename);
|
||||
handleFile(src, filename, dirname);
|
||||
});
|
||||
const files = util.readdir(dirname, commander.includeDotfiles);
|
||||
sequentialHandleFile(files, dirname, callback);
|
||||
} 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) {
|
||||
filenames.forEach(handle);
|
||||
sequentialHandle(filenames);
|
||||
}
|
||||
|
||||
if (commander.watch) {
|
||||
@ -104,7 +154,9 @@ export default function(commander, filenames, opts) {
|
||||
watcher.on(type, function(filename) {
|
||||
const relative = path.relative(dirname, filename) || filename;
|
||||
try {
|
||||
handleFile(filename, relative);
|
||||
handleFile(filename, relative, function(err) {
|
||||
if (err) throw err;
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err.stack);
|
||||
}
|
||||
|
||||
@ -103,7 +103,6 @@ export default function(commander, filenames, opts) {
|
||||
});
|
||||
|
||||
process.stdin.on("end", function() {
|
||||
results.push(
|
||||
util.transform(
|
||||
commander.filename,
|
||||
code,
|
||||
@ -113,9 +112,12 @@ export default function(commander, filenames, opts) {
|
||||
},
|
||||
opts,
|
||||
),
|
||||
),
|
||||
);
|
||||
function(err, res) {
|
||||
if (err) throw err;
|
||||
results.push(res);
|
||||
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;
|
||||
if (commander.outFile) {
|
||||
sourceFilename = path.relative(
|
||||
@ -150,7 +154,7 @@ export default function(commander, filenames, opts) {
|
||||
}
|
||||
sourceFilename = slash(sourceFilename);
|
||||
|
||||
const data = util.compile(
|
||||
util.compile(
|
||||
filename,
|
||||
defaults(
|
||||
{
|
||||
@ -158,14 +162,18 @@ export default function(commander, filenames, opts) {
|
||||
},
|
||||
opts,
|
||||
),
|
||||
);
|
||||
function(err, res) {
|
||||
if (err) throw err;
|
||||
|
||||
if (!data) return;
|
||||
|
||||
results.push(data);
|
||||
});
|
||||
filesProcessed++;
|
||||
if (res) results[index] = res;
|
||||
|
||||
if (filesProcessed === _filenames.length) {
|
||||
output();
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const files = function() {
|
||||
|
||||
@ -50,25 +50,26 @@ export function 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, {
|
||||
filename,
|
||||
});
|
||||
|
||||
return babel.transform(code, opts);
|
||||
babel.transform(code, opts, callback);
|
||||
}
|
||||
|
||||
export function compile(filename, opts) {
|
||||
try {
|
||||
return babel.transformFileSync(filename, opts);
|
||||
} catch (err) {
|
||||
export function compile(filename, opts, callback) {
|
||||
babel.transformFile(filename, opts, function(err, res) {
|
||||
if (err) {
|
||||
if (commander.watch) {
|
||||
console.error(err);
|
||||
return { ignored: true };
|
||||
return callback(null, { ignored: true });
|
||||
} else {
|
||||
throw err;
|
||||
return callback(err);
|
||||
}
|
||||
}
|
||||
return callback(null, res);
|
||||
});
|
||||
}
|
||||
|
||||
export function deleteDir(path) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user