import compileToString from './compile-string' ; import { getConfig } from './config' ; import EtaErr from './err' ; import type { EtaConfig , PartialConfig } from './config' ; import type { CallbackFn } from './file-handlers' ; import { getAsyncFunctionConstructor } from './polyfills' ; export type TemplateFunction = ( data : object , config : EtaConfig , cb ? : CallbackFn ) => string ; param str param config export default function compile ( str , config ? ) { const options = getConfig ( config || { } ) ; const ctor = options . async ? ( getAsyncFunctionConstructor ( ) as FunctionConstructor ) : Function ; try { return new ctor ( options . varName , 'E' , 'cb' , compileToString ( str , options ) ) as TemplateFunction ; } catch ( e ) { if ( e instanceof SyntaxError ) { throw EtaErr ( 'Bad template syntax\n\n' + e . message + '\n' + Array ( e . message . length + 1 ) . join ( '=' ) + '\n' + compileToString ( str , options ) + '\n' ) ; } else { throw e ; } } }