import { resolve } from "path" ; import { watch as rollupWatch , rollup } from "rollup" ; import { RollupWatcher , RollupWatchOptions , OutputOptions , RollupBuild } from "rollup" ; import createRollupConfig from "./rollup-config" ; import { getPackageMeta } from "./utils" ; import config from "./config" ; import { BuncheeRollupConfig , CliArgs } from "./types" ; function assignDefault ( options , name , defaultValue ) { if ( ! ( name in options ) || options [ name ] == null ) { options [ name ] = defaultValue ; } } function bundle ( entry , { watch , cwd , ... options } = { } ) { config . rootDir = resolve ( process . cwd ( ) , cwd || "" ) ; assignDefault ( options , "format" , "es" ) ; assignDefault ( options , "sourcemap" , true ) ; if ( options . format === "esm" ) { options . format = "es" ; } const npmPackage = getPackageMeta ( ) ; const rollupConfig = createRollupConfig ( entry , npmPackage , options ) ; if ( watch ) { return Promise . resolve ( runWatch ( rollupConfig ) ) ; } return runBundle ( rollupConfig ) ; } function runWatch ( { input , output } ) { const watchOptions = [ { ... input , output : output , watch : { exclude : [ "node_modules/**" ] , } , } ] ; return rollupWatch ( watchOptions ) ; } function runBundle ( { input , output } ) { return rollup ( input ) . then ( ( bundle ) => { const writeJobs = output . map ( ( options ) => bundle . write ( options ) ) ; return Promise . all ( writeJobs ) ; } , ( error = { } ) => { if ( error . frame ) { process . stdout . write ( error . frame + "\n" ) ; } if ( error . stack ) { process . stdout . write ( error . stack + "\n" ) ; } throw error ; } ) ; } export default bundle ;