import fs from 'fs' ; import { minifyOptions } from './options' ; import { TransformOptions } from '@balm-core/index' ; const esTransform = ( entryPoints , output , customTransformOptions , callback ) : void => { const build = { output : BalmJS . file . absPath ( output ) } ; const transformOptions = BalmJS . config . env . isProd ? Object . assign ( { } , minifyOptions ) : { } ; const options = Object . assign ( transformOptions , BalmJS . config . scripts . transformOptions , customTransformOptions ) ; ( async ( ) => { try { for await ( const entryPoint of entryPoints ) { build . input = BalmJS . file . absPath ( entryPoint ) ; BalmJS . logger . debug ( 'esbuild bundler - transform' , { input : build . input , output : build . output } , { pre : true } ) ; const filename = entryPoint . split ( '/' ) . pop ( ) ; if ( filename && fs . existsSync ( build . input ) ) { const inputCode = fs . readFileSync ( build . input , 'utf8' ) ; const result = await require ( 'esbuild' ) . transform ( inputCode , options ) ; fs . mkdirSync ( build . output , { recursive : true } ) ; fs . writeFileSync ( path . join ( build . output , filename ) , result . code ) ; } else { BalmJS . logger . warn ( 'esbuild' , `Invalid entry point: ${ entryPoint }` ) ; } } } finally { callback ( ) ; } } ) ( ) ; } ; export default esTransform ;