Get rid of circular dependencies in babel cli script.

This commit is contained in:
Logan Smyth
2017-03-12 16:31:40 -07:00
parent 01b250a8fa
commit 0c0f090a98
4 changed files with 18 additions and 18 deletions

View File

@@ -1,3 +1,4 @@
import defaults from "lodash/defaults";
import outputFileSync from "output-file-sync";
import slash from "slash";
import path from "path";
@@ -5,17 +6,17 @@ import fs from "fs";
import * as util from "./util";
export default function (commander, filenames) {
export default function (commander, filenames, opts) {
function write(src, relative) {
// remove extension and then append back on .js
relative = relative.replace(/\.(\w*?)$/, "") + ".js";
const dest = path.join(commander.outDir, relative);
const data = util.compile(src, {
const data = util.compile(src, defaults({
sourceFileName: slash(path.relative(dest + "/..", src)),
sourceMapTarget: path.basename(relative),
});
}, opts));
if (!commander.copyFiles && data.ignored) return;
// we've requested explicit sourcemaps to be written to disk
@@ -32,7 +33,7 @@ export default function (commander, filenames) {
}
function handleFile(src, filename) {
if (util.shouldIgnore(src)) return;
if (util.shouldIgnore(src, opts)) return;
if (util.canCompile(filename, commander.extensions)) {
write(src, filename);

View File

@@ -1,4 +1,5 @@
import convertSourceMap from "convert-source-map";
import defaults from "lodash/defaults";
import sourceMap from "source-map";
import slash from "slash";
import path from "path";
@@ -96,9 +97,9 @@ export default function (commander, filenames, opts) {
});
process.stdin.on("end", function () {
results.push(util.transform(commander.filename, code, {
results.push(util.transform(commander.filename, code, defaults({
sourceFileName: "stdin",
}));
}, opts)));
output();
});
};
@@ -123,7 +124,7 @@ export default function (commander, filenames, opts) {
});
_filenames.forEach(function (filename) {
if (util.shouldIgnore(filename)) return;
if (util.shouldIgnore(filename, opts)) return;
let sourceFilename = filename;
if (commander.outFile) {
@@ -131,9 +132,9 @@ export default function (commander, filenames, opts) {
}
sourceFilename = slash(sourceFilename);
const data = util.compile(filename, {
const data = util.compile(filename, defaults({
sourceFileName: sourceFilename,
});
}, opts));
if (data.ignored) return;
results.push(data);
@@ -158,7 +159,7 @@ export default function (commander, filenames, opts) {
pollInterval: 10,
},
}).on("all", function (type, filename) {
if (util.shouldIgnore(filename) || !util.canCompile(filename, commander.extensions)) return;
if (util.shouldIgnore(filename, opts) || !util.canCompile(filename, commander.extensions)) return;
if (type === "add" || type === "change") {
util.log(type + " " + filename);

View File

@@ -106,7 +106,7 @@ if (errors.length) {
//
export const opts = {};
const opts = {};
Object.keys(options).forEach(function (key) {
const opt = options[key];

View File

@@ -1,12 +1,9 @@
import commander from "commander";
import defaults from "lodash/defaults";
import readdir from "fs-readdir-recursive";
import * as babel from "babel-core";
import path from "path";
import fs from "fs";
import * as index from "./index";
export function chmod(src, dest) {
fs.chmodSync(dest, fs.statSync(src).mode);
}
@@ -21,8 +18,8 @@ export { readdir };
export const canCompile = babel.util.canCompile;
export function shouldIgnore(loc) {
return babel.util.shouldIgnore(loc, index.opts.ignore, index.opts.only);
export function shouldIgnore(loc, opts) {
return babel.util.shouldIgnore(loc, opts.ignore, opts.only);
}
export function addSourceMappingUrl(code, loc) {
@@ -34,8 +31,9 @@ export function log(msg) {
}
export function transform(filename, code, opts) {
opts = defaults(opts || {}, index.opts);
opts.filename = filename;
opts = Object.assign({}, opts, {
filename,
});
const result = babel.transform(code, opts);
result.filename = filename;