import { getBabelChain , ILibPresetOption , ISyntaxOption , } from '@modern-js/babel-preset-lib' ; import { TransformOptions } from '@babel/core' ; import { applyOptionsChain , fs , getAlias } from '@modern-js/utils' ; import type { NormalizedConfig } from '@modern-js/core' ; import json5 from 'json5' ; export * from '@babel/core' ; export interface ITsconfig { compilerOptions ? : { rootDir ? : string ; baseUrl ? : string ; declaration ? : boolean ; emitDeclarationOnly ? : boolean ; isolatedModules ? : boolean ; allowJs ? : boolean ; outDir ? : string ; paths ? : Record < string , string [ ] > ; } | undefined ; include ? : string [ ] ; exclude ? : string [ ] ; } export const readTsConfig = < T extends null | ITsconfig > ( tsconfigPath , noExistReturn = null as T ) : ITsconfig | T => { if ( ! fs . existsSync ( tsconfigPath ) ) { return noExistReturn ; } const content = fs . readFileSync ( tsconfigPath , 'utf-8' ) ; return json5 . parse ( content ) ; } ; export const existTsConfigFile = ( tsconfigAbsolutePath ) => { const tsconfig = readTsConfig ( tsconfigAbsolutePath ) ; return Boolean ( tsconfig ) ; } ; export const getBabelConfig = ( libPresetOption , syntaxOption ) : TransformOptions => { const chain = getBabelChain ( libPresetOption , syntaxOption ) ; return { sourceType : 'unambiguous' , ... chain . toJSON ( ) , } ; } ; export interface IPackageModeValue { type : 'module' | 'commonjs' ; syntax : 'es5' | 'es6+' ; tsconfigPath : string ; } export const resolveBabelConfig = ( appDirectory , modernConfig , option ) : any => { const { source : { envVars , globalVars , jsxTransformRuntime = 'automatic' , } , tools : { lodash : userLodashOption } , } = modernConfig ; const aliasConfig = getAlias ( modernConfig . source . alias , { appDirectory , ... option , } ) ; const lodashOptions = applyOptionsChain ( { id : [ 'lodash' , 'ramda' ] } , userLodashOption as any ) ; const babelChain = getBabelChain ( { appDirectory , enableReactPreset : true , enableTypescriptPreset : true , alias : aliasConfig , envVars , globalVars , lodashOptions , jsxTransformRuntime , } , { type : option . type , syntax : option . syntax , } ) ; const envOptions = babelChain . preset ( '@babel/preset-env' ) . options ( ) ; babelChain . preset ( '@babel/preset-env' ) . use ( require . resolve ( '@babel/preset-env' ) , [ { ... envOptions [ 0 ] , loose : true , } , ] ) ; babelChain . plugin ( 'babel-plugin-transform-typescript-metadata' ) . use ( require . resolve ( 'babel-plugin-transform-typescript-metadata' ) , [ ] ) ; babelChain . plugin ( '@babel/plugin-proposal-decorators' ) . use ( require . resolve ( '@babel/plugin-proposal-decorators' ) , [ { legacy : true } , ] ) ; babelChain . plugin ( '@babel/plugin-proposal-class-properties' ) . use ( require . resolve ( '@babel/plugin-proposal-class-properties' ) , [ { loose : true } ] ) ; const internalBabelConfig = { ... babelChain . toJSON ( ) } ; const userBabelConfig = modernConfig . tools . babel ; applyOptionsChain ( internalBabelConfig , userBabelConfig as any , { chain : babelChain } ) ; return internalBabelConfig ; } ;