-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: gracefully load @babel/core or babel-core
- Loading branch information
Showing
5 changed files
with
114 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { | ||
TBabel, | ||
TBabelPresetEnv, | ||
TBabelPluginIstanbul, | ||
TBabelPresetJest, | ||
} from '../types'; | ||
|
||
export function importBabelCore(): TBabel { | ||
const mod = tryRequire('@babel/core') || tryRequire('babel-core'); | ||
if (!mod) { | ||
throw new Error( | ||
`[ts-jest] You must install the '@babel/core' or 'babel-core'` + | ||
` package (depending on the version you want to use).`, | ||
); | ||
} | ||
return mod; | ||
} | ||
|
||
export function importBabelPresetEnv(): TBabelPresetEnv { | ||
const mod = tryRequire('@babel/preset-env') || tryRequire('babel-preset-env'); | ||
if (!mod) { | ||
throw new Error( // babel-jest has the env preset as a dep | ||
`[ts-jest] You must install the 'babel-jest' package if you're using babel.`, | ||
); | ||
} | ||
return mod; | ||
} | ||
|
||
export function importBabelPresetJest(): TBabelPresetJest { | ||
const mod = tryRequire('babel-preset-jest'); | ||
if (!mod) { | ||
throw new Error( // babel-jest has the jest preset as a dep | ||
`[ts-jest] You must install the 'babel-jest' package if you're using babel.`, | ||
); | ||
} | ||
return mod; | ||
} | ||
|
||
export function importBabelPluginIstanbul(): TBabelPluginIstanbul { | ||
const mod = tryRequire('babel-plugin-istanbul'); | ||
if (!mod) { | ||
throw new Error( // babel-jest has the istanbul plugin as a dep | ||
`[ts-jest] You must install the 'babel-jest' package if you're using babel.`, | ||
); | ||
} | ||
return mod.default; | ||
} | ||
|
||
function tryRequire<T = any>(packageName: string): T | void { | ||
let mod: T; | ||
try { | ||
mod = require(packageName); | ||
} catch (err) { | ||
if (err.code !== 'ENOENT') throw err; // tslint:disable-line | ||
} | ||
return mod; | ||
} |
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