1
1
import { $ } from "../../runtime/mod.ts" ;
2
2
3
+ const startTime = Date . now ( ) ;
4
+
3
5
export interface BootstrapOptions {
4
6
code ?: string ;
5
- startTime ?: number ;
6
7
mainModule ?: string ;
7
8
args ?: Array < string > | string ;
8
9
base64 ?: boolean ;
@@ -14,19 +15,32 @@ export function base64Module(code: string) {
14
15
}
15
16
16
17
export function stringifyArgs ( args : Array < string > ) {
17
- return args ?. length
18
- ? `$.args = JSON.parse(decodeURIComponent("${
18
+ if ( ! args ?. length ) {
19
+ return "" ;
20
+ }
21
+ let code = args ?. length
22
+ ? `const args = JSON.parse(decodeURIComponent("${
19
23
encodeURIComponent ( JSON . stringify ( args ) )
20
- } "));`
21
- : "" ;
24
+ } "));\n`
25
+ : "const args = [];\n" ;
26
+ code += `Object.defineProperty($, "args", { get: () => args });` ;
27
+ return code ;
28
+ }
29
+
30
+ export function stringifyMainModule ( mainModule : string ) {
31
+ return `Object.defineProperty($, "mainModule", { get: () => "${ mainModule } " });` ;
32
+ }
33
+
34
+ export function stringifyStartTime ( startTime : number ) {
35
+ return `Object.defineProperty($, "startTime", { get: () => ${ startTime } });` ;
22
36
}
23
37
24
38
export function bootstrap ( options : BootstrapOptions ) : string {
25
39
const code = [
26
40
`import "${ new URL ( "../../../mod.ts" , import . meta. url ) } ";` ,
27
41
"{" ,
28
- options . startTime ? `$.startTime = ${ options . startTime } ;` : "" ,
29
- options . mainModule ? `$.mainModule = " ${ options . mainModule } ";` : "" ,
42
+ stringifyStartTime ( startTime ) ,
43
+ options . mainModule ? stringifyMainModule ( options . mainModule ) : "" ,
30
44
options . verbose !== undefined ? `$.verbose = ${ options . verbose } ;` : "" ,
31
45
typeof options . args === "string"
32
46
? options . args
@@ -78,14 +92,26 @@ export interface ImportModuleOptions {
78
92
}
79
93
80
94
export async function importModule ( options : ImportModuleOptions ) {
81
- $ . mainModule = options . mainModule ;
82
- if ( options . args ) {
83
- $ . args = options . args ;
84
- }
95
+ const mainModule = options . mainModule ;
96
+ Object . defineProperty ( $ , "mainModule" , {
97
+ get : ( ) => mainModule ,
98
+ } ) ;
99
+
100
+ const args = options . args ? [ ...options . args ] : [ ] ;
101
+ Object . defineProperty ( $ , "args" , {
102
+ get : ( ) => args ,
103
+ } ) ;
104
+
85
105
if ( typeof options . verbose !== "undefined" ) {
86
106
$ . verbose = options . verbose ;
87
107
}
108
+
109
+ Object . defineProperty ( $ , "startTime" , {
110
+ get : ( ) : number => startTime ,
111
+ } ) ;
112
+
88
113
await import ( $ . mainModule ) ;
114
+
89
115
if ( $ . verbose ) {
90
116
console . log ( $ . bold ( "time: %ss" ) , Math . round ( $ . time ) / 1000 ) ;
91
117
}
0 commit comments