Skip to content

Commit 6b397ef

Browse files
authored
Introduce Sourcemap (#8393)
* introduce linked external source map * Remove marker entries after lookup * Preserve relative source paths in maps * Change sourceMap field schema to false or object * sourcemap schema in rescript.json * Add source map enabled policy * Preserve source map markers across package targets * Add comments * Add mapping %debugger statement * Refactor source maps to use JS IR source locations * Add inline and hidden source map modes * Reject sourceMap true during config parsing * Expand source map coverage across modes * Expand source map test coverage * format * Invalidate dependency builds on sourcemap changes * fix test in windows * Avoid source map work when disabled * Remove direct internal source map flag test * changelog
1 parent ff367da commit 6b397ef

50 files changed

Lines changed: 2260 additions & 279 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#### :rocket: New Feature
2020

21+
- Add source map support with linked, inline, and hidden modes. https://github.com/rescript-lang/rescript/pull/8393
22+
2123
#### :bug: Bug fix
2224

2325
#### :memo: Documentation

compiler/bsc/rescript_compiler_main.ml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,25 @@ let[@inline] string_optional_set s : Bsc_args.spec =
209209
let[@inline] unit_call s : Bsc_args.spec = Unit (Unit_call s)
210210
let[@inline] string_list_add s : Bsc_args.spec = String (String_list_add s)
211211

212+
let parse_source_map value =
213+
Js_config.source_map :=
214+
match String.lowercase_ascii value with
215+
| "linked" -> Linked
216+
| "inline" -> Inline
217+
| "hidden" -> Hidden
218+
| "false" | "none" -> No_source_map
219+
| value ->
220+
Bsc_args.bad_arg
221+
("Unsupported sourceMap value: " ^ value
222+
^ ". Expected linked, inline, hidden, false, or none")
223+
224+
let parse_bool_ref target value =
225+
target :=
226+
match String.lowercase_ascii value with
227+
| "true" -> true
228+
| "false" -> false
229+
| value -> Bsc_args.bad_arg ("Expected true or false, got: " ^ value)
230+
212231
(* mostly common used to list in the beginning to make search fast
213232
*)
214233
let command_line_flags : (string * Bsc_args.spec * string) array =
@@ -259,6 +278,15 @@ let command_line_flags : (string * Bsc_args.spec * string) array =
259278
string_call ignore,
260279
"*internal* Set jsx mode, this is no longer used and is a no-op." );
261280
("-bs-jsx-preserve", set Js_config.jsx_preserve, "*internal* Preserve jsx");
281+
( "-bs-source-map",
282+
string_call parse_source_map,
283+
"*internal* Configure source map output" );
284+
( "-bs-source-map-sources-content",
285+
string_call (parse_bool_ref Js_config.source_map_sources_content),
286+
"*internal* Include original source text in source maps" );
287+
( "-bs-source-map-root",
288+
string_call (fun value -> Js_config.source_map_root := value),
289+
"*internal* Set sourceRoot in source maps" );
262290
( "-bs-package-output",
263291
string_call Js_packages_state.update_npm_package_path,
264292
"*internal* Set npm-output-path: [opt_module]:path, for example: \

compiler/common/js_config.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
type jsx_version = Jsx_v4
2828
type jsx_module = React | Generic of {module_name: string}
29+
type source_map = No_source_map | Linked | Inline | Hidden
2930

3031
let no_version_header = ref false
3132

@@ -53,6 +54,9 @@ let jsx_version = ref None
5354
let jsx_module = ref React
5455
let jsx_preserve = ref false
5556
let js_stdout = ref true
57+
let source_map = ref No_source_map
58+
let source_map_sources_content = ref false
59+
let source_map_root = ref ""
5660
let all_module_aliases = ref false
5761
let no_stdlib = ref false
5862
let no_export = ref false

compiler/common/js_config.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
type jsx_version = Jsx_v4
2626
type jsx_module = React | Generic of {module_name: string}
27+
type source_map = No_source_map | Linked | Inline | Hidden
2728

2829
(* val get_packages_info :
2930
unit -> Js_packages_info.t *)
@@ -86,6 +87,12 @@ val jsx_preserve : bool ref
8687

8788
val js_stdout : bool ref
8889

90+
val source_map : source_map ref
91+
92+
val source_map_sources_content : bool ref
93+
94+
val source_map_root : string ref
95+
8996
val all_module_aliases : bool ref
9097

9198
val no_stdlib : bool ref

compiler/core/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
(backend bisect_ppx))
66
(flags
77
(:standard -w +a-4-9-27-30-40-41-42-48-70))
8-
(libraries depends ext flow_parser frontend gentype))
8+
(libraries depends ext flow_parser frontend gentype yojson))
99

