Skip to content

Commit

Permalink
Fixup: Fix Source_map uses
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierNicole committed Oct 2, 2024
1 parent ef5c8cc commit b61749c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 39 deletions.
2 changes: 1 addition & 1 deletion compiler/bin-js_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ let output_gen ~standalone ~custom_header ~build_info ~source_map output_file f
let data = Source_map.to_string sm in
"data:application/json;base64," ^ Base64.encode_exn data
| Some output_file ->
Source_map.to_file sm ~file:output_file;
Source_map.to_file sm output_file;
Filename.basename output_file
in
Pretty_print.newline fmt;
Expand Down
4 changes: 2 additions & 2 deletions compiler/bin-wasm_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let update_sourcemap ~sourcemap_root ~sourcemap_don't_inline_content sourcemap_f
if Option.is_some sourcemap_root || not sourcemap_don't_inline_content
then (
let open Source_map in
let source_map, mappings = Source_map.of_file_no_mappings sourcemap_file in
let source_map = Source_map.of_file sourcemap_file in
assert (List.is_empty (Option.value source_map.sources_content ~default:[]));
(* Add source file contents to source map *)
let sources_content =
Expand All @@ -50,7 +50,7 @@ let update_sourcemap ~sourcemap_root ~sourcemap_don't_inline_content sourcemap_f
(if Option.is_some sourcemap_root then sourcemap_root else source_map.sourceroot)
}
in
Source_map.to_file ?mappings source_map ~file:sourcemap_file)
Source_map.to_file source_map sourcemap_file)

let opt_with action x f =
match x with
Expand Down
2 changes: 1 addition & 1 deletion compiler/lib/link_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ let link ~output ~linkall ~mklib ~toplevel ~files ~resolve_sourcemap_url ~source
let s = sourceMappingURL_base64 ^ Base64.encode_exn data in
Line_writer.write oc s
| Some file ->
Source_map.to_file sm ~file;
Source_map.to_file sm file;
let s = sourceMappingURL ^ Filename.basename file in
Line_writer.write oc s));
if times () then Format.eprintf " sourcemap: %a@." Timer.print t
Expand Down
46 changes: 18 additions & 28 deletions compiler/lib/source_map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ let merge = function

(* IO *)

let json ?replace_mappings t =
let json t =
let rewrite_path path =
if Filename.is_relative path
then path
Expand All @@ -331,11 +331,7 @@ let json ?replace_mappings t =
| Some s -> rewrite_path s) )
; "names", `List (List.map t.names ~f:(fun s -> stringlit s))
; "sources", `List (List.map t.sources ~f:(fun s -> stringlit (rewrite_path s)))
; ( "mappings"
, stringlit
(match replace_mappings with
| None -> string_of_mapping t.mappings
| Some m -> m) )
; "mappings", stringlit (string_of_mapping t.mappings)
; ( "sourcesContent"
, `List
(match t.sources_content with
Expand Down Expand Up @@ -384,7 +380,7 @@ let list_stringlit_opt name rest =
| _ -> invalid ()
with Not_found -> None

let of_json ~parse_mappings (json : Yojson.Raw.t) =
let of_json (json : Yojson.Raw.t) =
match json with
| `Assoc (("version", `Intlit version) :: rest) when int_of_string version = 3 ->
let string name json = Option.map ~f:string_of_stringlit (stringlit name json) in
Expand Down Expand Up @@ -413,31 +409,25 @@ let of_json ~parse_mappings (json : Yojson.Raw.t) =
| None -> None
| Some s -> Some (Source_content.of_stringlit s)))
in
let mappings_str = string "mappings" rest in
let mappings =
match parse_mappings, mappings_str with
| false, _ -> mapping_of_string ""
| true, None -> mapping_of_string ""
| true, Some s -> mapping_of_string s
match string "mappings" rest with
| None -> mapping_of_string ""
| Some s -> mapping_of_string s
in
( { version = int_of_float (float_of_string version)
; file
; sourceroot
; names
; sources_content
; sources
; mappings
}
, if parse_mappings then None else mappings_str )
{ version = int_of_float (float_of_string version)
; file
; sourceroot
; names
; sources_content
; sources
; mappings
}
| _ -> invalid ()

let of_string s = of_json ~parse_mappings:true (Yojson.Raw.from_string s) |> fst
let of_string s = of_json (Yojson.Raw.from_string s)

let to_string m = Yojson.Raw.to_string (json m)
let of_file filename = of_json (Yojson.Raw.from_file filename)

let to_file ?mappings m ~file =
let replace_mappings = mappings in
Yojson.Raw.to_file file (json ?replace_mappings m)
let to_string m = Yojson.Raw.to_string (json m)

let of_file_no_mappings filename =
of_json ~parse_mappings:false (Yojson.Raw.from_file filename)
let to_file m file = Yojson.Raw.to_file file (json m)
9 changes: 2 additions & 7 deletions compiler/lib/source_map.mli
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ val to_string : t -> string

val of_string : string -> t

val of_file_no_mappings : string -> t * string option
(** Read source map from a file without parsing the mappings (which can be costly). The
[mappings] field is returned empty and the raw string is returned alongside the map.
*)
val to_file : t -> string -> unit

val to_file : ?mappings:string -> t -> file:string -> unit
(** Write to a file. If a string is supplied as [mappings], use it instead of the
sourcemap's [mappings]. *)
val of_file : string -> t

0 comments on commit b61749c

Please sign in to comment.