namespace ts { export interface TranspileOptions { compilerOptions ? : CompilerOptions ; fileName ? : string ; reportDiagnostics ? : boolean ; moduleName ? : string ; renamedDependencies ? : MapLike < string > ; transformers ? : CustomTransformers ; } export interface TranspileOutput { outputText : string ; diagnostics ? : Diagnostic [ ] ; sourceMapText ? : string ; } export function transpileModule ( input , transpileOptions ) { const diagnostics = [ ] ; const options = transpileOptions . compilerOptions ? fixupCompilerOptions ( transpileOptions . compilerOptions , diagnostics ) : { } ; const defaultOptions = getDefaultCompilerOptions ( ) ; for ( const key in defaultOptions ) { if ( hasProperty ( defaultOptions , key ) && options [ key ] === undefined ) { options [ key ] = defaultOptions [ key ] ; } } for ( const option of transpileOptionValueCompilerOptions ) { options [ option . name ] = option . transpileOptionValue ; } options . suppressOutputPathCheck = true ; options . allowNonTsExtensions = true ; const inputFileName = transpileOptions . fileName || ( transpileOptions . compilerOptions && transpileOptions . compilerOptions . jsx ? "module.tsx" : "module.ts" ) ; const sourceFile = createSourceFile ( inputFileName , input , getEmitScriptTarget ( options ) ) ; if ( transpileOptions . moduleName ) { sourceFile . moduleName = transpileOptions . moduleName ; } if ( transpileOptions . renamedDependencies ) { sourceFile . renamedDependencies = new Map ( getEntries ( transpileOptions . renamedDependencies ) ) ; } const newLine = getNewLineCharacter ( options ) ; let outputText ; let sourceMapText ; const compilerHost = { getSourceFile : ( fileName ) => fileName === normalizePath ( inputFileName ) ? sourceFile : undefined , writeFile : ( name , text ) => { if ( fileExtensionIs ( name , ".map" ) ) { Debug . assertEqual ( sourceMapText , undefined , "Unexpected multiple source map outputs, file:" , name ) ; sourceMapText = text ; } else { Debug . assertEqual ( outputText , undefined , "Unexpected multiple outputs, file:" , name ) ; outputText = text ; } } , getDefaultLibFileName : ( ) => "lib.d.ts" , useCaseSensitiveFileNames : ( ) => false , getCanonicalFileName : fileName => fileName , getCurrentDirectory : ( ) => "" , getNewLine : ( ) => newLine , fileExists : ( fileName ) : boolean => fileName === inputFileName , readFile : ( ) => "" , directoryExists : ( ) => true , getDirectories : ( ) => [ ] } ; const program = createProgram ( [ inputFileName ] , options , compilerHost ) ; if ( transpileOptions . reportDiagnostics ) { addRange ( diagnostics , program . getSyntacticDiagnostics ( sourceFile ) ) ; addRange ( diagnostics , program . getOptionsDiagnostics ( ) ) ; } program . emit ( undefined , undefined , undefined , undefined , transpileOptions . transformers ) ; if ( outputText === undefined ) return Debug . fail ( "Output generation failed" ) ; return { outputText , diagnostics , sourceMapText } ; } export function transpile ( input , compilerOptions ? , fileName ? , diagnostics ? , moduleName ? ) { const output = transpileModule ( input , { compilerOptions , fileName , reportDiagnostics : ! ! diagnostics , moduleName } ) ; addRange ( diagnostics , output . diagnostics ) ; return output . outputText ; } let commandLineOptionsStringToEnum ; /** JS users may pass in string values for enum compiler options (such as ModuleKind), so convert. */ export function fixupCompilerOptions ( options , diagnostics ) { commandLineOptionsStringToEnum = commandLineOptionsStringToEnum || filter ( optionDeclarations , o => typeof o . type === "object" && ! forEachEntry ( o . type , v => typeof v !== "number" ) ) as CommandLineOptionOfCustomType [ ] ; options = cloneCompilerOptions ( options ) ; for ( const opt of commandLineOptionsStringToEnum ) { if ( ! hasProperty ( options , opt . name ) ) { continue ; } const value = options [ opt . name ] ; if ( isString ( value ) ) { options [ opt . name ] = parseCustomTypeOption ( opt , value , diagnostics ) ; } else { if ( ! forEachEntry ( opt . type , v => v === value ) ) { diagnostics . push ( createCompilerDiagnosticForInvalidCustomType ( opt ) ) ; } } } return options ; } }