1010
(rule
1111
(target js_name_of_module_id.ml)

compiler/core/j.ml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ and case_clause = {
251251
should_break: bool;
252252
(* true means break *)
253253
comment: string option;
254+
source_loc: Location.t option;
254255
}
255256

256257
and string_clause = Ast_untagged_variants.tag_type * case_clause
@@ -295,8 +296,17 @@ and statement_desc =
295296
| Try of block * (exception_ident * block) option * block option
296297
| Debugger
297298

298-
and expression = {expression_desc: expression_desc; comment: string option}
299-
and statement = {statement_desc: statement_desc; comment: string option}
299+
and expression = {
300+
expression_desc: expression_desc;
301+
comment: string option;
302+
source_loc: Location.t option;
303+
}
304+
305+
and statement = {
306+
statement_desc: statement_desc;
307+
comment: string option;
308+
source_loc: Location.t option;
309+
}
300310

301311
and variable_declaration = {
302312
ident: ident;

compiler/core/js_analyzer.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ let rev_flatten_seq (x : J.expression) =
281281
let rec aux acc (x : J.expression) : J.block =
282282
match x.expression_desc with
283283
| Seq (a, b) -> aux (aux acc a) b
284-
| _ -> {statement_desc = Exp x; comment = None} :: acc
284+
| _ -> {statement_desc = Exp x; comment = None; source_loc = None} :: acc
285285
in
286286
aux [] x
287287

compiler/core/js_dump.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,16 @@ and pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t)
463463
and pp_one_case_clause :
464464
'a. _ -> P.t -> (P.t -> 'a -> unit) -> 'a * J.case_clause -> _ =
465465
fun cxt f pp_cond
466-
(switch_case, ({switch_body; should_break; comment} : J.case_clause)) ->
466+
( switch_case,
467+
({switch_body; should_break; comment; source_loc} : J.case_clause) ) ->
467468
P.newline f;
468469
let cxt =
469470
P.group f 1 (fun _ ->
470471
P.group f 0 (fun _ ->
471472
P.string f L.case;
472473
P.space f;
473474
pp_comment_option f comment;
475+
Js_source_map.mark_source_loc f source_loc;
474476
pp_cond f switch_case;
475477
(* could be integer or string *)
476478
P.space f;
@@ -517,6 +519,7 @@ and vident cxt f (v : J.vident) =
517519
(* The higher the level, the more likely that inner has to add parens *)
518520
and expression ~level:l cxt f (exp : J.expression) : cxt =
519521
pp_comment_option f exp.comment;
522+
Js_source_map.mark_source_loc f exp.source_loc;
520523
expression_desc cxt ~level:l f exp.expression_desc
521524

522525
and expression_desc cxt ~(level : int) f x : cxt =
@@ -1367,6 +1370,8 @@ and variable_declaration top cxt f (variable : J.variable_declaration) : cxt =
13671370
| _ -> (
13681371
match e.expression_desc with
13691372
| Fun {is_method; params; body; env; return_unit; async; directive} ->
1373+
pp_comment_option f e.comment;
1374+
Js_source_map.mark_source_loc f e.source_loc;
13701375
pp_function ?directive ~is_method ~return_unit ~async
13711376
~fn_state:(if top then Name_top name else Name_non_top name)
13721377
cxt f params body env
@@ -1407,8 +1412,10 @@ and pp_comment_option f comment =
14071412
| None -> ()
14081413
| Some x -> pp_comment f x
14091414

1410-
and statement top cxt f ({statement_desc = s; comment; _} : J.statement) : cxt =
1415+
and statement top cxt f
1416+
({statement_desc = s; comment; source_loc} : J.statement) : cxt =
14111417
pp_comment_option f comment;
1418+
Js_source_map.mark_source_loc f source_loc;
14121419
statement_desc top cxt f s
14131420

14141421
and statement_desc top cxt f (s : J.statement_desc) : cxt =

0 commit comments

Comments
 (0)