Skip to content

Wire to JSX PPX V4 and introduce Jsx* modules #5484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/docson/build-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,26 @@
}
}
},
"jsx-specs": {
"type": "object",
"properties": {
"version": {
"type": "number",
"enum": [3, 4],
"description": "Whether to apply the specific version of JSX PPX transformation"
},
"module": {
"type": "string",
"enum": ["react"],
"description": "JSX module, currently only support the React."
},
"mode": {
"type": "string",
"enum": ["classic", "automatic"],
"description": "JSX transformation mode"
}
}
},
"refmt-specs": {
"oneOf": [
{
Expand Down
8 changes: 8 additions & 0 deletions jscomp/bsb/bsb_build_schemas.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ let reason = "reason"

let react_jsx = "react-jsx"

let jsx = "jsx"

let jsx_version = "version"

let jsx_module = "module"

let jsx_mode = "mode"

let entries = "entries"

let backend = "backend"
Expand Down
52 changes: 51 additions & 1 deletion jscomp/bsb/bsb_config_parse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type json_map = Ext_json_types.t Map_string.t

(* Key is the path *)
let ( |? ) m (key, cb) = m |> Ext_json.test key cb

let ( .?() ) = Map_string.find_opt

(*TODO: it is a little mess that [cwd] and [project dir] are shared*)
Expand Down Expand Up @@ -131,6 +130,53 @@ let extract_reason_react_jsx (map : json_map) =
|> ignore;
!default

let extract_jsx (map : json_map) =
let version : Bsb_config_types.jsx_version option ref = ref None in
let module_ : Bsb_config_types.jsx_module option ref = ref None in
let mode : Bsb_config_types.jsx_mode option ref = ref None in
map
|? ( Bsb_build_schemas.jsx,
`Obj
(fun m ->
match m.?(Bsb_build_schemas.jsx_version) with
| Some (Flo { loc; flo }) -> (
match flo with
| "3" -> version := Some Jsx_v3
| "4" -> version := Some Jsx_v4
| _ -> Bsb_exception.errorf ~loc "Unsupported jsx-version %s" flo
)
| Some x ->
Bsb_exception.config_error x
"Unexpected input (expect a version number) for jsx-version"
| None -> ()) )
|? ( Bsb_build_schemas.jsx,
`Obj
(fun m ->
match m.?(Bsb_build_schemas.jsx_module) with
| Some (Str { loc; str }) -> (
match str with
| "react" -> module_ := Some React
| _ -> Bsb_exception.errorf ~loc "Unsupported jsx-module %s" str)
| Some x ->
Bsb_exception.config_error x
"Unexpected input (jsx module name) for jsx-mode"
| None -> ()) )
|? ( Bsb_build_schemas.jsx,
`Obj
(fun m ->
match m.?(Bsb_build_schemas.jsx_mode) with
| Some (Str { loc; str }) -> (
match str with
| "classic" -> mode := Some Classic
| "automatic" -> mode := Some Automatic
| _ -> Bsb_exception.errorf ~loc "Unsupported jsx-mode %s" str)
| Some x ->
Bsb_exception.config_error x
"Unexpected input (expect classic or automatic) for jsx-mode"
| None -> ()) )
|> ignore;
(!version, !module_, !mode)

let extract_warning (map : json_map) =
match map.?(Bsb_build_schemas.warnings) with
| None -> Bsb_warning.use_default
Expand Down Expand Up @@ -278,6 +324,7 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
.path)
in
let reason_react_jsx = extract_reason_react_jsx map in
let jsx_version, jsx_module, jsx_mode = extract_jsx map in
let bs_dependencies =
extract_dependencies map per_proj_dir Bsb_build_schemas.bs_dependencies
in
Expand Down Expand Up @@ -334,6 +381,9 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
generate_merlin =
extract_boolean map Bsb_build_schemas.generate_merlin false;
reason_react_jsx;
jsx_version;
jsx_module;
jsx_mode;
generators = extract_generators map;
cut_generators;
}
Expand Down
9 changes: 6 additions & 3 deletions jscomp/bsb/bsb_config_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ type dependency = {
}

type dependencies = dependency list

type reason_react_jsx = Jsx_v3
(* string option *)

type jsx_version = Jsx_v3 | Jsx_v4
type jsx_module = React
type jsx_mode = Classic | Automatic
type gentype_config = bool

type command = string

type ppx = { name : string; args : string list }

type t = {
Expand All @@ -62,6 +62,9 @@ type t = {
files_to_install : Bsb_db.module_info Queue.t;
generate_merlin : bool;
reason_react_jsx : reason_react_jsx option;
jsx_version : jsx_version option;
jsx_module : jsx_module option;
jsx_mode : jsx_mode option;
(* whether apply PPX transform or not*)
generators : command Map_string.t;
cut_generators : bool;
Expand Down
11 changes: 8 additions & 3 deletions jscomp/bsb/bsb_ninja_gen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ let output_ninja_and_namespace_map ~per_proj_dir ~package_kind
files_to_install;
built_in_dependency;
reason_react_jsx;
jsx_version;
jsx_module;
jsx_mode;
generators;
namespace;
warning;
Expand Down Expand Up @@ -206,9 +209,11 @@ let output_ninja_and_namespace_map ~per_proj_dir ~package_kind
let rules : Bsb_ninja_rule.builtin =
Bsb_ninja_rule.make_custom_rules ~gentype_config
~has_postbuild:js_post_build_cmd ~pp_file ~has_builtin:built_in_dependency
~reason_react_jsx ~package_specs ~namespace ~digest ~package_name
~warnings ~ppx_files ~bsc_flags ~dpkg_incls (* dev dependencies *)
~lib_incls (* its own libs *) ~dev_incls (* its own devs *)
~reason_react_jsx ~jsx_version ~jsx_module ~jsx_mode ~package_specs
~namespace ~digest ~package_name ~warnings ~ppx_files ~bsc_flags
~dpkg_incls (* dev dependencies *)
~lib_incls (* its own libs *)
~dev_incls (* its own devs *)
~bs_dependencies ~bs_dev_dependencies generators
in

Expand Down
36 changes: 23 additions & 13 deletions jscomp/bsb/bsb_ninja_rule.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
~(has_postbuild : string option) ~(pp_file : string option)
~(has_builtin : bool)
~(reason_react_jsx : Bsb_config_types.reason_react_jsx option)
~(digest : string) ~(package_specs : Bsb_package_specs.t)
~(namespace : string option) ~package_name ~warnings
~(ppx_files : Bsb_config_types.ppx list) ~bsc_flags ~(dpkg_incls : string)
~(lib_incls : string) ~(dev_incls : string) ~bs_dependencies
~bs_dev_dependencies (custom_rules : command Map_string.t) : builtin =
~(jsx_version : Bsb_config_types.jsx_version option)
~(jsx_module : Bsb_config_types.jsx_module option)
~(jsx_mode : Bsb_config_types.jsx_mode option) ~(digest : string)
~(package_specs : Bsb_package_specs.t) ~(namespace : string option)
~package_name ~warnings ~(ppx_files : Bsb_config_types.ppx list) ~bsc_flags
~(dpkg_incls : string) ~(lib_incls : string) ~(dev_incls : string)
~bs_dependencies ~bs_dev_dependencies (custom_rules : command Map_string.t)
: builtin =
let bs_dep = Ext_filename.maybe_quote Bsb_global_paths.vendor_bsdep in
let bsc = Ext_filename.maybe_quote Bsb_global_paths.vendor_bsc in
(* FIXME: We don't need set [-o ${out}] when building ast
Expand Down Expand Up @@ -142,7 +145,7 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
Ext_buffer.add_string buf " $out_last");
Ext_buffer.contents buf
in
let mk_ast ~has_reason_react_jsx : string =
let mk_ast =
Ext_buffer.clear buf;
Ext_buffer.add_string buf bsc;
Ext_buffer.add_char_string buf ' ' warnings;
Expand All @@ -160,18 +163,25 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
| None -> ()
| Some flag ->
Ext_buffer.add_char_string buf ' ' (Bsb_build_util.pp_flag flag));
(match (has_reason_react_jsx, reason_react_jsx) with
| false, _ | _, None -> ()
| _, Some Jsx_v3 -> Ext_buffer.add_string buf " -bs-jsx 3");
(match (reason_react_jsx, jsx_version) with
| _, Some Jsx_v3 -> Ext_buffer.add_string buf " -bs-jsx 3"
| _, Some Jsx_v4 -> Ext_buffer.add_string buf " -bs-jsx 4"
| Some Jsx_v3, None -> Ext_buffer.add_string buf " -bs-jsx 3"
| None, None -> ());
(match jsx_module with
| None -> ()
| Some React -> Ext_buffer.add_string buf " -bs-jsx-module react");
(match jsx_mode with
| None -> ()
| Some Classic -> Ext_buffer.add_string buf " -bs-jsx-mode classic"
| Some Automatic -> Ext_buffer.add_string buf " -bs-jsx-mode automatic");

Ext_buffer.add_char_string buf ' ' bsc_flags;
Ext_buffer.add_string buf " -absname -bs-ast -o $out $i";
Ext_buffer.contents buf
in
let build_ast = define ~command:(mk_ast ~has_reason_react_jsx:false) "ast" in
let build_ast_from_re =
define ~command:(mk_ast ~has_reason_react_jsx:true) "astj"
in
let build_ast = define ~command:mk_ast "ast" in
let build_ast_from_re = define ~command:mk_ast "astj" in

let copy_resources =
define
Expand Down
3 changes: 3 additions & 0 deletions jscomp/bsb/bsb_ninja_rule.mli
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ val make_custom_rules :
pp_file:string option ->
has_builtin:bool ->
reason_react_jsx:Bsb_config_types.reason_react_jsx option ->
jsx_version:Bsb_config_types.jsx_version option ->
jsx_module:Bsb_config_types.jsx_module option ->
jsx_mode:Bsb_config_types.jsx_mode option ->
digest:string ->
package_specs:Bsb_package_specs.t ->
namespace:string option ->
Expand Down
73 changes: 34 additions & 39 deletions jscomp/common/js_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,79 +23,74 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)







(** Browser is not set via command line only for internal use *)

type jsx_version = Jsx_v3 | Jsx_v4
type jsx_module = React
type jsx_mode = Classic | Automatic

let no_version_header = ref false

let cross_module_inline = ref false



let diagnose = ref false
let get_diagnose () =

let get_diagnose () =
!diagnose

# 44 "common/js_config.pp.ml"
# 38 "common/js_config.pp.ml"
|| Sys.getenv_opt "RES_DEBUG_FILE" <> None

# 47 "common/js_config.pp.ml"
# 41 "common/js_config.pp.ml"
(* let (//) = Filename.concat *)

(* let get_packages_info () = !packages_info *)

let no_builtin_ppx = ref false




let tool_name = "ReScript"

let check_div_by_zero = ref true
let get_check_div_by_zero () = !check_div_by_zero



let syntax_only = ref false
let binary_ast = ref false



let debug = ref false

let cmi_only = ref false
let cmi_only = ref false
let cmj_only = ref false

let force_cmi = ref false
let force_cmj = ref false

let jsx_version = ref (-1)


let jsx_version = ref None
let jsx_module = ref React
let jsx_mode = ref Classic
let js_stdout = ref true

let all_module_aliases = ref false

let no_stdlib = ref false

let no_export = ref false
let as_ppx = ref false

let int_of_jsx_version = function
| Jsx_v3 -> 3
| Jsx_v4 -> 4

let string_of_jsx_module = function
| React -> "react"

let as_ppx = ref false
let string_of_jsx_mode = function
| Classic -> "classic"
| Automatic -> "automatic"

let jsx_version_of_int = function
| 3 -> Some Jsx_v3
| 4 -> Some Jsx_v4
| _ -> None

(* option to config `@rescript/std`*)
let customize_runtime : string option ref = ref None
let jsx_module_of_string = function
| "react" -> React
| _ -> React

let as_pp = ref false
let jsx_mode_of_string = function
| "classic" -> Classic
| "automatic" -> Automatic
| _ -> Classic

(* option to config `@rescript/std`*)
let customize_runtime : string option ref = ref None
let as_pp = ref false
let self_stack : string Stack.t = Stack.create ()

let modules = ref false
Loading