import { getOptions } from 'loader-utils' ; import { generateClient , GenClientOptions } from '@modern-js/bff-utils' ; import { LoaderContext } from 'webpack' ; export type APILoaderOptions = { prefix : string ; apiDir : string ; port : number ; fetcher ? : string ; requestCreator ? : string ; target : string ; } ; async function loader ( this , source ) { this . cacheable ( ) ; const callback = this . async ( ) ; const draftOptions = getOptions ( this as any ) ; const options = { prefix : ( Array . isArray ( draftOptions . prefix ) ? draftOptions . prefix [ 0 ] : draftOptions . prefix ) as string , apiDir : draftOptions . apiDir as string , target : draftOptions . target as string , port : Number ( draftOptions . port ) , source , resourcePath : this . resourcePath , } ; if ( draftOptions . fetcher ) { options . fetcher = draftOptions . fetcher as string ; } if ( draftOptions . requestCreator ) { options . requestCreator = draftOptions . requestCreator as string ; } options . requireResolve = require . resolve ; const result = await generateClient ( options ) ; if ( result . isOk ) { callback ( undefined , result . value ) ; } else { callback ( undefined , `throw new Error('${ result . value }')` ) ; } } export default loader ;