import ts from "typescript" ; import fs from "fs" ; import path from "path" ; import esbuild_ from "esbuild" ; import { transform as sucraseTransform } from "sucrase" ; import { transformSync } from "../dist/index.cjs" ; import { createTransformer } from "../dist/node_main.cjs" ; const N = 1 ; const code_scratch = fs . readFileSync ( path . join ( __dirname , "cases/_scratch.ts" ) , "utf-8" ) ; const code0 = fs . readFileSync ( path . join ( __dirname , "cases/example0.ts" ) , "utf-8" ) ; const code1 = fs . readFileSync ( path . join ( __dirname , "cases/example1.ts" ) , "utf-8" ) ; const code2 = fs . readFileSync ( path . join ( __dirname , "cases/example2.ts" ) , "utf-8" ) ; const code3 = fs . readFileSync ( path . join ( __dirname , "cases/example3.ts" ) , "utf-8" ) ; const code4 = fs . readFileSync ( path . join ( __dirname , "cases/example4.ts" ) , "utf-8" ) ; const code5 = fs . readFileSync ( path . join ( __dirname , "cases/example5.tsx" ) , "utf-8" ) ; const code6 = fs . readFileSync ( path . join ( __dirname , "cases/example6.tsx" ) , "utf-8" ) ; const code7 = fs . readFileSync ( path . join ( __dirname , "cases/example7.ts" ) , "utf-8" ) ; function tsc ( input ) { return ts . transpileModule ( input , { fileName : "$.tsx" , compilerOptions : { module : ts . ModuleKind . ESNext , target : ts . ScriptTarget . Latest , jsx : ts . JsxEmit . React , } , } ) . outputText ; } function sucrase ( input ) { return sucraseTransform ( input , { transforms : [ "jsx" , "typescript" ] } ) . code ; } async function esbuild ( input ) { const x = await esbuild_ . transform ( input , { loader : "tsx" , } ) ; return x . code ; } function mints ( input ) { const out = transformSync ( input ) ; if ( out . error ) { const errorText = ( out . tokens as string [ ] ) . map ( ( t , i ) => { if ( i === out . pos ) return "<[ " + t + " ]>" ; return t ; } ) . join ( " " ) ; console . log ( "---\n" , out , "\n" ) ; throw new Error ( `[Mints:ParseError]\n${ errorText }` ) ; } return out . code as string ; } const transformer = createTransformer ( path . join ( __dirname , "../dist/node_worker.js" ) , 14 ) ; process . on ( "exit" , ( ) => transformer . terminate ( ) ) ; async function mints_para ( input ) { const out = await transformer . transform ( input ) ; if ( typeof out === "object" ) { throw out ; } return out as string ; } export async function main ( ) { const compilers = [ tsc , esbuild , mints , mints_para , ] ; const targets = [ code0 , code1 , code2 , code3 , code4 , code5 , code6 , code7 ] ; console . log ( "=== mints-check" ) ; for ( const target of targets ) { const _out = mints ( target ) ; console . log ( "pass" , JSON . stringify ( target . slice ( 0 , 10 ) ) + "..." ) ; } console . log ( "=== perf start" ) ; for ( const code of targets ) { const caseName = "example:" + targets . indexOf ( code ) + ":" + code . length + "chars" ; console . log ( "---------" , caseName ) ; for ( const compiler of compilers ) { const results = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { const now = Date . now ( ) ; const out = await compiler ( code ) ; results . push ( Date . now ( ) - now ) ; } console . log ( `[${ compiler . name }]` , Math . floor ( results . reduce ( ( s , n ) => s + n , 0 ) / results . length ) + "ms" ) ; } } process . exit ( 0 ) ; } main ( ) . catch ( ( e ) => { console . error ( e ) ; process . exit ( 1 ) ; } ) ;