6
6
// has been loaded, so we'd prefer to protect this
7
7
// API with the playground compiler manager state
8
8
9
+ [@ bs . val ] [@ bs . scope "performance" ] external now : unit => float = "now" ;
10
+
9
11
module Lang = {
10
12
type t =
11
13
| Reason
@@ -109,6 +111,32 @@ module LocMsg = {
109
111
110
112
{ j | [1;31m[$prefix] Line $row, $column:[0m $shortMsg| j } ;
111
113
};
114
+
115
+ // Creates a somewhat unique id based on the rows / cols of the locMsg
116
+ let makeId = t => {
117
+ Belt . Int . (
118
+ toString(t. row)
119
+ ++ "-"
120
+ ++ toString(t. endRow)
121
+ ++ "-"
122
+ ++ toString(t. column)
123
+ ++ "-"
124
+ ++ toString(t. endColumn)
125
+ );
126
+ };
127
+
128
+ let dedupe = (arr: array (t )) => {
129
+ let result = Js . Dict . empty() ;
130
+
131
+ for (i in 0 to Js . Array . length(arr) - 1 ) {
132
+ let locMsg = Js . Array2 . unsafe_get(arr, i);
133
+ let id = makeId(locMsg);
134
+
135
+ // The last element with the same id wins
136
+ result-> Js . Dict . set(id, locMsg);
137
+ };
138
+ Js . Dict . values(result);
139
+ };
112
140
};
113
141
114
142
module Warning = {
@@ -173,12 +201,14 @@ module CompileSuccess = {
173
201
type t = {
174
202
js_code: string ,
175
203
warnings: array (Warning . t ),
204
+ time: float // total compilation time
176
205
};
177
206
178
- let decode = (json): t => {
207
+ let decode = (~time : float , json): t => {
179
208
Json . Decode . {
180
209
js_code: field("js_code" , string, json),
181
210
warnings: field("warnings" , array(Warning . decode), json),
211
+ time,
182
212
};
183
213
};
184
214
};
@@ -213,7 +243,9 @@ module CompileFail = {
213
243
switch (field("type" , string, json)) {
214
244
| "syntax_error" =>
215
245
let locMsgs = field("errors" , array(LocMsg . decode), json);
216
- SyntaxErr (locMsgs);
246
+ // TODO: There seems to be a bug in the ReScript bundle that reports
247
+ // back multiple LocMsgs of the same value
248
+ locMsgs-> LocMsg . dedupe-> SyntaxErr ;
217
249
| "type_error" =>
218
250
let locMsgs = field("errors" , array(LocMsg . decode), json);
219
251
TypecheckErr (locMsgs);
@@ -241,12 +273,12 @@ module CompilationResult = {
241
273
| Unknown (string , Js . Json . t );
242
274
243
275
// TODO: We might change this specific api completely before launching
244
- let decode = (json: Js . Json . t ): t => {
276
+ let decode = (~time : float , json: Js . Json . t ): t => {
245
277
open ! Json . Decode ;
246
278
247
279
try (
248
280
switch (field("type" , string, json)) {
249
- | "success" => Success (CompileSuccess . decode(json))
281
+ | "success" => Success (CompileSuccess . decode(~time , json))
250
282
| "unexpected_error" => UnexpectedError (field("msg" , string, json))
251
283
| _ => Fail (CompileFail . decode(json))
252
284
}
@@ -288,7 +320,6 @@ module Config = {
288
320
type t = {
289
321
module_system: string ,
290
322
warn_flags: string ,
291
- warn_error_flags: string ,
292
323
};
293
324
};
294
325
@@ -299,8 +330,6 @@ module Compiler = {
299
330
[@ bs . val ] [@ bs . scope "bs_platform" ] external make : unit => t = "make" ;
300
331
301
332
[@ bs . get ] external version : t => string = "version" ;
302
- [@ bs . get ]
303
- external version_git_commit : t => option (string ) = "version_git_commit" ;
304
333
305
334
/*
306
335
Res compiler actions
@@ -311,7 +340,11 @@ module Compiler = {
311
340
external resCompile : (t , string ) => Js . Json . t = "compile" ;
312
341
313
342
let resCompile = (t, code): CompilationResult . t => {
314
- resCompile(t, code)-> CompilationResult . decode;
343
+ let startTime = now() ;
344
+ let json = resCompile(t, code);
345
+ let stopTime = now() ;
346
+
347
+ CompilationResult . decode(~time= stopTime -. startTime, json);
315
348
};
316
349
317
350
[@ bs . send ] [@ bs . scope "napkin" ]
@@ -331,7 +364,11 @@ module Compiler = {
331
364
[@ bs . send ] [@ bs . scope "reason" ]
332
365
external reasonCompile : (t , string ) => Js . Json . t = "compile" ;
333
366
let reasonCompile = (t, code): CompilationResult . t => {
334
- reasonCompile(t, code)-> CompilationResult . decode;
367
+ let startTime = now() ;
368
+ let json = reasonCompile(t, code);
369
+ let stopTime = now() ;
370
+
371
+ CompilationResult . decode(~time= stopTime -. startTime, json);
335
372
};
336
373
337
374
[@ bs . send ] [@ bs . scope "reason" ]
@@ -351,7 +388,11 @@ module Compiler = {
351
388
external ocamlCompile : (t , string ) => Js . Json . t = "compile" ;
352
389
353
390
let ocamlCompile = (t, code): CompilationResult . t => {
354
- ocamlCompile(t, code)-> CompilationResult . decode;
391
+ let startTime = now() ;
392
+ let json = ocamlCompile(t, code);
393
+ let stopTime = now() ;
394
+
395
+ CompilationResult . decode(~time= stopTime -. startTime, json);
355
396
};
356
397
357
398
/*
@@ -367,9 +408,6 @@ module Compiler = {
367
408
368
409
[@ bs . send ] external setWarnFlags : (t , string ) => bool = "setWarnFlags" ;
369
410
370
- [@ bs . send ]
371
- external setWarnErrorFlags : (t , string ) => bool = "setWarnErrorFlags" ;
372
-
373
411
let setConfig = (t: t , config: Config . t ): unit => {
374
412
let moduleSystem =
375
413
switch (config. module_system) {
@@ -383,7 +421,6 @@ module Compiler = {
383
421
);
384
422
385
423
t-> setWarnFlags(config. warn_flags)-> ignore ;
386
- t-> setWarnErrorFlags(config. warn_error_flags)-> ignore ;
387
424
};
388
425
389
426
[@ bs . send ]
0 commit comments