Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More strictly parse configs and explicitly handle arguments in babel-cli #5463

Merged
merged 4 commits into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/babel-cli/src/babel/dir.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import defaults from "lodash/defaults";
import outputFileSync from "output-file-sync";
import slash from "slash";
import path from "path";
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
Expand All @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions packages/babel-cli/src/babel/file.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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();
});
};
Expand All @@ -123,17 +124,17 @@ 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) {
sourceFilename = path.relative(path.dirname(commander.outFile), sourceFilename);
}
sourceFilename = slash(sourceFilename);

const data = util.compile(filename, {
const data = util.compile(filename, defaults({
sourceFileName: sourceFilename,
});
}, opts));

if (data.ignored) return;
results.push(data);
Expand All @@ -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);
Expand Down
92 changes: 56 additions & 36 deletions packages/babel-cli/src/babel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import fs from "fs";
import commander from "commander";
import kebabCase from "lodash/kebabCase";
import { options, util, version } from "babel-core";
import { util, version } from "babel-core";
import uniq from "lodash/uniq";
import glob from "glob";

Expand All @@ -12,34 +11,41 @@ import fileCommand from "./file";

import pkg from "../../package.json";

Object.keys(options).forEach(function (key) {
const option = options[key];
if (option.hidden) return;

let arg = kebabCase(key);

if (option.type !== "boolean") {
arg += " [" + (option.type || "string") + "]";
}

if (option.type === "boolean" && option.default === true) {
arg = "no-" + arg;
}

arg = "--" + arg;

if (option.shorthand) {
arg = "-" + option.shorthand + ", " + arg;
}

const desc = [];
if (option.deprecated) desc.push("[DEPRECATED] " + option.deprecated);
if (option.description) desc.push(option.description);

commander.option(arg, desc.join(" "));
});

/* eslint-disable max-len */
// Standard Babel input configs.
commander.option("-f, --filename [filename]", "filename to use when reading from stdin - this will be used in source-maps, errors etc");
commander.option("--presets [list]", "comma-separated list of preset names");
commander.option("--plugins [list]", "comma-separated list of plugin names");

// Basic file input configuration.
commander.option("--source-type [script|module]", "");
commander.option("--no-babelrc", "Whether or not to look up .babelrc and .babelignore files");
commander.option("--ignore [list]", "list of glob paths to **not** compile");
commander.option("--only [list]", "list of glob paths to **only** compile");

// Misc babel config.
commander.option("--no-highlight-code", "enable/disable ANSI syntax highlighting of code frames (on by default)");

// General output formatting.
commander.option("--no-comments", "write comments to generated output (true by default)");
commander.option("--retain-lines", "retain line numbers - will result in really ugly code");
commander.option("--compact [true|false|auto]", "do not include superfluous whitespace characters and line terminators");
commander.option("--minified", "save as much bytes when printing [true|false]");
commander.option("--auxiliary-comment-before [string]", "print a comment before any injected non-user code");
commander.option("--auxiliary-comment-after [string]", "print a comment after any injected non-user code");

// General soucemap formatting.
commander.option("-s, --source-maps [true|false|inline|both]", "");
commander.option("--source-map-target [string]", "set `file` on returned source map");
commander.option("--source-file-name [string]", "set `sources[0]` on returned source map");
commander.option("--source-root [filename]", "the root from which all sources are relative");

// Config params for certain module output formats.
commander.option("--module-root [filename]", "optional prefix for the AMD module formatter that will be prepend to the filename on module definitions");
commander.option("-M, --module-ids", "insert an explicit id for modules");
commander.option("--module-id [string]", "specify a custom name for module ids");

// "babel" command specific arguments that are not passed to babel-core.
commander.option("-x, --extensions [extensions]", "List of extensions to compile when a directory has been input [.es6,.js,.es,.jsx]");
commander.option("-w, --watch", "Recompile files on changes");
commander.option("--skip-initial-build", "Do not compile files before watching");
Expand Down Expand Up @@ -106,20 +112,34 @@ if (errors.length) {

//

export const opts = {};
const opts = commander.opts();

Object.keys(options).forEach(function (key) {
const opt = options[key];
if (commander[key] !== undefined && commander[key] !== opt.default) {
opts[key] = commander[key];
}
});
// Delete options that are specific to babel-cli and shouldn't be passed to babel-core.
delete opts.version;
delete opts.extensions;
delete opts.watch;
delete opts.skipInitialBuild;
delete opts.outFile;
delete opts.outDir;
delete opts.copyFiles;
delete opts.quiet;

// Commander will default the "--no-" arguments to true, but we want to leave them undefined so that
// babel-core can handle the default-assignment logic on its own.
if (opts.babelrc === true) opts.babelrc = undefined;
if (opts.comments === true) opts.comments = undefined;
if (opts.highlightCode === true) opts.highlightCode = undefined;

opts.ignore = util.arrayify(opts.ignore, util.regexify);

if (opts.only) {
opts.only = util.arrayify(opts.only, util.regexify);
}

if (opts.sourceMaps) opts.sourceMaps = util.booleanify(opts.sourceMaps);
if (opts.compact) opts.compact = util.booleanify(opts.compact);
if (opts.presets) opts.presets = util.list(opts.presets);
if (opts.plugins) opts.plugins = util.list(opts.plugins);

const fn = commander.outDir ? dirCommand : fileCommand;
fn(commander, filenames, opts);
12 changes: 5 additions & 7 deletions packages/babel-cli/src/babel/util.js
Original file line number Diff line number Diff line change
@@ -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);
}
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
1 change: 0 additions & 1 deletion packages/babel-core/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import fs from "fs";

export { default as File } from "./transformation/file";
export { default as options } from "./transformation/file/options/config";
export { default as buildExternalHelpers } from "./tools/build-external-helpers";
export { default as template } from "babel-template";
export { default as resolvePlugin } from "./helpers/resolve-plugin";
Expand Down
6 changes: 1 addition & 5 deletions packages/babel-core/src/transformation/file/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,6 @@ export default class File extends Store {

opts.basename = path.basename(opts.filename, path.extname(opts.filename));

opts.ignore = util.arrayify(opts.ignore, util.regexify);

if (opts.only) opts.only = util.arrayify(opts.only, util.regexify);

defaults(opts, {
moduleRoot: opts.sourceRoot,
});
Expand Down Expand Up @@ -577,7 +573,7 @@ export default class File extends Store {
if (!opts.code) return this.makeResult(result);

let gen = generate;
if (opts.generatorOpts.generator) {
if (opts.generatorOpts && opts.generatorOpts.generator) {
gen = opts.generatorOpts.generator;

if (typeof gen === "string") {
Expand Down
Loading