Make only/ignore relative to cwd/config file and move only/ignore checking all to core. (#5487)
* Make only/ignore relative to cwd/config file and move only/ignore checking all to core.
This commit is contained in:
@@ -3,7 +3,6 @@ import Module from "module";
|
||||
import { inspect } from "util";
|
||||
import path from "path";
|
||||
import repl from "repl";
|
||||
import { util } from "babel-core";
|
||||
import * as babel from "babel-core";
|
||||
import vm from "vm";
|
||||
import "babel-polyfill";
|
||||
@@ -13,13 +12,24 @@ import pkg from "../package.json";
|
||||
|
||||
const program = new commander.Command("babel-node");
|
||||
|
||||
function collect(value, previousValue): Array<string> {
|
||||
// If the user passed the option with no value, like "babel-node file.js --presets", do nothing.
|
||||
if (typeof value !== "string") return previousValue;
|
||||
|
||||
const values = value.split(",");
|
||||
|
||||
return previousValue ? previousValue.concat(values) : values;
|
||||
}
|
||||
|
||||
/* eslint-disable max-len */
|
||||
program.option("-e, --eval [script]", "Evaluate script");
|
||||
program.option("-p, --print [code]", "Evaluate script and print result");
|
||||
program.option("-o, --only [globs]", "");
|
||||
program.option("-i, --ignore [globs]", "");
|
||||
program.option("-x, --extensions [extensions]", "List of extensions to hook into [.es6,.js,.es,.jsx]");
|
||||
program.option("-w, --plugins [string]", "", util.list);
|
||||
program.option("-b, --presets [string]", "", util.list);
|
||||
program.option("-o, --only [globs]", "A comma-separated list of glob patterns to compile", collect);
|
||||
program.option("-i, --ignore [globs]", "A comma-separated list of glob patterns to skip compiling", collect);
|
||||
program.option("-x, --extensions [extensions]", "List of extensions to hook into [.es6,.js,.es,.jsx]", collect);
|
||||
program.option("-w, --plugins [string]", "", collect);
|
||||
program.option("-b, --presets [string]", "", collect);
|
||||
/* eslint-enable max-len */
|
||||
|
||||
program.version(pkg.version);
|
||||
program.usage("[options] [ -e script | script.js ] [arguments]");
|
||||
|
||||
@@ -1,7 +1,16 @@
|
||||
import commander from "commander";
|
||||
import { util, buildExternalHelpers } from "babel-core";
|
||||
import { buildExternalHelpers } from "babel-core";
|
||||
|
||||
commander.option("-l, --whitelist [whitelist]", "Whitelist of helpers to ONLY include", util.list);
|
||||
function collect(value, previousValue): Array<string> {
|
||||
// If the user passed the option with no value, like "babel-external-helpers --whitelist", do nothing.
|
||||
if (typeof value !== "string") return previousValue;
|
||||
|
||||
const values = value.split(",");
|
||||
|
||||
return previousValue ? previousValue.concat(values) : values;
|
||||
}
|
||||
|
||||
commander.option("-l, --whitelist [whitelist]", "Whitelist of helpers to ONLY include", collect);
|
||||
commander.option("-t, --output-type [type]", "Type of output (global|umd|var)", "global");
|
||||
|
||||
commander.usage("[options]");
|
||||
|
||||
@@ -8,6 +8,8 @@ import * as util from "./util";
|
||||
|
||||
export default function (commander, filenames, opts) {
|
||||
function write(src, relative) {
|
||||
if (!util.isCompilableExtension(relative, commander.extensions)) return false;
|
||||
|
||||
// remove extension and then append back on .js
|
||||
relative = relative.replace(/\.(\w*?)$/, "") + ".js";
|
||||
|
||||
@@ -17,7 +19,8 @@ export default function (commander, filenames, opts) {
|
||||
sourceFileName: slash(path.relative(dest + "/..", src)),
|
||||
sourceMapTarget: path.basename(relative),
|
||||
}, opts));
|
||||
if (!commander.copyFiles && data.ignored) return;
|
||||
|
||||
if (!data) return false;
|
||||
|
||||
// we've requested explicit sourcemaps to be written to disk
|
||||
if (data.map && commander.sourceMaps && commander.sourceMaps !== "inline") {
|
||||
@@ -30,14 +33,14 @@ export default function (commander, filenames, opts) {
|
||||
util.chmod(src, dest);
|
||||
|
||||
util.log(src + " -> " + dest);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function handleFile(src, filename) {
|
||||
if (util.shouldIgnore(src, opts)) return;
|
||||
const didWrite = write(src, filename);
|
||||
|
||||
if (util.canCompile(filename, commander.extensions)) {
|
||||
write(src, filename);
|
||||
} else if (commander.copyFiles) {
|
||||
if (!didWrite && commander.copyFiles) {
|
||||
const dest = path.join(commander.outDir, filename);
|
||||
outputFileSync(dest, fs.readFileSync(src));
|
||||
util.chmod(src, dest);
|
||||
|
||||
@@ -124,8 +124,6 @@ export default function (commander, filenames, opts) {
|
||||
});
|
||||
|
||||
_filenames.forEach(function (filename) {
|
||||
if (util.shouldIgnore(filename, opts)) return;
|
||||
|
||||
let sourceFilename = filename;
|
||||
if (commander.outFile) {
|
||||
sourceFilename = path.relative(path.dirname(commander.outFile), sourceFilename);
|
||||
@@ -136,7 +134,8 @@ export default function (commander, filenames, opts) {
|
||||
sourceFileName: sourceFilename,
|
||||
}, opts));
|
||||
|
||||
if (data.ignored) return;
|
||||
if (!data) return;
|
||||
|
||||
results.push(data);
|
||||
});
|
||||
|
||||
@@ -159,7 +158,7 @@ export default function (commander, filenames, opts) {
|
||||
pollInterval: 10,
|
||||
},
|
||||
}).on("all", function (type, filename) {
|
||||
if (util.shouldIgnore(filename, opts) || !util.canCompile(filename, commander.extensions)) return;
|
||||
if (!util.isCompilableExtension(filename, commander.extensions)) return;
|
||||
|
||||
if (type === "add" || type === "change") {
|
||||
util.log(type + " " + filename);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import fs from "fs";
|
||||
import commander from "commander";
|
||||
import { util, version } from "babel-core";
|
||||
import { version } from "babel-core";
|
||||
import uniq from "lodash/uniq";
|
||||
import glob from "glob";
|
||||
|
||||
@@ -11,17 +11,38 @@ import fileCommand from "./file";
|
||||
|
||||
import pkg from "../../package.json";
|
||||
|
||||
function booleanify(val: any): boolean | any {
|
||||
if (val === "true" || val == 1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (val === "false" || val == 0 || !val) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
function collect(value, previousValue): Array<string> {
|
||||
// If the user passed the option with no value, like "babel file.js --presets", do nothing.
|
||||
if (typeof value !== "string") return previousValue;
|
||||
|
||||
const values = value.split(",");
|
||||
|
||||
return previousValue ? previousValue.concat(values) : values;
|
||||
}
|
||||
|
||||
/* 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");
|
||||
commander.option("--presets [list]", "comma-separated list of preset names", collect);
|
||||
commander.option("--plugins [list]", "comma-separated list of plugin names", collect);
|
||||
|
||||
// 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");
|
||||
commander.option("--ignore [list]", "list of glob paths to **not** compile", collect);
|
||||
commander.option("--only [list]", "list of glob paths to **only** compile", collect);
|
||||
|
||||
// Misc babel config.
|
||||
commander.option("--no-highlight-code", "enable/disable ANSI syntax highlighting of code frames (on by default)");
|
||||
@@ -29,13 +50,13 @@ commander.option("--no-highlight-code", "enable/disable ANSI syntax highlighting
|
||||
// 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("--compact [true|false|auto]", "do not include superfluous whitespace characters and line terminators", booleanify);
|
||||
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("-s, --source-maps [true|false|inline|both]", "", booleanify);
|
||||
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");
|
||||
@@ -46,7 +67,7 @@ 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("-x, --extensions [extensions]", "List of extensions to compile when a directory has been input [.es6,.js,.es,.jsx]", collect);
|
||||
commander.option("-w, --watch", "Recompile files on changes");
|
||||
commander.option("--skip-initial-build", "Do not compile files before watching");
|
||||
commander.option("-o, --out-file [out]", "Compile all input files into a single file");
|
||||
@@ -61,12 +82,6 @@ commander.parse(process.argv);
|
||||
|
||||
//
|
||||
|
||||
if (commander.extensions) {
|
||||
commander.extensions = util.arrayify(commander.extensions);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
const errors = [];
|
||||
|
||||
let filenames = commander.args.reduce(function (globbed, input) {
|
||||
@@ -130,16 +145,5 @@ 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);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import commander from "commander";
|
||||
import readdir from "fs-readdir-recursive";
|
||||
import * as babel from "babel-core";
|
||||
import includes from "lodash/includes";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
|
||||
@@ -10,16 +11,19 @@ export function chmod(src, dest) {
|
||||
|
||||
export function readdirFilter(filename) {
|
||||
return readdir(filename).filter(function (filename) {
|
||||
return babel.util.canCompile(filename);
|
||||
return babel.util.isCompilableExtension(filename);
|
||||
});
|
||||
}
|
||||
|
||||
export { readdir };
|
||||
|
||||
export const canCompile = babel.util.canCompile;
|
||||
|
||||
export function shouldIgnore(loc, opts) {
|
||||
return babel.util.shouldIgnore(loc, opts.ignore, opts.only);
|
||||
/**
|
||||
* Test if a filename ends with a compilable extension.
|
||||
*/
|
||||
export function isCompilableExtension(filename: string, altExts?: Array<string>): boolean {
|
||||
const exts = altExts || babel.DEFAULT_EXTENSIONS;
|
||||
const ext = path.extname(filename);
|
||||
return includes(exts, ext);
|
||||
}
|
||||
|
||||
export function addSourceMappingUrl(code, loc) {
|
||||
@@ -35,16 +39,12 @@ export function transform(filename, code, opts) {
|
||||
filename,
|
||||
});
|
||||
|
||||
const result = babel.transform(code, opts);
|
||||
result.filename = filename;
|
||||
result.actual = code;
|
||||
return result;
|
||||
return babel.transform(code, opts);
|
||||
}
|
||||
|
||||
export function compile(filename, opts) {
|
||||
try {
|
||||
const code = fs.readFileSync(filename, "utf8");
|
||||
return transform(filename, code, opts);
|
||||
return babel.transformFileSync(filename, opts);
|
||||
} catch (err) {
|
||||
if (commander.watch) {
|
||||
console.error(toErrorStack(err));
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"args": ["src", "--out-dir", "lib", "--ignore", "/bar/*"]
|
||||
"args": ["src", "--out-dir", "lib", "--ignore", "src/bar"]
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ const buildTest = function (binName, testName, opts) {
|
||||
}
|
||||
|
||||
if (binName === "babel-node") {
|
||||
args.push("--only", "packages/*/test");
|
||||
args.push("--only", "../../../../packages/*/test");
|
||||
}
|
||||
|
||||
args = args.concat(opts.args);
|
||||
@@ -119,7 +119,7 @@ const buildTest = function (binName, testName, opts) {
|
||||
}
|
||||
|
||||
if (err) {
|
||||
err.message = args.join(" ") + ": " + err.message;
|
||||
err.message = args.map((arg) => `"${ arg }"`).join(" ") + ": " + err.message;
|
||||
}
|
||||
|
||||
callback(err);
|
||||
|
||||
Reference in New Issue
Block a user