Merge pull request #7345 from loganfsmyth/small-bug-fixes
Small tweaks to prep for coming .babelrc lookup work
This commit is contained in:
commit
493996e02a
@ -12,12 +12,7 @@ import {
|
|||||||
|
|
||||||
const debug = buildDebug("babel:config:config-chain");
|
const debug = buildDebug("babel:config:config-chain");
|
||||||
|
|
||||||
import {
|
import { findRelativeConfig, loadConfig, type ConfigFile } from "./files";
|
||||||
findBabelrc,
|
|
||||||
findBabelignore,
|
|
||||||
loadConfig,
|
|
||||||
type ConfigFile,
|
|
||||||
} from "./files";
|
|
||||||
|
|
||||||
import { makeWeakCache, makeStrongCache } from "./caching";
|
import { makeWeakCache, makeStrongCache } from "./caching";
|
||||||
|
|
||||||
@ -108,7 +103,6 @@ const loadPresetOverridesEnvDescriptors = makeWeakCache(
|
|||||||
* Build a config chain for Babel's full root configuration.
|
* Build a config chain for Babel's full root configuration.
|
||||||
*/
|
*/
|
||||||
export function buildRootChain(
|
export function buildRootChain(
|
||||||
cwd: string,
|
|
||||||
opts: ValidatedOptions,
|
opts: ValidatedOptions,
|
||||||
context: ConfigContext,
|
context: ConfigContext,
|
||||||
): ConfigChain | null {
|
): ConfigChain | null {
|
||||||
@ -125,22 +119,15 @@ export function buildRootChain(
|
|||||||
// resolve all .babelrc files
|
// resolve all .babelrc files
|
||||||
if (opts.babelrc !== false && context.filename !== null) {
|
if (opts.babelrc !== false && context.filename !== null) {
|
||||||
const filename = context.filename;
|
const filename = context.filename;
|
||||||
const babelignoreFile = findBabelignore(filename);
|
|
||||||
if (
|
const { ignore, config } = findRelativeConfig(filename, context.envName);
|
||||||
babelignoreFile &&
|
|
||||||
shouldIgnore(
|
if (ignore && shouldIgnore(context, ignore.ignore, null, ignore.dirname)) {
|
||||||
context,
|
|
||||||
babelignoreFile.ignore,
|
|
||||||
null,
|
|
||||||
babelignoreFile.dirname,
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const babelrcFile = findBabelrc(filename, context.envName);
|
if (config) {
|
||||||
if (babelrcFile) {
|
const result = loadFileChain(config, context);
|
||||||
const result = loadFileChain(babelrcFile, context);
|
|
||||||
if (!result) return null;
|
if (!result) return null;
|
||||||
|
|
||||||
mergeChain(fileChain, result);
|
mergeChain(fileChain, result);
|
||||||
|
|||||||
@ -21,23 +21,29 @@ export type IgnoreFile = {
|
|||||||
ignore: Array<string>,
|
ignore: Array<string>,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type RelativeConfig = {
|
||||||
|
config: ConfigFile | null,
|
||||||
|
ignore: IgnoreFile | null,
|
||||||
|
};
|
||||||
|
|
||||||
const BABELRC_FILENAME = ".babelrc";
|
const BABELRC_FILENAME = ".babelrc";
|
||||||
const BABELRC_JS_FILENAME = ".babelrc.js";
|
const BABELRC_JS_FILENAME = ".babelrc.js";
|
||||||
const PACKAGE_FILENAME = "package.json";
|
const PACKAGE_FILENAME = "package.json";
|
||||||
const BABELIGNORE_FILENAME = ".babelignore";
|
const BABELIGNORE_FILENAME = ".babelignore";
|
||||||
|
|
||||||
export function findBabelrc(
|
export function findRelativeConfig(
|
||||||
filepath: string,
|
filepath: string,
|
||||||
envName: string,
|
envName: string,
|
||||||
): ConfigFile | null {
|
): RelativeConfig {
|
||||||
|
let config = null;
|
||||||
|
let ignore = null;
|
||||||
|
|
||||||
const dirname = path.dirname(filepath);
|
const dirname = path.dirname(filepath);
|
||||||
let loc = dirname;
|
let loc = dirname;
|
||||||
while (true) {
|
while (true) {
|
||||||
const conf = [
|
if (!config) {
|
||||||
BABELRC_FILENAME,
|
config = [BABELRC_FILENAME, BABELRC_JS_FILENAME, PACKAGE_FILENAME].reduce(
|
||||||
BABELRC_JS_FILENAME,
|
(previousConfig: ConfigFile | null, name) => {
|
||||||
PACKAGE_FILENAME,
|
|
||||||
].reduce((previousConfig: ConfigFile | null, name) => {
|
|
||||||
const filepath = path.join(loc, name);
|
const filepath = path.join(loc, name);
|
||||||
const config = readConfig(filepath, envName);
|
const config = readConfig(filepath, envName);
|
||||||
|
|
||||||
@ -51,31 +57,22 @@ export function findBabelrc(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return config || previousConfig;
|
return config || previousConfig;
|
||||||
}, null);
|
},
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
|
||||||
if (conf) {
|
if (config) {
|
||||||
debug("Found configuration %o from %o.", conf.filepath, dirname);
|
debug("Found configuration %o from %o.", config.filepath, dirname);
|
||||||
return conf;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const nextLoc = path.dirname(loc);
|
if (!ignore) {
|
||||||
if (loc === nextLoc) break;
|
|
||||||
loc = nextLoc;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function findBabelignore(filepath: string): IgnoreFile | null {
|
|
||||||
const dirname = path.dirname(filepath);
|
|
||||||
let loc = dirname;
|
|
||||||
while (true) {
|
|
||||||
const ignoreLoc = path.join(loc, BABELIGNORE_FILENAME);
|
const ignoreLoc = path.join(loc, BABELIGNORE_FILENAME);
|
||||||
const ignore = readIgnoreConfig(ignoreLoc);
|
ignore = readIgnoreConfig(ignoreLoc);
|
||||||
|
|
||||||
if (ignore) {
|
if (ignore) {
|
||||||
debug("Found ignore %o from %o.", ignore.filepath, dirname);
|
debug("Found ignore %o from %o.", ignore.filepath, dirname);
|
||||||
return ignore;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const nextLoc = path.dirname(loc);
|
const nextLoc = path.dirname(loc);
|
||||||
@ -83,7 +80,7 @@ export function findBabelignore(filepath: string): IgnoreFile | null {
|
|||||||
loc = nextLoc;
|
loc = nextLoc;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return { config, ignore };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function loadConfig(
|
export function loadConfig(
|
||||||
@ -106,7 +103,7 @@ export function loadConfig(
|
|||||||
* Read the given config file, returning the result. Returns null if no config was found, but will
|
* Read the given config file, returning the result. Returns null if no config was found, but will
|
||||||
* throw if there are parsing errors while loading a config.
|
* throw if there are parsing errors while loading a config.
|
||||||
*/
|
*/
|
||||||
function readConfig(filepath, envName) {
|
function readConfig(filepath, envName): ConfigFile | null {
|
||||||
return path.extname(filepath) === ".js"
|
return path.extname(filepath) === ".js"
|
||||||
? readConfigJS(filepath, { envName })
|
? readConfigJS(filepath, { envName })
|
||||||
: readConfigFile(filepath);
|
: readConfigFile(filepath);
|
||||||
|
|||||||
@ -1,27 +1,14 @@
|
|||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
export type ConfigFile = {
|
import type { ConfigFile, IgnoreFile, RelativeConfig } from "./configuration";
|
||||||
filepath: string,
|
|
||||||
dirname: string,
|
|
||||||
options: {},
|
|
||||||
};
|
|
||||||
|
|
||||||
export type IgnoreFile = {
|
export type { ConfigFile, IgnoreFile, RelativeConfig };
|
||||||
filepath: string,
|
|
||||||
dirname: string,
|
|
||||||
ignore: Array<string>,
|
|
||||||
};
|
|
||||||
|
|
||||||
export function findBabelrc(
|
export function findRelativeConfig(
|
||||||
filepath: string,
|
filepath: string,
|
||||||
envName: string, // eslint-disable-line no-unused-vars
|
envName: string, // eslint-disable-line no-unused-vars
|
||||||
): ConfigFile | null {
|
): RelativeConfig {
|
||||||
return null;
|
return { config: null, ignore: null };
|
||||||
}
|
|
||||||
|
|
||||||
// eslint-disable-next-line no-unused-vars
|
|
||||||
export function findBabelignore(filepath: string): IgnoreFile | null {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function loadConfig(name: string, dirname: string): ConfigFile {
|
export function loadConfig(name: string, dirname: string): ConfigFile {
|
||||||
|
|||||||
@ -62,7 +62,7 @@ export default function loadConfig(inputOpts: mixed): ResolvedConfig | null {
|
|||||||
envName,
|
envName,
|
||||||
};
|
};
|
||||||
|
|
||||||
const configChain = buildRootChain(absoluteCwd, args, context);
|
const configChain = buildRootChain(args, context);
|
||||||
if (!configChain) return null;
|
if (!configChain) return null;
|
||||||
|
|
||||||
const optionDefaults = {};
|
const optionDefaults = {};
|
||||||
|
|||||||
@ -140,6 +140,8 @@ export function assertConfigApplicableTest(
|
|||||||
key: string,
|
key: string,
|
||||||
value: mixed,
|
value: mixed,
|
||||||
): ConfigApplicableTest | void {
|
): ConfigApplicableTest | void {
|
||||||
|
if (value === undefined) return value;
|
||||||
|
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
value.forEach((item, i) => {
|
value.forEach((item, i) => {
|
||||||
if (!checkValidTest(item)) {
|
if (!checkValidTest(item)) {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
import loadConfig, { InputOptions } from "./config";
|
import loadConfig, { type InputOptions } from "./config";
|
||||||
import normalizeFile from "./transformation/normalize-file";
|
import normalizeFile from "./transformation/normalize-file";
|
||||||
import normalizeOptions from "./transformation/normalize-opts";
|
import normalizeOptions from "./transformation/normalize-opts";
|
||||||
|
|
||||||
|
|||||||
@ -177,6 +177,16 @@ fs.readdirSync(fixtureLoc).forEach(function(binName) {
|
|||||||
|
|
||||||
const suiteLoc = path.join(fixtureLoc, binName);
|
const suiteLoc = path.join(fixtureLoc, binName);
|
||||||
describe("bin/" + binName, function() {
|
describe("bin/" + binName, function() {
|
||||||
|
let cwd;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
cwd = process.cwd();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
process.chdir(cwd);
|
||||||
|
});
|
||||||
|
|
||||||
fs.readdirSync(suiteLoc).forEach(function(testName) {
|
fs.readdirSync(suiteLoc).forEach(function(testName) {
|
||||||
if (testName[0] === ".") return;
|
if (testName[0] === ".") return;
|
||||||
|
|
||||||
|
|||||||
@ -76,6 +76,16 @@ const buildTest = opts => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
describe("debug output", () => {
|
describe("debug output", () => {
|
||||||
|
let cwd;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
cwd = process.cwd();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
process.chdir(cwd);
|
||||||
|
});
|
||||||
|
|
||||||
fs.readdirSync(fixtureLoc).forEach(testName => {
|
fs.readdirSync(fixtureLoc).forEach(testName => {
|
||||||
if (testName.slice(0, 1) === ".") return;
|
if (testName.slice(0, 1) === ".") return;
|
||||||
const testLoc = path.join(fixtureLoc, testName);
|
const testLoc = path.join(fixtureLoc, testName);
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"presets": [
|
"presets": [
|
||||||
["../../../../lib", {
|
["../../../../lib", {
|
||||||
"configPath": "../fixtures/preset-options/browserslist-config",
|
"configPath": "packages/babel-preset-env/test/fixtures/preset-options/browserslist-config",
|
||||||
"modules": false
|
"modules": false
|
||||||
}]
|
}]
|
||||||
]
|
]
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"presets": [
|
"presets": [
|
||||||
["../../../../lib", {
|
["../../../../lib", {
|
||||||
"configPath": "../fixtures/preset-options/browserslist-package",
|
"configPath": "packages/babel-preset-env/test/fixtures/preset-options/browserslist-package",
|
||||||
"targets": {
|
"targets": {
|
||||||
"chrome": 55
|
"chrome": 55
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user