@@ -13,48 +13,13 @@ mod ts {
13
13
use std:: path:: PathBuf ;
14
14
15
15
use deno_core:: op2;
16
+ use deno_core:: v8;
16
17
use deno_core:: OpState ;
17
18
use deno_error:: JsErrorBox ;
18
19
use serde:: Serialize ;
19
20
20
21
use super :: * ;
21
22
22
- #[ derive( Debug , Serialize ) ]
23
- #[ serde( rename_all = "camelCase" ) ]
24
- struct BuildInfoResponse {
25
- build_specifier : String ,
26
- libs : Vec < String > ,
27
- }
28
-
29
- #[ op2]
30
- #[ serde]
31
- fn op_build_info ( state : & mut OpState ) -> BuildInfoResponse {
32
- let build_specifier = "asset:///bootstrap.ts" . to_string ( ) ;
33
- let build_libs = state
34
- . borrow :: < Vec < & str > > ( )
35
- . iter ( )
36
- . map ( |s| s. to_string ( ) )
37
- . collect ( ) ;
38
- BuildInfoResponse {
39
- build_specifier,
40
- libs : build_libs,
41
- }
42
- }
43
-
44
- #[ op2( fast) ]
45
- fn op_is_node_file ( ) -> bool {
46
- false
47
- }
48
-
49
- #[ op2]
50
- #[ string]
51
- fn op_script_version (
52
- _state : & mut OpState ,
53
- #[ string] _arg : & str ,
54
- ) -> Result < Option < String > , JsErrorBox > {
55
- Ok ( Some ( "1" . to_string ( ) ) )
56
- }
57
-
58
23
#[ derive( Debug , Serialize ) ]
59
24
#[ serde( rename_all = "camelCase" ) ]
60
25
struct LoadResponse {
@@ -74,19 +39,10 @@ mod ts {
74
39
let op_crate_libs = state. borrow :: < HashMap < & str , PathBuf > > ( ) ;
75
40
let path_dts = state. borrow :: < PathBuf > ( ) ;
76
41
let re_asset = lazy_regex:: regex!( r"asset:/{3}lib\.(\S+)\.d\.ts" ) ;
77
- let build_specifier = "asset:///bootstrap.ts" ;
78
-
79
- // we need a basic file to send to tsc to warm it up.
80
- if load_specifier == build_specifier {
81
- Ok ( LoadResponse {
82
- data : r#"Deno.writeTextFile("hello.txt", "hello deno!");"# . to_string ( ) ,
83
- version : "1" . to_string ( ) ,
84
- // this corresponds to `ts.ScriptKind.TypeScript`
85
- script_kind : 3 ,
86
- } )
87
- // specifiers come across as `asset:///lib.{lib_name}.d.ts` and we need to
88
- // parse out just the name so we can lookup the asset.
89
- } else if let Some ( caps) = re_asset. captures ( load_specifier) {
42
+
43
+ // specifiers come across as `asset:///lib.{lib_name}.d.ts` and we need to
44
+ // parse out just the name so we can lookup the asset.
45
+ if let Some ( caps) = re_asset. captures ( load_specifier) {
90
46
if let Some ( lib) = caps. get ( 1 ) . map ( |m| m. as_str ( ) ) {
91
47
// if it comes from an op crate, we were supplied with the path to the
92
48
// file.
@@ -100,28 +56,25 @@ mod ts {
100
56
} ;
101
57
let data =
102
58
std:: fs:: read_to_string ( path) . map_err ( JsErrorBox :: from_err) ?;
103
- Ok ( LoadResponse {
59
+ return Ok ( LoadResponse {
104
60
data,
105
61
version : "1" . to_string ( ) ,
106
62
// this corresponds to `ts.ScriptKind.TypeScript`
107
63
script_kind : 3 ,
108
- } )
109
- } else {
110
- Err ( JsErrorBox :: new (
111
- "InvalidSpecifier" ,
112
- format ! ( "An invalid specifier was requested: {}" , load_specifier) ,
113
- ) )
64
+ } ) ;
114
65
}
115
- } else {
116
- Err ( JsErrorBox :: new (
117
- "InvalidSpecifier" ,
118
- format ! ( "An invalid specifier was requested: {}" , load_specifier) ,
119
- ) )
120
66
}
67
+
68
+ Err ( JsErrorBox :: new (
69
+ "InvalidSpecifier" ,
70
+ format ! ( "An invalid specifier was requested: {}" , load_specifier) ,
71
+ ) )
121
72
}
122
73
123
74
deno_core:: extension!( deno_tsc,
124
- ops = [ op_build_info, op_is_node_file, op_load, op_script_version] ,
75
+ ops = [
76
+ op_load,
77
+ ] ,
125
78
esm_entry_point = "ext:deno_tsc/99_main_compiler.js" ,
126
79
esm = [
127
80
dir "tsc" ,
@@ -277,6 +230,28 @@ mod ts {
277
230
)
278
231
. unwrap ( ) ;
279
232
233
+ // Leak to satisfy type-checker. It's okay since it's only run once for a build script.
234
+ let build_libs_ = Box :: leak ( Box :: new ( build_libs. clone ( ) ) ) ;
235
+ let runtime_cb = Box :: new ( |rt : & mut deno_core:: JsRuntimeForSnapshot | {
236
+ let scope = & mut rt. handle_scope ( ) ;
237
+
238
+ let context = scope. get_current_context ( ) ;
239
+ let global = context. global ( scope) ;
240
+
241
+ let name = v8:: String :: new ( scope, "snapshot" ) . unwrap ( ) ;
242
+ let snapshot_fn_val = global. get ( scope, name. into ( ) ) . unwrap ( ) ;
243
+ let snapshot_fn: v8:: Local < v8:: Function > =
244
+ snapshot_fn_val. try_into ( ) . unwrap ( ) ;
245
+ let undefined = v8:: undefined ( scope) ;
246
+ let build_libs = build_libs_. clone ( ) ;
247
+ let build_libs_v8 =
248
+ deno_core:: serde_v8:: to_v8 ( scope, build_libs) . unwrap ( ) ;
249
+
250
+ snapshot_fn
251
+ . call ( scope, undefined. into ( ) , & [ build_libs_v8] )
252
+ . unwrap ( ) ;
253
+ } ) ;
254
+
280
255
let output = create_snapshot (
281
256
CreateSnapshotOptions {
282
257
cargo_manifest_dir : env ! ( "CARGO_MANIFEST_DIR" ) ,
@@ -287,7 +262,7 @@ mod ts {
287
262
path_dts,
288
263
) ] ,
289
264
extension_transpiler : None ,
290
- with_runtime_cb : None ,
265
+ with_runtime_cb : Some ( runtime_cb ) ,
291
266
skip_op_registration : false ,
292
267
} ,
293
268
None ,
0 commit comments