| 1 |
import path from "path"; |
| 2 |
import gensync from "gensync"; |
| 3 |
import type { Handler } from "gensync"; |
| 4 |
import Plugin from "./plugin"; |
| 5 |
import { mergeOptions } from "./util"; |
| 6 |
import { createItemFromDescriptor } from "./item"; |
| 7 |
import { buildRootChain } from "./config-chain"; |
| 8 |
import type { ConfigContext, FileHandling } from "./config-chain"; |
| 9 |
import { getEnv } from "./helpers/environment"; |
| 10 |
import { validate } from "./validation/options"; |
| 11 |
|
| 12 |
import type { |
| 13 |
ValidatedOptions, |
| 14 |
NormalizedOptions, |
| 15 |
RootMode, |
| 16 |
} from "./validation/options"; |
| 17 |
|
| 18 |
import { |
| 19 |
findConfigUpwards, |
| 20 |
resolveShowConfigPath, |
| 21 |
ROOT_CONFIG_FILENAMES, |
| 22 |
} from "./files"; |
| 23 |
import type { ConfigFile, IgnoreFile } from "./files"; |
| 24 |
import { resolveTargets } from "./resolve-targets"; |
| 25 |
|
| 26 |
function resolveRootMode(rootDir: string, rootMode: RootMode): string { |
| 27 |
switch (rootMode) { |
| 28 |
case "root": |
| 29 |
return rootDir; |
| 30 |
|
| 31 |
case "upward-optional": { |
| 32 |
const upwardRootDir = findConfigUpwards(rootDir); |
| 33 |
return upwardRootDir === null ? rootDir : upwardRootDir; |
| 34 |
} |
| 35 |
|
| 36 |
case "upward": { |
| 37 |
const upwardRootDir = findConfigUpwards(rootDir); |
| 38 |
if (upwardRootDir !== null) return upwardRootDir; |
| 39 |
|
| 40 |
throw Object.assign( |
| 41 |
new Error( |
| 42 |
`Babel was run with rootMode:"upward" but a root could not ` + |
| 43 |
`be found when searching upward from "${rootDir}".\n` + |
| 44 |
`One of the following config files must be in the directory tree: ` + |
| 45 |
`"${ROOT_CONFIG_FILENAMES.join(", ")}".`, |
| 46 |
) as any, |
| 47 |
{ |
| 48 |
code: "BABEL_ROOT_NOT_FOUND", |
| 49 |
dirname: rootDir, |
| 50 |
}, |
| 51 |
); |
| 52 |
} |
| 53 |
default: |
| 54 |
throw new Error(`Assertion failure - unknown rootMode value.`); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
type PrivPartialConfig = { |
| 59 |
options: NormalizedOptions; |
| 60 |
context: ConfigContext; |
| 61 |
fileHandling: FileHandling; |
| 62 |
ignore: IgnoreFile | void; |
| 63 |
babelrc: ConfigFile | void; |
| 64 |
config: ConfigFile | void; |
| 65 |
files: Set<string>; |
| 66 |
}; |
| 67 |
|
| 68 |
export default function* loadPrivatePartialConfig( |
| 69 |
inputOpts: unknown, |
| 70 |
): Handler<PrivPartialConfig | null> { |
| 71 |
if ( |
| 72 |
inputOpts != null && |
| 73 |
(typeof inputOpts !== "object" || Array.isArray(inputOpts)) |
| 74 |
) { |
| 75 |
throw new Error("Babel options must be an object, null, or undefined"); |
| 76 |
} |
| 77 |
|
| 78 |
const args = inputOpts ? validate("arguments", inputOpts) : {}; |
| 79 |
|
| 80 |
const { |
| 81 |
envName = getEnv(), |
| 82 |
cwd = ".", |
| 83 |
root: rootDir = ".", |
| 84 |
rootMode = "root", |
| 85 |
caller, |
| 86 |
cloneInputAst = true, |
| 87 |
} = args; |
| 88 |
const absoluteCwd = path.resolve(cwd); |
| 89 |
const absoluteRootDir = resolveRootMode( |
| 90 |
path.resolve(absoluteCwd, rootDir), |
| 91 |
rootMode, |
| 92 |
); |
| 93 |
|
| 94 |
const filename = |
| 95 |
typeof args.filename === "string" |
| 96 |
? path.resolve(cwd, args.filename) |
| 97 |
: undefined; |
| 98 |
|
| 99 |
const showConfigPath = yield* resolveShowConfigPath(absoluteCwd); |
| 100 |
|
| 101 |
const context: ConfigContext = { |
| 102 |
filename, |
| 103 |
cwd: absoluteCwd, |
| 104 |
root: absoluteRootDir, |
| 105 |
envName, |
| 106 |
caller, |
| 107 |
showConfig: showConfigPath === filename, |
| 108 |
}; |
| 109 |
|
| 110 |
const configChain = yield* buildRootChain(args, context); |
| 111 |
if (!configChain) return null; |
| 112 |
|
| 113 |
const merged: ValidatedOptions = { |
| 114 |
assumptions: {}, |
| 115 |
}; |
| 116 |
configChain.options.forEach(opts => { |
| 117 |
mergeOptions(merged as any, opts); |
| 118 |
}); |
| 119 |
|
| 120 |
const options: NormalizedOptions = { |
| 121 |
...merged, |
| 122 |
targets: resolveTargets(merged, absoluteRootDir), |
| 123 |
|
| 124 |
// Tack the passes onto the object itself so that, if this object is |
| 125 |
// passed back to Babel a second time, it will be in the right structure |
| 126 |
// to not change behavior. |
| 127 |
cloneInputAst, |
| 128 |
babelrc: false, |
| 129 |
configFile: false, |
| 130 |
browserslistConfigFile: false, |
| 131 |
passPerPreset: false, |
| 132 |
envName: context.envName, |
| 133 |
cwd: context.cwd, |
| 134 |
root: context.root, |
| 135 |
rootMode: "root", |
| 136 |
filename: |
| 137 |
typeof context.filename === "string" ? context.filename : undefined, |
| 138 |
|
| 139 |
plugins: configChain.plugins.map(descriptor => |
| 140 |
createItemFromDescriptor(descriptor), |
| 141 |
), |
| 142 |
presets: configChain.presets.map(descriptor => |
| 143 |
createItemFromDescriptor(descriptor), |
| 144 |
), |
| 145 |
}; |
| 146 |
|
| 147 |
return { |
| 148 |
options, |
| 149 |
context, |
| 150 |
fileHandling: configChain.fileHandling, |
| 151 |
ignore: configChain.ignore, |
| 152 |
babelrc: configChain.babelrc, |
| 153 |
config: configChain.config, |
| 154 |
files: configChain.files, |
| 155 |
}; |
| 156 |
} |
| 157 |
|
| 158 |
type LoadPartialConfigOpts = { |
| 159 |
showIgnoredFiles?: boolean; |
| 160 |
}; |
| 161 |
|
| 162 |
export const loadPartialConfig = gensync(function* ( |
| 163 |
opts?: LoadPartialConfigOpts, |
| 164 |
): Handler<PartialConfig | null> { |
| 165 |
let showIgnoredFiles = false; |
| 166 |
// We only extract showIgnoredFiles if opts is an object, so that |
| 167 |
// loadPrivatePartialConfig can throw the appropriate error if it's not. |
| 168 |
if (typeof opts === "object" && opts !== null && !Array.isArray(opts)) { |
| 169 |
({ showIgnoredFiles, ...opts } = opts); |
| 170 |
} |
| 171 |
|
| 172 |
const result: PrivPartialConfig | undefined | null = |
| 173 |
yield* loadPrivatePartialConfig(opts); |
| 174 |
if (!result) return null; |
| 175 |
|
| 176 |
const { options, babelrc, ignore, config, fileHandling, files } = result; |
| 177 |
|
| 178 |
if (fileHandling === "ignored" && !showIgnoredFiles) { |
| 179 |
return null; |
| 180 |
} |
| 181 |
|
| 182 |
(options.plugins || []).forEach(item => { |
| 183 |
// @ts-expect-error todo(flow->ts): better type annotation for `item.value` |
| 184 |
if (item.value instanceof Plugin) { |
| 185 |
throw new Error( |
| 186 |
"Passing cached plugin instances is not supported in " + |
| 187 |
"babel.loadPartialConfig()", |
| 188 |
); |
| 189 |
} |
| 190 |
}); |
| 191 |
|
| 192 |
return new PartialConfig( |
| 193 |
options, |
| 194 |
babelrc ? babelrc.filepath : undefined, |
| 195 |
ignore ? ignore.filepath : undefined, |
| 196 |
config ? config.filepath : undefined, |
| 197 |
fileHandling, |
| 198 |
files, |
| 199 |
); |
| 200 |
}); |
| 201 |
|
| 202 |
export type { PartialConfig }; |
| 203 |
|
| 204 |
class PartialConfig { |
| 205 |
/** |
| 206 |
* These properties are public, so any changes to them should be considered |
| 207 |
* a breaking change to Babel's API. |
| 208 |
*/ |
| 209 |
options: NormalizedOptions; |
| 210 |
babelrc: string | void; |
| 211 |
babelignore: string | void; |
| 212 |
config: string | void; |
| 213 |
fileHandling: FileHandling; |
| 214 |
files: Set<string>; |
| 215 |
|
| 216 |
constructor( |
| 217 |
options: NormalizedOptions, |
| 218 |
babelrc: string | void, |
| 219 |
ignore: string | void, |
| 220 |
config: string | void, |
| 221 |
fileHandling: FileHandling, |
| 222 |
files: Set<string>, |
| 223 |
) { |
| 224 |
this.options = options; |
| 225 |
this.babelignore = ignore; |
| 226 |
this.babelrc = babelrc; |
| 227 |
this.config = config; |
| 228 |
this.fileHandling = fileHandling; |
| 229 |
this.files = files; |
| 230 |
|
| 231 |
// Freeze since this is a public API and it should be extremely obvious that |
| 232 |
// reassigning properties on here does nothing. |
| 233 |
Object.freeze(this); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Returns true if there is a config file in the filesystem for this config. |
| 238 |
*/ |
| 239 |
hasFilesystemConfig(): boolean { |
| 240 |
return this.babelrc !== undefined || this.config !== undefined; |
| 241 |
} |
| 242 |
} |
| 243 |
Object.freeze(PartialConfig.prototype); |