| 1 |
/* eslint-disable @typescript-eslint/no-use-before-define */ |
| 2 |
|
| 3 |
import path from "path"; |
| 4 |
import buildDebug from "debug"; |
| 5 |
import type { Handler } from "gensync"; |
| 6 |
import { validate } from "./validation/options"; |
| 7 |
import type { |
| 8 |
ValidatedOptions, |
| 9 |
IgnoreList, |
| 10 |
ConfigApplicableTest, |
| 11 |
BabelrcSearch, |
| 12 |
CallerMetadata, |
| 13 |
IgnoreItem, |
| 14 |
} from "./validation/options"; |
| 15 |
import pathPatternToRegex from "./pattern-to-regex"; |
| 16 |
import { ConfigPrinter, ChainFormatter } from "./printer"; |
| 17 |
import type { ReadonlyDeepArray } from "./helpers/deep-array"; |
| 18 |
|
| 19 |
import { endHiddenCallStack } from "../errors/rewrite-stack-trace"; |
| 20 |
import ConfigError from "../errors/config-error"; |
| 21 |
|
| 22 |
const debug = buildDebug("babel:config:config-chain"); |
| 23 |
|
| 24 |
import { |
| 25 |
findPackageData, |
| 26 |
findRelativeConfig, |
| 27 |
findRootConfig, |
| 28 |
loadConfig, |
| 29 |
} from "./files"; |
| 30 |
import type { ConfigFile, IgnoreFile, FilePackageData } from "./files"; |
| 31 |
|
| 32 |
import { makeWeakCacheSync, makeStrongCacheSync } from "./caching"; |
| 33 |
|
| 34 |
import { |
| 35 |
createCachedDescriptors, |
| 36 |
createUncachedDescriptors, |
| 37 |
} from "./config-descriptors"; |
| 38 |
import type { |
| 39 |
UnloadedDescriptor, |
| 40 |
OptionsAndDescriptors, |
| 41 |
ValidatedFile, |
| 42 |
} from "./config-descriptors"; |
| 43 |
|
| 44 |
export type ConfigChain = { |
| 45 |
plugins: Array<UnloadedDescriptor>; |
| 46 |
presets: Array<UnloadedDescriptor>; |
| 47 |
options: Array<ValidatedOptions>; |
| 48 |
files: Set<string>; |
| 49 |
}; |
| 50 |
|
| 51 |
export type PresetInstance = { |
| 52 |
options: ValidatedOptions; |
| 53 |
alias: string; |
| 54 |
dirname: string; |
| 55 |
externalDependencies: ReadonlyDeepArray<string>; |
| 56 |
}; |
| 57 |
|
| 58 |
export type ConfigContext = { |
| 59 |
filename: string | undefined; |
| 60 |
cwd: string; |
| 61 |
root: string; |
| 62 |
envName: string; |
| 63 |
caller: CallerMetadata | undefined; |
| 64 |
showConfig: boolean; |
| 65 |
}; |
| 66 |
|
| 67 |
/** |
| 68 |
* Build a config chain for a given preset. |
| 69 |
*/ |
| 70 |
export function* buildPresetChain( |
| 71 |
arg: PresetInstance, |
| 72 |
context: any, |
| 73 |
): Handler<ConfigChain | null> { |
| 74 |
const chain = yield* buildPresetChainWalker(arg, context); |
| 75 |
if (!chain) return null; |
| 76 |
|
| 77 |
return { |
| 78 |
plugins: dedupDescriptors(chain.plugins), |
| 79 |
presets: dedupDescriptors(chain.presets), |
| 80 |
options: chain.options.map(o => normalizeOptions(o)), |
| 81 |
files: new Set(), |
| 82 |
}; |
| 83 |
} |
| 84 |
|
| 85 |
export const buildPresetChainWalker = makeChainWalker<PresetInstance>({ |
| 86 |
root: preset => loadPresetDescriptors(preset), |
| 87 |
env: (preset, envName) => loadPresetEnvDescriptors(preset)(envName), |
| 88 |
overrides: (preset, index) => loadPresetOverridesDescriptors(preset)(index), |
| 89 |
overridesEnv: (preset, index, envName) => |
| 90 |
loadPresetOverridesEnvDescriptors(preset)(index)(envName), |
| 91 |
createLogger: () => () => {}, // Currently we don't support logging how preset is expanded |
| 92 |
}); |
| 93 |
const loadPresetDescriptors = makeWeakCacheSync((preset: PresetInstance) => |
| 94 |
buildRootDescriptors(preset, preset.alias, createUncachedDescriptors), |
| 95 |
); |
| 96 |
const loadPresetEnvDescriptors = makeWeakCacheSync((preset: PresetInstance) => |
| 97 |
makeStrongCacheSync((envName: string) => |
| 98 |
buildEnvDescriptors( |
| 99 |
preset, |
| 100 |
preset.alias, |
| 101 |
createUncachedDescriptors, |
| 102 |
envName, |
| 103 |
), |
| 104 |
), |
| 105 |
); |
| 106 |
const loadPresetOverridesDescriptors = makeWeakCacheSync( |
| 107 |
(preset: PresetInstance) => |
| 108 |
makeStrongCacheSync((index: number) => |
| 109 |
buildOverrideDescriptors( |
| 110 |
preset, |
| 111 |
preset.alias, |
| 112 |
createUncachedDescriptors, |
| 113 |
index, |
| 114 |
), |
| 115 |
), |
| 116 |
); |
| 117 |
const loadPresetOverridesEnvDescriptors = makeWeakCacheSync( |
| 118 |
(preset: PresetInstance) => |
| 119 |
makeStrongCacheSync((index: number) => |
| 120 |
makeStrongCacheSync((envName: string) => |
| 121 |
buildOverrideEnvDescriptors( |
| 122 |
preset, |
| 123 |
preset.alias, |
| 124 |
createUncachedDescriptors, |
| 125 |
index, |
| 126 |
envName, |
| 127 |
), |
| 128 |
), |
| 129 |
), |
| 130 |
); |
| 131 |
|
| 132 |
export type FileHandling = "transpile" | "ignored" | "unsupported"; |
| 133 |
export type RootConfigChain = ConfigChain & { |
| 134 |
babelrc: ConfigFile | void; |
| 135 |
config: ConfigFile | void; |
| 136 |
ignore: IgnoreFile | void; |
| 137 |
fileHandling: FileHandling; |
| 138 |
files: Set<string>; |
| 139 |
}; |
| 140 |
|
| 141 |
/** |
| 142 |
* Build a config chain for Babel's full root configuration. |
| 143 |
*/ |
| 144 |
export function* buildRootChain( |
| 145 |
opts: ValidatedOptions, |
| 146 |
context: ConfigContext, |
| 147 |
): Handler<RootConfigChain | null> { |
| 148 |
let configReport, babelRcReport; |
| 149 |
const programmaticLogger = new ConfigPrinter(); |
| 150 |
const programmaticChain = yield* loadProgrammaticChain( |
| 151 |
{ |
| 152 |
options: opts, |
| 153 |
dirname: context.cwd, |
| 154 |
}, |
| 155 |
context, |
| 156 |
undefined, |
| 157 |
programmaticLogger, |
| 158 |
); |
| 159 |
if (!programmaticChain) return null; |
| 160 |
const programmaticReport = yield* programmaticLogger.output(); |
| 161 |
|
| 162 |
let configFile; |
| 163 |
if (typeof opts.configFile === "string") { |
| 164 |
configFile = yield* loadConfig( |
| 165 |
opts.configFile, |
| 166 |
context.cwd, |
| 167 |
context.envName, |
| 168 |
context.caller, |
| 169 |
); |
| 170 |
} else if (opts.configFile !== false) { |
| 171 |
configFile = yield* findRootConfig( |
| 172 |
context.root, |
| 173 |
context.envName, |
| 174 |
context.caller, |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
let { babelrc, babelrcRoots } = opts; |
| 179 |
let babelrcRootsDirectory = context.cwd; |
| 180 |
|
| 181 |
const configFileChain = emptyChain(); |
| 182 |
const configFileLogger = new ConfigPrinter(); |
| 183 |
if (configFile) { |
| 184 |
const validatedFile = validateConfigFile(configFile); |
| 185 |
const result = yield* loadFileChain( |
| 186 |
validatedFile, |
| 187 |
context, |
| 188 |
undefined, |
| 189 |
configFileLogger, |
| 190 |
); |
| 191 |
if (!result) return null; |
| 192 |
configReport = yield* configFileLogger.output(); |
| 193 |
|
| 194 |
// Allow config files to toggle `.babelrc` resolution on and off and |
| 195 |
// specify where the roots are. |
| 196 |
if (babelrc === undefined) { |
| 197 |
babelrc = validatedFile.options.babelrc; |
| 198 |
} |
| 199 |
if (babelrcRoots === undefined) { |
| 200 |
babelrcRootsDirectory = validatedFile.dirname; |
| 201 |
babelrcRoots = validatedFile.options.babelrcRoots; |
| 202 |
} |
| 203 |
|
| 204 |
mergeChain(configFileChain, result); |
| 205 |
} |
| 206 |
|
| 207 |
let ignoreFile, babelrcFile; |
| 208 |
let isIgnored = false; |
| 209 |
const fileChain = emptyChain(); |
| 210 |
// resolve all .babelrc files |
| 211 |
if ( |
| 212 |
(babelrc === true || babelrc === undefined) && |
| 213 |
typeof context.filename === "string" |
| 214 |
) { |
| 215 |
const pkgData = yield* findPackageData(context.filename); |
| 216 |
|
| 217 |
if ( |
| 218 |
pkgData && |
| 219 |
babelrcLoadEnabled(context, pkgData, babelrcRoots, babelrcRootsDirectory) |
| 220 |
) { |
| 221 |
({ ignore: ignoreFile, config: babelrcFile } = yield* findRelativeConfig( |
| 222 |
pkgData, |
| 223 |
context.envName, |
| 224 |
context.caller, |
| 225 |
)); |
| 226 |
|
| 227 |
if (ignoreFile) { |
| 228 |
fileChain.files.add(ignoreFile.filepath); |
| 229 |
} |
| 230 |
|
| 231 |
if ( |
| 232 |
ignoreFile && |
| 233 |
shouldIgnore(context, ignoreFile.ignore, null, ignoreFile.dirname) |
| 234 |
) { |
| 235 |
isIgnored = true; |
| 236 |
} |
| 237 |
|
| 238 |
if (babelrcFile && !isIgnored) { |
| 239 |
const validatedFile = validateBabelrcFile(babelrcFile); |
| 240 |
const babelrcLogger = new ConfigPrinter(); |
| 241 |
const result = yield* loadFileChain( |
| 242 |
validatedFile, |
| 243 |
context, |
| 244 |
undefined, |
| 245 |
babelrcLogger, |
| 246 |
); |
| 247 |
if (!result) { |
| 248 |
isIgnored = true; |
| 249 |
} else { |
| 250 |
babelRcReport = yield* babelrcLogger.output(); |
| 251 |
mergeChain(fileChain, result); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
if (babelrcFile && isIgnored) { |
| 256 |
fileChain.files.add(babelrcFile.filepath); |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if (context.showConfig) { |
| 262 |
console.log( |
| 263 |
`Babel configs on "${context.filename}" (ascending priority):\n` + |
| 264 |
// print config by the order of ascending priority |
| 265 |
[configReport, babelRcReport, programmaticReport] |
| 266 |
.filter(x => !!x) |
| 267 |
.join("\n\n") + |
| 268 |
"\n-----End Babel configs-----", |
| 269 |
); |
| 270 |
} |
| 271 |
// Insert file chain in front so programmatic options have priority |
| 272 |
// over configuration file chain items. |
| 273 |
const chain = mergeChain( |
| 274 |
mergeChain(mergeChain(emptyChain(), configFileChain), fileChain), |
| 275 |
programmaticChain, |
| 276 |
); |
| 277 |
|
| 278 |
return { |
| 279 |
plugins: isIgnored ? [] : dedupDescriptors(chain.plugins), |
| 280 |
presets: isIgnored ? [] : dedupDescriptors(chain.presets), |
| 281 |
options: isIgnored ? [] : chain.options.map(o => normalizeOptions(o)), |
| 282 |
fileHandling: isIgnored ? "ignored" : "transpile", |
| 283 |
ignore: ignoreFile || undefined, |
| 284 |
babelrc: babelrcFile || undefined, |
| 285 |
config: configFile || undefined, |
| 286 |
files: chain.files, |
| 287 |
}; |
| 288 |
} |
| 289 |
|
| 290 |
function babelrcLoadEnabled( |
| 291 |
context: ConfigContext, |
| 292 |
pkgData: FilePackageData, |
| 293 |
babelrcRoots: BabelrcSearch | undefined, |
| 294 |
babelrcRootsDirectory: string, |
| 295 |
): boolean { |
| 296 |
if (typeof babelrcRoots === "boolean") return babelrcRoots; |
| 297 |
|
| 298 |
const absoluteRoot = context.root; |
| 299 |
|
| 300 |
// Fast path to avoid having to match patterns if the babelrc is just |
| 301 |
// loading in the standard root directory. |
| 302 |
if (babelrcRoots === undefined) { |
| 303 |
return pkgData.directories.indexOf(absoluteRoot) !== -1; |
| 304 |
} |
| 305 |
|
| 306 |
let babelrcPatterns = babelrcRoots; |
| 307 |
if (!Array.isArray(babelrcPatterns)) { |
| 308 |
babelrcPatterns = [babelrcPatterns as IgnoreItem]; |
| 309 |
} |
| 310 |
babelrcPatterns = babelrcPatterns.map(pat => { |
| 311 |
return typeof pat === "string" |
| 312 |
? path.resolve(babelrcRootsDirectory, pat) |
| 313 |
: pat; |
| 314 |
}); |
| 315 |
|
| 316 |
// Fast path to avoid having to match patterns if the babelrc is just |
| 317 |
// loading in the standard root directory. |
| 318 |
if (babelrcPatterns.length === 1 && babelrcPatterns[0] === absoluteRoot) { |
| 319 |
return pkgData.directories.indexOf(absoluteRoot) !== -1; |
| 320 |
} |
| 321 |
|
| 322 |
return babelrcPatterns.some(pat => { |
| 323 |
if (typeof pat === "string") { |
| 324 |
pat = pathPatternToRegex(pat, babelrcRootsDirectory); |
| 325 |
} |
| 326 |
|
| 327 |
return pkgData.directories.some(directory => { |
| 328 |
return matchPattern(pat, babelrcRootsDirectory, directory, context); |
| 329 |
}); |
| 330 |
}); |
| 331 |
} |
| 332 |
|
| 333 |
const validateConfigFile = makeWeakCacheSync( |
| 334 |
(file: ConfigFile): ValidatedFile => ({ |
| 335 |
filepath: file.filepath, |
| 336 |
dirname: file.dirname, |
| 337 |
options: validate("configfile", file.options, file.filepath), |
| 338 |
}), |
| 339 |
); |
| 340 |
|
| 341 |
const validateBabelrcFile = makeWeakCacheSync( |
| 342 |
(file: ConfigFile): ValidatedFile => ({ |
| 343 |
filepath: file.filepath, |
| 344 |
dirname: file.dirname, |
| 345 |
options: validate("babelrcfile", file.options, file.filepath), |
| 346 |
}), |
| 347 |
); |
| 348 |
|
| 349 |
const validateExtendFile = makeWeakCacheSync( |
| 350 |
(file: ConfigFile): ValidatedFile => ({ |
| 351 |
filepath: file.filepath, |
| 352 |
dirname: file.dirname, |
| 353 |
options: validate("extendsfile", file.options, file.filepath), |
| 354 |
}), |
| 355 |
); |
| 356 |
|
| 357 |
/** |
| 358 |
* Build a config chain for just the programmatic options passed into Babel. |
| 359 |
*/ |
| 360 |
const loadProgrammaticChain = makeChainWalker({ |
| 361 |
root: input => buildRootDescriptors(input, "base", createCachedDescriptors), |
| 362 |
env: (input, envName) => |
| 363 |
buildEnvDescriptors(input, "base", createCachedDescriptors, envName), |
| 364 |
overrides: (input, index) => |
| 365 |
buildOverrideDescriptors(input, "base", createCachedDescriptors, index), |
| 366 |
overridesEnv: (input, index, envName) => |
| 367 |
buildOverrideEnvDescriptors( |
| 368 |
input, |
| 369 |
"base", |
| 370 |
createCachedDescriptors, |
| 371 |
index, |
| 372 |
envName, |
| 373 |
), |
| 374 |
createLogger: (input, context, baseLogger) => |
| 375 |
buildProgrammaticLogger(input, context, baseLogger), |
| 376 |
}); |
| 377 |
|
| 378 |
/** |
| 379 |
* Build a config chain for a given file. |
| 380 |
*/ |
| 381 |
const loadFileChainWalker = makeChainWalker<ValidatedFile>({ |
| 382 |
root: file => loadFileDescriptors(file), |
| 383 |
env: (file, envName) => loadFileEnvDescriptors(file)(envName), |
| 384 |
overrides: (file, index) => loadFileOverridesDescriptors(file)(index), |
| 385 |
overridesEnv: (file, index, envName) => |
| 386 |
loadFileOverridesEnvDescriptors(file)(index)(envName), |
| 387 |
createLogger: (file, context, baseLogger) => |
| 388 |
buildFileLogger(file.filepath, context, baseLogger), |
| 389 |
}); |
| 390 |
|
| 391 |
function* loadFileChain( |
| 392 |
input: ValidatedFile, |
| 393 |
context: ConfigContext, |
| 394 |
files: Set<ConfigFile>, |
| 395 |
baseLogger: ConfigPrinter, |
| 396 |
) { |
| 397 |
const chain = yield* loadFileChainWalker(input, context, files, baseLogger); |
| 398 |
if (chain) { |
| 399 |
chain.files.add(input.filepath); |
| 400 |
} |
| 401 |
|
| 402 |
return chain; |
| 403 |
} |
| 404 |
|
| 405 |
const loadFileDescriptors = makeWeakCacheSync((file: ValidatedFile) => |
| 406 |
buildRootDescriptors(file, file.filepath, createUncachedDescriptors), |
| 407 |
); |
| 408 |
const loadFileEnvDescriptors = makeWeakCacheSync((file: ValidatedFile) => |
| 409 |
makeStrongCacheSync((envName: string) => |
| 410 |
buildEnvDescriptors( |
| 411 |
file, |
| 412 |
file.filepath, |
| 413 |
createUncachedDescriptors, |
| 414 |
envName, |
| 415 |
), |
| 416 |
), |
| 417 |
); |
| 418 |
const loadFileOverridesDescriptors = makeWeakCacheSync((file: ValidatedFile) => |
| 419 |
makeStrongCacheSync((index: number) => |
| 420 |
buildOverrideDescriptors( |
| 421 |
file, |
| 422 |
file.filepath, |
| 423 |
createUncachedDescriptors, |
| 424 |
index, |
| 425 |
), |
| 426 |
), |
| 427 |
); |
| 428 |
const loadFileOverridesEnvDescriptors = makeWeakCacheSync( |
| 429 |
(file: ValidatedFile) => |
| 430 |
makeStrongCacheSync((index: number) => |
| 431 |
makeStrongCacheSync((envName: string) => |
| 432 |
buildOverrideEnvDescriptors( |
| 433 |
file, |
| 434 |
file.filepath, |
| 435 |
createUncachedDescriptors, |
| 436 |
index, |
| 437 |
envName, |
| 438 |
), |
| 439 |
), |
| 440 |
), |
| 441 |
); |
| 442 |
|
| 443 |
function buildFileLogger( |
| 444 |
filepath: string, |
| 445 |
context: ConfigContext, |
| 446 |
baseLogger: ConfigPrinter | void, |
| 447 |
) { |
| 448 |
if (!baseLogger) { |
| 449 |
return () => {}; |
| 450 |
} |
| 451 |
return baseLogger.configure(context.showConfig, ChainFormatter.Config, { |
| 452 |
filepath, |
| 453 |
}); |
| 454 |
} |
| 455 |
|
| 456 |
function buildRootDescriptors( |
| 457 |
{ dirname, options }: Partial<ValidatedFile>, |
| 458 |
alias: string, |
| 459 |
descriptors: ( |
| 460 |
dirname: string, |
| 461 |
options: ValidatedOptions, |
| 462 |
alias: string, |
| 463 |
) => OptionsAndDescriptors, |
| 464 |
) { |
| 465 |
return descriptors(dirname, options, alias); |
| 466 |
} |
| 467 |
|
| 468 |
function buildProgrammaticLogger( |
| 469 |
_: unknown, |
| 470 |
context: ConfigContext, |
| 471 |
baseLogger: ConfigPrinter | void, |
| 472 |
) { |
| 473 |
if (!baseLogger) { |
| 474 |
return () => {}; |
| 475 |
} |
| 476 |
return baseLogger.configure(context.showConfig, ChainFormatter.Programmatic, { |
| 477 |
callerName: context.caller?.name, |
| 478 |
}); |
| 479 |
} |
| 480 |
|
| 481 |
function buildEnvDescriptors( |
| 482 |
{ dirname, options }: Partial<ValidatedFile>, |
| 483 |
alias: string, |
| 484 |
descriptors: ( |
| 485 |
dirname: string, |
| 486 |
options: ValidatedOptions, |
| 487 |
alias: string, |
| 488 |
) => OptionsAndDescriptors, |
| 489 |
envName: string, |
| 490 |
) { |
| 491 |
const opts = options.env && options.env[envName]; |
| 492 |
return opts ? descriptors(dirname, opts, `${alias}.env["${envName}"]`) : null; |
| 493 |
} |
| 494 |
|
| 495 |
function buildOverrideDescriptors( |
| 496 |
{ dirname, options }: Partial<ValidatedFile>, |
| 497 |
alias: string, |
| 498 |
descriptors: ( |
| 499 |
dirname: string, |
| 500 |
options: ValidatedOptions, |
| 501 |
alias: string, |
| 502 |
) => OptionsAndDescriptors, |
| 503 |
index: number, |
| 504 |
) { |
| 505 |
const opts = options.overrides && options.overrides[index]; |
| 506 |
if (!opts) throw new Error("Assertion failure - missing override"); |
| 507 |
|
| 508 |
return descriptors(dirname, opts, `${alias}.overrides[${index}]`); |
| 509 |
} |
| 510 |
|
| 511 |
function buildOverrideEnvDescriptors( |
| 512 |
{ dirname, options }: Partial<ValidatedFile>, |
| 513 |
alias: string, |
| 514 |
descriptors: ( |
| 515 |
dirname: string, |
| 516 |
options: ValidatedOptions, |
| 517 |
alias: string, |
| 518 |
) => OptionsAndDescriptors, |
| 519 |
index: number, |
| 520 |
envName: string, |
| 521 |
) { |
| 522 |
const override = options.overrides && options.overrides[index]; |
| 523 |
if (!override) throw new Error("Assertion failure - missing override"); |
| 524 |
|
| 525 |
const opts = override.env && override.env[envName]; |
| 526 |
return opts |
| 527 |
? descriptors( |
| 528 |
dirname, |
| 529 |
opts, |
| 530 |
`${alias}.overrides[${index}].env["${envName}"]`, |
| 531 |
) |
| 532 |
: null; |
| 533 |
} |
| 534 |
|
| 535 |
function makeChainWalker< |
| 536 |
ArgT extends { |
| 537 |
options: ValidatedOptions; |
| 538 |
dirname: string; |
| 539 |
filepath?: string; |
| 540 |
}, |
| 541 |
>({ |
| 542 |
root, |
| 543 |
env, |
| 544 |
overrides, |
| 545 |
overridesEnv, |
| 546 |
createLogger, |
| 547 |
}: { |
| 548 |
root: (configEntry: ArgT) => OptionsAndDescriptors; |
| 549 |
env: (configEntry: ArgT, env: string) => OptionsAndDescriptors | null; |
| 550 |
overrides: (configEntry: ArgT, index: number) => OptionsAndDescriptors; |
| 551 |
overridesEnv: ( |
| 552 |
configEntry: ArgT, |
| 553 |
index: number, |
| 554 |
env: string, |
| 555 |
) => OptionsAndDescriptors | null; |
| 556 |
createLogger: ( |
| 557 |
configEntry: ArgT, |
| 558 |
context: ConfigContext, |
| 559 |
printer: ConfigPrinter | void, |
| 560 |
) => ( |
| 561 |
opts: OptionsAndDescriptors, |
| 562 |
index?: number | null, |
| 563 |
env?: string | null, |
| 564 |
) => void; |
| 565 |
}): ( |
| 566 |
configEntry: ArgT, |
| 567 |
context: ConfigContext, |
| 568 |
files?: Set<ConfigFile>, |
| 569 |
baseLogger?: ConfigPrinter, |
| 570 |
) => Handler<ConfigChain | null> { |
| 571 |
return function* chainWalker(input, context, files = new Set(), baseLogger) { |
| 572 |
const { dirname } = input; |
| 573 |
|
| 574 |
const flattenedConfigs: Array<{ |
| 575 |
config: OptionsAndDescriptors; |
| 576 |
index: number | undefined | null; |
| 577 |
envName: string | undefined | null; |
| 578 |
}> = []; |
| 579 |
|
| 580 |
const rootOpts = root(input); |
| 581 |
if (configIsApplicable(rootOpts, dirname, context, input.filepath)) { |
| 582 |
flattenedConfigs.push({ |
| 583 |
config: rootOpts, |
| 584 |
envName: undefined, |
| 585 |
index: undefined, |
| 586 |
}); |
| 587 |
|
| 588 |
const envOpts = env(input, context.envName); |
| 589 |
if ( |
| 590 |
envOpts && |
| 591 |
configIsApplicable(envOpts, dirname, context, input.filepath) |
| 592 |
) { |
| 593 |
flattenedConfigs.push({ |
| 594 |
config: envOpts, |
| 595 |
envName: context.envName, |
| 596 |
index: undefined, |
| 597 |
}); |
| 598 |
} |
| 599 |
|
| 600 |
(rootOpts.options.overrides || []).forEach((_, index) => { |
| 601 |
const overrideOps = overrides(input, index); |
| 602 |
if (configIsApplicable(overrideOps, dirname, context, input.filepath)) { |
| 603 |
flattenedConfigs.push({ |
| 604 |
config: overrideOps, |
| 605 |
index, |
| 606 |
envName: undefined, |
| 607 |
}); |
| 608 |
|
| 609 |
const overrideEnvOpts = overridesEnv(input, index, context.envName); |
| 610 |
if ( |
| 611 |
overrideEnvOpts && |
| 612 |
configIsApplicable( |
| 613 |
overrideEnvOpts, |
| 614 |
dirname, |
| 615 |
context, |
| 616 |
input.filepath, |
| 617 |
) |
| 618 |
) { |
| 619 |
flattenedConfigs.push({ |
| 620 |
config: overrideEnvOpts, |
| 621 |
index, |
| 622 |
envName: context.envName, |
| 623 |
}); |
| 624 |
} |
| 625 |
} |
| 626 |
}); |
| 627 |
} |
| 628 |
|
| 629 |
// Process 'ignore' and 'only' before 'extends' items are processed so |
| 630 |
// that we don't do extra work loading extended configs if a file is |
| 631 |
// ignored. |
| 632 |
if ( |
| 633 |
flattenedConfigs.some( |
| 634 |
({ |
| 635 |
config: { |
| 636 |
options: { ignore, only }, |
| 637 |
}, |
| 638 |
}) => shouldIgnore(context, ignore, only, dirname), |
| 639 |
) |
| 640 |
) { |
| 641 |
return null; |
| 642 |
} |
| 643 |
|
| 644 |
const chain = emptyChain(); |
| 645 |
const logger = createLogger(input, context, baseLogger); |
| 646 |
|
| 647 |
for (const { config, index, envName } of flattenedConfigs) { |
| 648 |
if ( |
| 649 |
!(yield* mergeExtendsChain( |
| 650 |
chain, |
| 651 |
config.options, |
| 652 |
dirname, |
| 653 |
context, |
| 654 |
files, |
| 655 |
baseLogger, |
| 656 |
)) |
| 657 |
) { |
| 658 |
return null; |
| 659 |
} |
| 660 |
|
| 661 |
logger(config, index, envName); |
| 662 |
yield* mergeChainOpts(chain, config); |
| 663 |
} |
| 664 |
return chain; |
| 665 |
}; |
| 666 |
} |
| 667 |
|
| 668 |
function* mergeExtendsChain( |
| 669 |
chain: ConfigChain, |
| 670 |
opts: ValidatedOptions, |
| 671 |
dirname: string, |
| 672 |
context: ConfigContext, |
| 673 |
files: Set<ConfigFile>, |
| 674 |
baseLogger?: ConfigPrinter, |
| 675 |
): Handler<boolean> { |
| 676 |
if (opts.extends === undefined) return true; |
| 677 |
|
| 678 |
const file = yield* loadConfig( |
| 679 |
opts.extends, |
| 680 |
dirname, |
| 681 |
context.envName, |
| 682 |
context.caller, |
| 683 |
); |
| 684 |
|
| 685 |
if (files.has(file)) { |
| 686 |
throw new Error( |
| 687 |
`Configuration cycle detected loading ${file.filepath}.\n` + |
| 688 |
`File already loaded following the config chain:\n` + |
| 689 |
Array.from(files, file => ` - ${file.filepath}`).join("\n"), |
| 690 |
); |
| 691 |
} |
| 692 |
|
| 693 |
files.add(file); |
| 694 |
const fileChain = yield* loadFileChain( |
| 695 |
validateExtendFile(file), |
| 696 |
context, |
| 697 |
files, |
| 698 |
baseLogger, |
| 699 |
); |
| 700 |
files.delete(file); |
| 701 |
|
| 702 |
if (!fileChain) return false; |
| 703 |
|
| 704 |
mergeChain(chain, fileChain); |
| 705 |
|
| 706 |
return true; |
| 707 |
} |
| 708 |
|
| 709 |
function mergeChain(target: ConfigChain, source: ConfigChain): ConfigChain { |
| 710 |
target.options.push(...source.options); |
| 711 |
target.plugins.push(...source.plugins); |
| 712 |
target.presets.push(...source.presets); |
| 713 |
for (const file of source.files) { |
| 714 |
target.files.add(file); |
| 715 |
} |
| 716 |
|
| 717 |
return target; |
| 718 |
} |
| 719 |
|
| 720 |
function* mergeChainOpts( |
| 721 |
target: ConfigChain, |
| 722 |
{ options, plugins, presets }: OptionsAndDescriptors, |
| 723 |
): Handler<ConfigChain> { |
| 724 |
target.options.push(options); |
| 725 |
target.plugins.push(...(yield* plugins())); |
| 726 |
target.presets.push(...(yield* presets())); |
| 727 |
|
| 728 |
return target; |
| 729 |
} |
| 730 |
|
| 731 |
function emptyChain(): ConfigChain { |
| 732 |
return { |
| 733 |
options: [], |
| 734 |
presets: [], |
| 735 |
plugins: [], |
| 736 |
files: new Set(), |
| 737 |
}; |
| 738 |
} |
| 739 |
|
| 740 |
function normalizeOptions(opts: ValidatedOptions): ValidatedOptions { |
| 741 |
const options = { |
| 742 |
...opts, |
| 743 |
}; |
| 744 |
delete options.extends; |
| 745 |
delete options.env; |
| 746 |
delete options.overrides; |
| 747 |
delete options.plugins; |
| 748 |
delete options.presets; |
| 749 |
delete options.passPerPreset; |
| 750 |
delete options.ignore; |
| 751 |
delete options.only; |
| 752 |
delete options.test; |
| 753 |
delete options.include; |
| 754 |
delete options.exclude; |
| 755 |
|
| 756 |
// "sourceMap" is just aliased to sourceMap, so copy it over as |
| 757 |
// we merge the options together. |
| 758 |
if (Object.prototype.hasOwnProperty.call(options, "sourceMap")) { |
| 759 |
options.sourceMaps = options.sourceMap; |
| 760 |
delete options.sourceMap; |
| 761 |
} |
| 762 |
return options; |
| 763 |
} |
| 764 |
|
| 765 |
function dedupDescriptors( |
| 766 |
items: Array<UnloadedDescriptor>, |
| 767 |
): Array<UnloadedDescriptor> { |
| 768 |
const map: Map< |
| 769 |
Function, |
| 770 |
Map<string | void, { value: UnloadedDescriptor }> |
| 771 |
> = new Map(); |
| 772 |
|
| 773 |
const descriptors = []; |
| 774 |
|
| 775 |
for (const item of items) { |
| 776 |
if (typeof item.value === "function") { |
| 777 |
const fnKey = item.value; |
| 778 |
let nameMap = map.get(fnKey); |
| 779 |
if (!nameMap) { |
| 780 |
nameMap = new Map(); |
| 781 |
map.set(fnKey, nameMap); |
| 782 |
} |
| 783 |
let desc = nameMap.get(item.name); |
| 784 |
if (!desc) { |
| 785 |
desc = { value: item }; |
| 786 |
descriptors.push(desc); |
| 787 |
|
| 788 |
// Treat passPerPreset presets as unique, skipping them |
| 789 |
// in the merge processing steps. |
| 790 |
if (!item.ownPass) nameMap.set(item.name, desc); |
| 791 |
} else { |
| 792 |
desc.value = item; |
| 793 |
} |
| 794 |
} else { |
| 795 |
descriptors.push({ value: item }); |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
return descriptors.reduce((acc, desc) => { |
| 800 |
acc.push(desc.value); |
| 801 |
return acc; |
| 802 |
}, []); |
| 803 |
} |
| 804 |
|
| 805 |
function configIsApplicable( |
| 806 |
{ options }: OptionsAndDescriptors, |
| 807 |
dirname: string, |
| 808 |
context: ConfigContext, |
| 809 |
configName: string, |
| 810 |
): boolean { |
| 811 |
return ( |
| 812 |
(options.test === undefined || |
| 813 |
configFieldIsApplicable(context, options.test, dirname, configName)) && |
| 814 |
(options.include === undefined || |
| 815 |
configFieldIsApplicable(context, options.include, dirname, configName)) && |
| 816 |
(options.exclude === undefined || |
| 817 |
!configFieldIsApplicable(context, options.exclude, dirname, configName)) |
| 818 |
); |
| 819 |
} |
| 820 |
|
| 821 |
function configFieldIsApplicable( |
| 822 |
context: ConfigContext, |
| 823 |
test: ConfigApplicableTest, |
| 824 |
dirname: string, |
| 825 |
configName: string, |
| 826 |
): boolean { |
| 827 |
const patterns = Array.isArray(test) ? test : [test]; |
| 828 |
|
| 829 |
return matchesPatterns(context, patterns, dirname, configName); |
| 830 |
} |
| 831 |
|
| 832 |
/** |
| 833 |
* Print the ignoreList-values in a more helpful way than the default. |
| 834 |
*/ |
| 835 |
function ignoreListReplacer( |
| 836 |
_key: string, |
| 837 |
value: IgnoreList | IgnoreItem, |
| 838 |
): IgnoreList | IgnoreItem | string { |
| 839 |
if (value instanceof RegExp) { |
| 840 |
return String(value); |
| 841 |
} |
| 842 |
|
| 843 |
return value; |
| 844 |
} |
| 845 |
|
| 846 |
/** |
| 847 |
* Tests if a filename should be ignored based on "ignore" and "only" options. |
| 848 |
*/ |
| 849 |
function shouldIgnore( |
| 850 |
context: ConfigContext, |
| 851 |
ignore: IgnoreList | undefined | null, |
| 852 |
only: IgnoreList | undefined | null, |
| 853 |
dirname: string, |
| 854 |
): boolean { |
| 855 |
if (ignore && matchesPatterns(context, ignore, dirname)) { |
| 856 |
const message = `No config is applied to "${ |
| 857 |
context.filename ?? "(unknown)" |
| 858 |
}" because it matches one of \`ignore: ${JSON.stringify( |
| 859 |
ignore, |
| 860 |
ignoreListReplacer, |
| 861 |
)}\` from "${dirname}"`; |
| 862 |
debug(message); |
| 863 |
if (context.showConfig) { |
| 864 |
console.log(message); |
| 865 |
} |
| 866 |
return true; |
| 867 |
} |
| 868 |
|
| 869 |
if (only && !matchesPatterns(context, only, dirname)) { |
| 870 |
const message = `No config is applied to "${ |
| 871 |
context.filename ?? "(unknown)" |
| 872 |
}" because it fails to match one of \`only: ${JSON.stringify( |
| 873 |
only, |
| 874 |
ignoreListReplacer, |
| 875 |
)}\` from "${dirname}"`; |
| 876 |
debug(message); |
| 877 |
if (context.showConfig) { |
| 878 |
console.log(message); |
| 879 |
} |
| 880 |
return true; |
| 881 |
} |
| 882 |
|
| 883 |
return false; |
| 884 |
} |
| 885 |
|
| 886 |
/** |
| 887 |
* Returns result of calling function with filename if pattern is a function. |
| 888 |
* Otherwise returns result of matching pattern Regex with filename. |
| 889 |
*/ |
| 890 |
function matchesPatterns( |
| 891 |
context: ConfigContext, |
| 892 |
patterns: IgnoreList, |
| 893 |
dirname: string, |
| 894 |
configName?: string, |
| 895 |
): boolean { |
| 896 |
return patterns.some(pattern => |
| 897 |
matchPattern(pattern, dirname, context.filename, context, configName), |
| 898 |
); |
| 899 |
} |
| 900 |
|
| 901 |
function matchPattern( |
| 902 |
pattern: IgnoreItem, |
| 903 |
dirname: string, |
| 904 |
pathToTest: string | undefined, |
| 905 |
context: ConfigContext, |
| 906 |
configName?: string, |
| 907 |
): boolean { |
| 908 |
if (typeof pattern === "function") { |
| 909 |
return !!endHiddenCallStack(pattern)(pathToTest, { |
| 910 |
dirname, |
| 911 |
envName: context.envName, |
| 912 |
caller: context.caller, |
| 913 |
}); |
| 914 |
} |
| 915 |
|
| 916 |
if (typeof pathToTest !== "string") { |
| 917 |
throw new ConfigError( |
| 918 |
`Configuration contains string/RegExp pattern, but no filename was passed to Babel`, |
| 919 |
configName, |
| 920 |
); |
| 921 |
} |
| 922 |
|
| 923 |
if (typeof pattern === "string") { |
| 924 |
pattern = pathPatternToRegex(pattern, dirname); |
| 925 |
} |
| 926 |
return pattern.test(pathToTest); |
| 927 |
} |