1
1
import { compile , CompileOptions } from 'https://deno.land/x/[email protected] /tsc/compile.ts'
2
2
import { colors , path , Sha1 , walk } from '../deps.ts'
3
+ import { hashShort } from '../shared/constants.ts'
3
4
import { initWasm , transpileSync } from './mod.ts'
4
5
5
- const hashShort = 9
6
- const reHttp = / ^ h t t p s ? : \/ \/ / i
7
-
8
6
function tsc ( source : string , opts : any ) {
9
7
const compileOptions : CompileOptions = {
10
8
mode : opts . isDev ? 'development' : 'production' ,
11
9
target : 'es2020' ,
12
10
reactRefresh : opts . isDev ,
13
- rewriteImportPath : ( path : string ) => path . replace ( reHttp , '/-/' ) ,
11
+ rewriteImportPath : ( path : string ) => path . replace ( 'https://' , '/-/' ) ,
14
12
signUseDeno : ( id : string ) => {
15
- const sig = 'useDeno.' + ( new Sha1 ( ) ) . update ( id ) . update ( " 0.2.25" ) . update ( Date . now ( ) . toString ( ) ) . hex ( ) . slice ( 0 , hashShort )
13
+ const sig = 'useDeno.' + ( new Sha1 ( ) ) . update ( id ) . update ( ' 0.2.25' ) . update ( Date . now ( ) . toString ( ) ) . hex ( ) . slice ( 0 , hashShort )
16
14
return sig
17
15
}
18
16
}
@@ -43,10 +41,10 @@ async function benchmark(sourceFiles: Array<{ code: string, filename: string }>,
43
41
44
42
for ( const { code, filename } of sourceFiles ) {
45
43
const t = performance . now ( )
46
- for ( let i = 0 ; i < 5 ; i ++ ) {
44
+ for ( let i = 0 ; i < 2 ; i ++ ) {
47
45
tsc ( code , { filename, isDev } )
48
46
}
49
- const d = ( performance . now ( ) - t ) / 5
47
+ const d = ( performance . now ( ) - t ) / 2
50
48
if ( d1 . min === 0 || d < d1 . min ) {
51
49
d1 . min = d
52
50
}
@@ -57,10 +55,14 @@ async function benchmark(sourceFiles: Array<{ code: string, filename: string }>,
57
55
}
58
56
for ( const { code, filename } of sourceFiles ) {
59
57
const t = performance . now ( )
60
- for ( let i = 0 ; i < 5 ; i ++ ) {
61
- transpileSync ( code , { url : filename , swcOptions : { } , isDev } )
58
+ for ( let i = 0 ; i < 2 ; i ++ ) {
59
+ transpileSync ( code , {
60
+ url : filename ,
61
+ swcOptions : { target : 'es2020' } ,
62
+ isDev
63
+ } )
62
64
}
63
- const d = ( performance . now ( ) - t ) / 5
65
+ const d = ( performance . now ( ) - t ) / 2
64
66
if ( d2 . min === 0 || d < d2 . min ) {
65
67
d2 . min = d
66
68
}
@@ -75,23 +77,25 @@ async function benchmark(sourceFiles: Array<{ code: string, filename: string }>,
75
77
console . log ( `swc is ${ coloredDiff ( d1 . d / d2 . d ) } ${ d1 . d > d2 . d ? 'faster' : 'slower' } than tsc` )
76
78
}
77
79
78
- if ( import . meta. main ) {
79
- ( async ( ) => {
80
- const p = Deno . run ( {
81
- cmd : [ 'deno' , 'info' ] ,
82
- stdout : 'piped' ,
83
- stderr : 'null'
84
- } )
85
- await initWasm ( ( new TextDecoder ) . decode ( await p . output ( ) ) . split ( '"' ) [ 1 ] )
86
- p . close ( )
80
+ async function main ( ) {
81
+ const p = Deno . run ( {
82
+ cmd : [ 'deno' , 'info' ] ,
83
+ stdout : 'piped' ,
84
+ stderr : 'null'
85
+ } )
86
+ await initWasm ( ( new TextDecoder ) . decode ( await p . output ( ) ) . split ( '"' ) [ 1 ] )
87
+ p . close ( )
87
88
88
- const sourceFiles : Array < { code : string , filename : string } > = [ ]
89
- const walkOptions = { includeDirs : false , exts : [ '.tsx' ] , skip : [ / [ \. _ ] t e s t \. t s x ? $ / i] }
90
- for await ( const { path : filename } of walk ( path . resolve ( '..' ) , walkOptions ) ) {
91
- sourceFiles . push ( { code : await Deno . readTextFile ( filename ) , filename } )
92
- }
89
+ const sourceFiles : Array < { code : string , filename : string } > = [ ]
90
+ const walkOptions = { includeDirs : false , exts : [ 'ts' , ' .tsx'] , skip : [ / [ \. _ ] ( t e s t | d ) \. t s x ? $ / i, / \/ c o m p i l e r \/ / ] }
91
+ for await ( const { path : filename } of walk ( path . resolve ( '..' ) , walkOptions ) ) {
92
+ sourceFiles . push ( { code : await Deno . readTextFile ( filename ) , filename } )
93
+ }
93
94
94
- await benchmark ( sourceFiles , true )
95
- await benchmark ( sourceFiles , false )
96
- } ) ( )
95
+ await benchmark ( sourceFiles , true )
96
+ await benchmark ( sourceFiles , false )
97
+ }
98
+
99
+ if ( import . meta. main ) {
100
+ main ( )
97
101
}
0 commit comments