forked from babel/babel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restrict .babelrc resolution to within a given package.
- Loading branch information
1 parent
e45b58d
commit f013dab
Showing
7 changed files
with
226 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// @flow | ||
|
||
import path from "path"; | ||
import { makeStaticFileCache } from "./utils"; | ||
|
||
import type { ConfigFile, FilePackageData } from "./types"; | ||
|
||
const PACKAGE_FILENAME = "package.json"; | ||
|
||
/** | ||
* Find metadata about the package that this file is inside of. Resolution | ||
* of Babel's config requires general package information to decide when to | ||
* search for .babelrc files | ||
*/ | ||
export function findPackageData(filepath: string): FilePackageData { | ||
let pkg = null; | ||
const directories = []; | ||
let isPackage = true; | ||
|
||
let dirname = path.dirname(filepath); | ||
while (!pkg && path.basename(dirname) !== "node_modules") { | ||
directories.push(dirname); | ||
|
||
pkg = readConfigPackage(path.join(dirname, PACKAGE_FILENAME)); | ||
|
||
const nextLoc = path.dirname(dirname); | ||
if (dirname === nextLoc) { | ||
isPackage = false; | ||
break; | ||
} | ||
dirname = nextLoc; | ||
} | ||
|
||
return { filepath, directories, pkg, isPackage }; | ||
} | ||
|
||
const readConfigPackage = makeStaticFileCache( | ||
(filepath, content): ConfigFile => { | ||
let options; | ||
try { | ||
options = JSON.parse(content); | ||
} catch (err) { | ||
err.message = `${filepath}: Error while parsing JSON - ${err.message}`; | ||
throw err; | ||
} | ||
|
||
if (typeof options !== "object") { | ||
throw new Error(`${filepath}: Config returned typeof ${typeof options}`); | ||
} | ||
if (Array.isArray(options)) { | ||
throw new Error(`${filepath}: Expected config object but found array`); | ||
} | ||
|
||
return { | ||
filepath, | ||
dirname: path.dirname(filepath), | ||
options, | ||
}; | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// @flow | ||
|
||
export type ConfigFile = { | ||
filepath: string, | ||
dirname: string, | ||
options: {}, | ||
}; | ||
|
||
export type IgnoreFile = { | ||
filepath: string, | ||
dirname: string, | ||
ignore: Array<string>, | ||
}; | ||
|
||
export type RelativeConfig = { | ||
// The actual config, either from package.json#babel, .babelrc, or | ||
// .babelrc.js, if there was one. | ||
config: ConfigFile | null, | ||
|
||
// The .babelignore, if there was one. | ||
ignore: IgnoreFile | null, | ||
}; | ||
|
||
export type FilePackageData = { | ||
// The file in the package. | ||
filepath: string, | ||
|
||
// Any ancestor directories of the file that are within the package. | ||
directories: Array<string>, | ||
|
||
// The contents of the package.json. May not be found if the package just | ||
// terminated at a node_modules folder without finding one. | ||
pkg: ConfigFile | null, | ||
|
||
// True if a package.json or node_modules folder was found while traversing | ||
// the directory structure. | ||
isPackage: boolean, | ||
}; |
Oops, something went wrong.