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:
Logan Smyth
2017-03-16 23:22:43 -07:00
committed by GitHub
parent b6194a8533
commit 39c862c195
20 changed files with 255 additions and 387 deletions

View File

@@ -1,9 +1,10 @@
import deepClone from "lodash/cloneDeep";
import sourceMapSupport from "source-map-support";
import * as registerCache from "./cache";
import escapeRegExp from "lodash/escapeRegExp";
import extend from "lodash/extend";
import * as babel from "babel-core";
import { util, OptionManager } from "babel-core";
import { OptionManager, DEFAULT_EXTENSIONS } from "babel-core";
import fs from "fs";
import path from "path";
@@ -28,18 +29,9 @@ let cache = registerCache.get();
const transformOpts = {};
let ignore;
let only;
let oldHandlers = {};
const maps = {};
const cwd = process.cwd();
function getRelativePath(filename) {
return path.relative(cwd, filename);
}
function mtime(filename) {
return +fs.statSync(filename).mtime;
}
@@ -54,6 +46,9 @@ function compile(filename) {
{ filename }
));
// Bail out ASAP if the file has been ignored.
if (opts === null) return null;
let cacheKey = `${JSON.stringify(opts)}:${babel.version}`;
const env = babel.getEnv(false);
@@ -87,27 +82,14 @@ function compile(filename) {
return result.code;
}
function shouldIgnore(filename) {
if (!ignore && !only) {
return getRelativePath(filename).split(path.sep).indexOf("node_modules") >= 0;
} else {
return util.shouldIgnore(filename, ignore || [], only);
}
}
function loader(m, filename) {
m._compile(compile(filename), filename);
}
function registerExtension(ext) {
const old = oldHandlers[ext] || oldHandlers[".js"] || require.extensions[".js"];
require.extensions[ext] = function (m, filename) {
if (shouldIgnore(filename)) {
old(m, filename);
} else {
loader(m, filename, old);
}
const result = compile(filename);
if (result === null) old(m, filename);
else m._compile(result, filename);
};
}
@@ -129,20 +111,27 @@ function hookExtensions(_exts) {
});
}
hookExtensions(util.canCompile.EXTENSIONS);
hookExtensions(DEFAULT_EXTENSIONS);
export default function (opts?: Object = {}) {
if (opts.only != null) only = util.arrayify(opts.only, util.regexify);
if (opts.ignore != null) ignore = util.arrayify(opts.ignore, util.regexify);
if (opts.extensions) hookExtensions(util.arrayify(opts.extensions));
if (opts.extensions) hookExtensions(opts.extensions);
if (opts.cache === false) cache = null;
delete opts.extensions;
delete opts.ignore;
delete opts.cache;
delete opts.only;
extend(transformOpts, opts);
if (!transformOpts.ignore && !transformOpts.only) {
// By default, ignore files inside the node_modules relative to the current working directory.
transformOpts.ignore = [
new RegExp(
"^" +
escapeRegExp(process.cwd() + path.sep) +
".*" +
escapeRegExp(path.sep + "node_modules" + path.sep)
, "i"),
];
}
}