Skip to content

Commit

Permalink
Prepare sourcemap interface to input index maps (unsupported for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierNicole committed May 23, 2024
1 parent 8c1168b commit ac75d97
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 20 deletions.
17 changes: 11 additions & 6 deletions compiler/lib/link_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,19 @@ let prefix_kind line =
| true -> `Json_base64 (String.length sourceMappingURL_base64)
| false -> `Url (String.length sourceMappingURL))

let rule_out_index_map = function
| `Standard sm -> sm
| `Index _ -> failwith "unexpected index map at this stage"

let action ~resolve_sourcemap_url ~drop_source_map file line =
match prefix_kind line, drop_source_map with
| `Other, (true | false) -> Keep
| `Unit, (true | false) -> Unit
| `Build_info bi, _ -> Build_info bi
| (`Json_base64 _ | `Url _), true -> Drop
| `Json_base64 offset, false ->
Source_map (Source_map_io.of_string (Base64.decode_exn ~off:offset line))
Source_map (
rule_out_index_map (Source_map_io.of_string (Base64.decode_exn ~off:offset line)))
| `Url _, false when not resolve_sourcemap_url -> Drop
| `Url offset, false ->
let url = String.sub line ~pos:offset ~len:(String.length line - offset) in
Expand All @@ -189,7 +194,7 @@ let action ~resolve_sourcemap_url ~drop_source_map file line =
let l = in_channel_length ic in
let content = really_input_string ic l in
close_in ic;
Source_map (Source_map_io.of_string content)
Source_map (rule_out_index_map (Source_map_io.of_string content))

module Units : sig
val read : Line_reader.t -> Unit_info.t -> Unit_info.t
Expand Down Expand Up @@ -471,12 +476,12 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
| _ -> false);
{ version = init_sm.version
; file = init_sm.file
; Composite.sections =
; Index.sections =
(let _, sections =
List.fold_right
sourcemaps_and_line_counts
~f:(fun (sm, generated_line_count) (cur_ofs, sections) ->
let offset = Composite.{ gen_line = cur_ofs; gen_column = 0 } in
let offset = Index.{ gen_line = cur_ofs; gen_column = 0 } in
cur_ofs + generated_line_count, (offset, `Map sm) :: sections)
~init:(0, [])
in
Expand All @@ -493,11 +498,11 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
in
(match file with
| None ->
let data = Source_map_io.Composite.to_string merged_sourcemap in
let data = Source_map_io.Index.to_string merged_sourcemap in
let s = sourceMappingURL_base64 ^ Base64.encode_exn data in
Line_writer.write oc s |> ignore
| Some file ->
Source_map_io.Composite.to_file merged_sourcemap file;
Source_map_io.Index.to_file merged_sourcemap file;
let s = sourceMappingURL ^ Filename.basename file in
Line_writer.write oc s |> ignore);
if times () then Format.eprintf " sourcemap: %a@." Timer.print t
Expand Down
2 changes: 1 addition & 1 deletion compiler/lib/source_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ let concat ~file ~sourceroot s1 s2 =
s2.mappings
}

module Composite = struct
module Index = struct
type offset =
{ gen_line : int
; gen_column : int
Expand Down
2 changes: 1 addition & 1 deletion compiler/lib/source_map.mli
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ val concat : file:string -> sourceroot:string option -> t -> t -> t
codebases, as it decodes the whole source text. This may be fixed in the
future. *)

module Composite : sig
module Index : sig
type offset =
{ gen_line : int
; gen_column : int
Expand Down
10 changes: 5 additions & 5 deletions compiler/lib/source_map_io.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ val to_string : t -> string

val to_file : t -> string -> unit

val of_string : string -> t
module Index : sig
val to_string : Index.t -> string

module Composite : sig
val to_string : Composite.t -> string

val to_file : Composite.t -> string -> unit
val to_file : Index.t -> string -> unit
end

val of_string : string -> [ `Standard of Source_map.t | `Index of Source_map.Index.t ]
12 changes: 8 additions & 4 deletions compiler/lib/source_map_io.yojson.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ let of_json json =
| `Intlit version when Int.equal (int_of_string version) 3 -> ()
| `Floatlit _ | `Intlit _ -> invalid_arg "Source_map_io.of_json: version != 3"
| _ -> invalid_arg "Source_map_io.of_json: version field is not a number");
(match List.assoc "sections" rest with
| _ ->
invalid_arg "Source_map_io.of_json: this seems to be an index map. Reading index maps is currently not supported."
| exception Not_found -> ());
let file = string "file" rest in
let sourceroot = string "sourceRoot" rest in
let names = list_string "names" rest in
Expand Down Expand Up @@ -124,23 +128,23 @@ let of_json json =
}
| _ -> invalid ()

let of_string s = of_json (Yojson.Raw.from_string s)
let of_string s = `Standard (of_json (Yojson.Raw.from_string s))

let to_string m = Yojson.Raw.to_string (json m)

let to_file m file = Yojson.Raw.to_file file (json m)

let enabled = true

module Composite = struct
module Index = struct
let json t =
`Assoc
[ "version", `Intlit (Int.to_string t.Composite.version)
[ "version", `Intlit (Int.to_string t.Index.version)
; "file", stringlit_of_string (rewrite_path t.file)
; ( "sections"
, `List
(List.map
(fun ({ Composite.gen_line; gen_column }, `Map sm) ->
(fun ({ Index.gen_line; gen_column }, `Map sm) ->
`Assoc
[ ( "offset"
, `Assoc
Expand Down
3 changes: 2 additions & 1 deletion compiler/tests-compiler/build_path_prefix_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ let%expect_test _ =
|> compile_cmo_to_javascript ~sourcemap:true ~pretty:false
|> extract_sourcemap
|> function
| Some (sm : Js_of_ocaml_compiler.Source_map.t) ->
| Some (`Standard (sm : Js_of_ocaml_compiler.Source_map.t)) ->
Printf.printf "file: %s\n" sm.file;
Printf.printf "sourceRoot: %s\n" (Option.value ~default:"<none>" sm.sourceroot);
Printf.printf "sources:\n";
List.iter sm.sources ~f:(fun source ->
Printf.printf "- %s\n" (normalize_path source))
| Some (`Index _) -> failwith "unexpected index map"
| None -> failwith "no sourcemap generated!");
[%expect
{|
Expand Down
3 changes: 2 additions & 1 deletion compiler/tests-compiler/sourcemap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ let%expect_test _ =
print_file (Filetype.path_of_js_file js_file);
match extract_sourcemap js_file with
| None -> Printf.printf "No sourcemap found\n"
| Some sm -> print_mapping sm);
| Some (`Standard sm) -> print_mapping sm
| Some (`Index _) -> failwith "unexpected index map");
[%expect
{|
$ cat "test.ml"
Expand Down
2 changes: 1 addition & 1 deletion compiler/tests-compiler/util/util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ val compile_bc_to_javascript :
val jsoo_minify :
?flags:string list -> pretty:bool -> Filetype.js_file -> Filetype.js_file

val extract_sourcemap : Filetype.js_file -> Js_of_ocaml_compiler.Source_map.t option
val extract_sourcemap : Filetype.js_file -> [ `Standard of Js_of_ocaml_compiler.Source_map.t | `Index of Js_of_ocaml_compiler.Source_map.Index.t ] option

val run_javascript : Filetype.js_file -> string

Expand Down

0 comments on commit ac75d97

Please sign in to comment.