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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a list of files that were read from loadPartialConfig #11907

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 41 additions & 9 deletions packages/babel-core/src/config/config-chain.js
Expand Up @@ -40,6 +40,7 @@ export type ConfigChain = {
plugins: Array<UnloadedDescriptor>,
presets: Array<UnloadedDescriptor>,
options: Array<ValidatedOptions>,
files: Set<string>,
};

export type PresetInstance = {
Expand Down Expand Up @@ -71,6 +72,7 @@ export function* buildPresetChain(
plugins: dedupDescriptors(chain.plugins),
presets: dedupDescriptors(chain.presets),
options: chain.options.map(o => normalizeOptions(o)),
files: new Set(),
};
}

Expand Down Expand Up @@ -124,10 +126,13 @@ const loadPresetOverridesEnvDescriptors = makeWeakCacheSync(
),
);

export type FileHandling = "transpile" | "ignored" | "unsupported";
export type RootConfigChain = ConfigChain & {
babelrc: ConfigFile | void,
config: ConfigFile | void,
ignore: IgnoreFile | void,
fileHandling: FileHandling,
files: Set<string>,
};

/**
Expand Down Expand Up @@ -202,6 +207,7 @@ export function* buildRootChain(
: null;

let ignoreFile, babelrcFile;
let isIgnored = false;
const fileChain = emptyChain();
// resolve all .babelrc files
if (
Expand All @@ -215,14 +221,18 @@ export function* buildRootChain(
context.caller,
));

if (ignoreFile) {
fileChain.files.add(ignoreFile.filepath);
}

if (
ignoreFile &&
shouldIgnore(context, ignoreFile.ignore, null, ignoreFile.dirname)
) {
return null;
isIgnored = true;
}

if (babelrcFile) {
if (babelrcFile && !isIgnored) {
const validatedFile = validateBabelrcFile(babelrcFile);
const babelrcLogger = new ConfigPrinter();
const result = yield* loadFileChain(
Expand All @@ -231,10 +241,16 @@ export function* buildRootChain(
undefined,
babelrcLogger,
);
if (!result) return null;
babelRcReport = babelrcLogger.output();
if (!result) {
isIgnored = true;
} else {
babelRcReport = babelrcLogger.output();
mergeChain(fileChain, result);
}
}

mergeChain(fileChain, result);
if (babelrcFile && isIgnored) {
fileChain.files.add(babelrcFile.filepath);
}
}

Expand All @@ -257,12 +273,14 @@ export function* buildRootChain(
);

return {
plugins: dedupDescriptors(chain.plugins),
presets: dedupDescriptors(chain.presets),
options: chain.options.map(o => normalizeOptions(o)),
plugins: isIgnored ? [] : dedupDescriptors(chain.plugins),
presets: isIgnored ? [] : dedupDescriptors(chain.presets),
options: isIgnored ? [] : chain.options.map(o => normalizeOptions(o)),
fileHandling: isIgnored ? "ignored" : "transpile",
ignore: ignoreFile || undefined,
babelrc: babelrcFile || undefined,
config: configFile || undefined,
files: chain.files,
};
}

Expand Down Expand Up @@ -355,7 +373,7 @@ const loadProgrammaticChain = makeChainWalker({
/**
* Build a config chain for a given file.
*/
const loadFileChain = makeChainWalker({
const loadFileChainWalker = makeChainWalker({
root: file => loadFileDescriptors(file),
env: (file, envName) => loadFileEnvDescriptors(file)(envName),
overrides: (file, index) => loadFileOverridesDescriptors(file)(index),
Expand All @@ -364,6 +382,16 @@ const loadFileChain = makeChainWalker({
createLogger: (file, context, baseLogger) =>
buildFileLogger(file.filepath, context, baseLogger),
});

function* loadFileChain(input, context, files, baseLogger) {
const chain = yield* loadFileChainWalker(input, context, files, baseLogger);
if (chain) {
chain.files.add(input.filepath);
}

return chain;
}

const loadFileDescriptors = makeWeakCacheSync((file: ValidatedFile) =>
buildRootDescriptors(file, file.filepath, createUncachedDescriptors),
);
Expand Down Expand Up @@ -622,6 +650,9 @@ function mergeChain(target: ConfigChain, source: ConfigChain): ConfigChain {
target.options.push(...source.options);
target.plugins.push(...source.plugins);
target.presets.push(...source.presets);
for (const file of source.files) {
target.files.add(file);
}

return target;
}
Expand All @@ -642,6 +673,7 @@ function emptyChain(): ConfigChain {
options: [],
presets: [],
plugins: [],
files: new Set(),
};
}

Expand Down
6 changes: 5 additions & 1 deletion packages/babel-core/src/config/full.js
Expand Up @@ -63,7 +63,11 @@ export default gensync<[any], ResolvedConfig | null>(function* loadFullConfig(
if (!result) {
return null;
}
const { options, context } = result;
const { options, context, fileHandling } = result;

if (fileHandling === "ignored") {
return null;
}

const optionDefaults = {};
const passes: Array<Array<Plugin>> = [[]];
Expand Down
36 changes: 30 additions & 6 deletions packages/babel-core/src/config/partial.js
Expand Up @@ -5,7 +5,11 @@ import gensync, { type Handler } from "gensync";
import Plugin from "./plugin";
import { mergeOptions } from "./util";
import { createItemFromDescriptor } from "./item";
import { buildRootChain, type ConfigContext } from "./config-chain";
import {
buildRootChain,
type ConfigContext,
type FileHandling,
} from "./config-chain";
import { getEnv } from "./helpers/environment";
import {
validate,
Expand Down Expand Up @@ -59,9 +63,11 @@ function* resolveRootMode(
type PrivPartialConfig = {
options: ValidatedOptions,
context: ConfigContext,
fileHandling: FileHandling,
ignore: IgnoreFile | void,
babelrc: ConfigFile | void,
config: ConfigFile | void,
files: Set<string>,
};

export default function* loadPrivatePartialConfig(
Expand Down Expand Up @@ -137,20 +143,30 @@ export default function* loadPrivatePartialConfig(
return {
options,
context,
fileHandling: configChain.fileHandling,
ignore: configChain.ignore,
babelrc: configChain.babelrc,
config: configChain.config,
files: configChain.files,
};
}

type LoadPartialConfigOpts = {
showIgnoredFiles?: boolean,
...
};

export const loadPartialConfig = gensync<[any], PartialConfig | null>(
function* (inputOpts: mixed): Handler<PartialConfig | null> {
const result: ?PrivPartialConfig = yield* loadPrivatePartialConfig(
inputOpts,
);
function* (inputOpts: LoadPartialConfigOpts): Handler<PartialConfig | null> {
const { showIgnoredFiles, ...opts } = inputOpts;
const result: ?PrivPartialConfig = yield* loadPrivatePartialConfig(opts);
if (!result) return null;

const { options, babelrc, ignore, config } = result;
const { options, babelrc, ignore, config, fileHandling, files } = result;

if (fileHandling === "ignored" && !showIgnoredFiles) {
return null;
}

(options.plugins || []).forEach(item => {
if (item.value instanceof Plugin) {
Expand All @@ -166,6 +182,8 @@ export const loadPartialConfig = gensync<[any], PartialConfig | null>(
babelrc ? babelrc.filepath : undefined,
ignore ? ignore.filepath : undefined,
config ? config.filepath : undefined,
fileHandling,
files,
);
},
);
Expand All @@ -181,17 +199,23 @@ class PartialConfig {
babelrc: string | void;
babelignore: string | void;
config: string | void;
fileHandling: FileHandling;
files: Set<string>;

constructor(
options: ValidatedOptions,
babelrc: string | void,
ignore: string | void,
config: string | void,
fileHandling: FileHandling,
files: Set<string>,
) {
this.options = options;
this.babelignore = ignore;
this.babelrc = babelrc;
this.config = config;
this.fileHandling = fileHandling;
this.files = files;

// Freeze since this is a public API and it should be extremely obvious that
// reassigning properties on here does nothing.
Expand Down
59 changes: 59 additions & 0 deletions packages/babel-core/test/config-chain.js
Expand Up @@ -1222,6 +1222,65 @@ describe("buildConfigChain", function () {
loadOptionsAsync({ filename, cwd: path.dirname(filename) }),
).rejects.toThrow(error);
});

it("loadPartialConfig should return a list of files that were extended", () => {
const filename = fixture("config-files", "babelrc-extended", "src.js");

expect(
babel.loadPartialConfig({ filename, cwd: path.dirname(filename) }),
).toEqual({
babelignore: fixture("config-files", ".babelignore"),
babelrc: fixture("config-files", "babelrc-extended", ".babelrc"),
config: undefined,
fileHandling: "transpile",
options: {
...getDefaults(),
filename: filename,
cwd: path.dirname(filename),
root: path.dirname(filename),
comments: true,
},
files: new Set([
fixture("config-files", ".babelignore"),
fixture("config-files", "babelrc-extended", ".babelrc-extended"),
fixture("config-files", "babelrc-extended", ".babelrc"),
]),
});
});

it("loadPartialConfig should return null when ignored", () => {
const filename = fixture("config-files", "babelignore", "src.js");

expect(
babel.loadPartialConfig({ filename, cwd: path.dirname(filename) }),
).toBeNull();
});

it("loadPartialConfig should return a list of files when ignored with showIgnoredFiles option", () => {
const filename = fixture("config-files", "babelignore", "src.js");

expect(
babel.loadPartialConfig({
filename,
cwd: path.dirname(filename),
showIgnoredFiles: true,
}),
).toEqual({
babelignore: fixture("config-files", "babelignore", ".babelignore"),
babelrc: undefined,
config: undefined,
fileHandling: "ignored",
options: {
...getDefaults(),
filename: filename,
cwd: path.dirname(filename),
root: path.dirname(filename),
},
files: new Set([
fixture("config-files", "babelignore", ".babelignore"),
]),
});
});
});

it("should throw when `test` presents but `filename` is not passed", () => {
Expand Down
@@ -0,0 +1,3 @@
{
"extends": "./.babelrc-extended"
}
@@ -0,0 +1,3 @@
{
"comments": true
}