// tslint:disable: object-literal-sort-keys import { TransformOptions } from '@babel/core'; import { merge } from '../lib'; export interface BabelRegisterOptions extends TransformOptions { extensions?: string[]; cache?: boolean; } const defaultExtensions = ['.es6', '.es', '.jsx', '.js', '.mjs', '.ts', '.tsx']; const defaultRegisterOptions: BabelRegisterOptions = { presets: [ [ '@babel/preset-env', { targets: { node: true, }, }, ], '@babel/typescript', ], plugins: [ '@babel/plugin-proposal-class-properties', '@babel/plugin-proposal-object-rest-spread', '@babel/plugin-transform-object-assign', ], extensions: defaultExtensions, }; export const initBabel = (rootPathPaths?: string | string[], options?: BabelRegisterOptions, mergeDefault = false) => { let registerOptions: BabelRegisterOptions = options || defaultRegisterOptions; if (options && mergeDefault) { registerOptions = merge([defaultRegisterOptions, options], { concatArrays: true }); } // confine to specified root paths if passed if (rootPathPaths) { rootPathPaths = Array.isArray(rootPathPaths) ? rootPathPaths : [rootPathPaths]; registerOptions.only = rootPathPaths; registerOptions.babelrcRoots = rootPathPaths; } require('@babel/register')(registerOptions); };