Skip to content

Commit 8ce1fe0

Browse files
committed
[add] ppx for virtual DOM
1 parent 3b4a4dd commit 8ce1fe0

File tree

9 files changed

+277
-70
lines changed

9 files changed

+277
-70
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ log
1717
resources/js/core.js
1818
setup.data
1919
setup.log
20+
ppx_react_html.byte

_oasis

+16-3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,28 @@ Authors: Roma Sokolov
66
License: MIT
77
Plugins: DevFiles (0.2)
88
BuildTools: ocamlbuild
9+
OCamlVersion: >= 4.02
10+
11+
PreInstallCommand: $ocamlfind install ppx_react_html src/syntax/META
12+
PreUninstallCommand: $ocamlfind remove ppx_react_html
13+
14+
Executable ppx_react_html
15+
Path: src/syntax
16+
BuildDepends: compiler-libs.common
17+
MainIs: ppx_react_html.ml
18+
CompiledObject: byte
19+
920

1021
Executable example
1122
Path: src
1223
MainIs: core.ml
1324
Install: false
1425
CompiledObject: byte
15-
BuildTools: camlp4o
16-
BuildDepends: js_of_ocaml, js_of_ocaml.syntax
17-
ByteOpt: -g -syntax camlp4o -package js_of_ocaml -package js_of_ocaml.syntax
26+
BuildTools: ocamlbuild
27+
BuildDepends: js_of_ocaml
28+
ByteOpt+: -g
29+
ByteOpt+: -ppx src/syntax/ppx_react_html.byte
30+
1831

1932
PostBuildCommand:
2033
js_of_ocaml --pretty --noinline --sourcemap core.byte -o core.js

_tags

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# OASIS_START
2-
# DO NOT EDIT (digest: 719254c77e8c9114157239c7801cc2fa)
2+
# DO NOT EDIT (digest: d58e588b87547ac9fcb27ee96463b4b0)
33
# Ignore VCS directories, you can use the same kind of rule outside
44
# OASIS_START/STOP if you want to exclude directories that contains
55
# useless stuff for the build process
@@ -14,11 +14,12 @@ true: annot, bin_annot
1414
".git": not_hygienic
1515
"_darcs": -traverse
1616
"_darcs": not_hygienic
17+
# Executable ppx_react_html
18+
"src/syntax/ppx_react_html.byte": package(compiler-libs.common)
19+
<src/syntax/*.ml{,i,y}>: package(compiler-libs.common)
1720
# Executable example
1821
"src/core.byte": oasis_executable_example_byte
1922
<src/*.ml{,i,y}>: oasis_executable_example_byte
20-
"src/core.byte": pkg_js_of_ocaml
21-
"src/core.byte": pkg_js_of_ocaml.syntax
22-
<src/*.ml{,i,y}>: pkg_js_of_ocaml
23-
<src/*.ml{,i,y}>: pkg_js_of_ocaml.syntax
23+
"src/core.byte": package(js_of_ocaml)
24+
<src/*.ml{,i,y}>: package(js_of_ocaml)
2425
# OASIS_STOP

myocamlbuild.ml

+5-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* OASIS_START *)
2-
(* DO NOT EDIT (digest: 0902c890be3194e6dbc8220bb9e646a9) *)
2+
(* DO NOT EDIT (digest: 2911513bc8e663925b076264b436b9dd) *)
33
module OASISGettext = struct
44
(* # 22 "src/oasis/OASISGettext.ml" *)
55

@@ -614,44 +614,17 @@ let package_default =
614614
(["oasis_executable_example_byte"; "ocaml"; "link"; "byte"],
615615
[
616616
(OASISExpr.EBool true,
617-
S
618-
[
619-
A "-g";
620-
A "-syntax";
621-
A "camlp4o";
622-
A "-package";
623-
A "js_of_ocaml";
624-
A "-package";
625-
A "js_of_ocaml.syntax"
626-
])
617+
S [A "-g"; A "-ppx"; A "src/syntax/ppx_react_html.byte"])
627618
]);
628619
(["oasis_executable_example_byte"; "ocaml"; "ocamldep"; "byte"],
629620
[
630621
(OASISExpr.EBool true,
631-
S
632-
[
633-
A "-g";
634-
A "-syntax";
635-
A "camlp4o";
636-
A "-package";
637-
A "js_of_ocaml";
638-
A "-package";
639-
A "js_of_ocaml.syntax"
640-
])
622+
S [A "-g"; A "-ppx"; A "src/syntax/ppx_react_html.byte"])
641623
]);
642624
(["oasis_executable_example_byte"; "ocaml"; "compile"; "byte"],
643625
[
644626
(OASISExpr.EBool true,
645-
S
646-
[
647-
A "-g";
648-
A "-syntax";
649-
A "camlp4o";
650-
A "-package";
651-
A "js_of_ocaml";
652-
A "-package";
653-
A "js_of_ocaml.syntax"
654-
])
627+
S [A "-g"; A "-ppx"; A "src/syntax/ppx_react_html.byte"])
655628
])
656629
];
657630
includes = []
@@ -662,6 +635,6 @@ let conf = {MyOCamlbuildFindlib.no_automatic_syntax = false}
662635

663636
let dispatch_default = MyOCamlbuildBase.dispatch_default conf package_default;;
664637

665-
# 666 "myocamlbuild.ml"
638+
# 639 "myocamlbuild.ml"
666639
(* OASIS_STOP *)
667640
Ocamlbuild_plugin.dispatch dispatch_default;;

setup.ml

+45-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(* setup.ml generated for the first time by OASIS v0.4.5 *)
22

33
(* OASIS_START *)
4-
(* DO NOT EDIT (digest: 3863c173cefd5fad2b8a58b26d472844) *)
4+
(* DO NOT EDIT (digest: 38cd7826289685f14fb36e6692ea81da) *)
55
(*
66
Regenerated by OASIS v0.4.5
77
Visit http://oasis.forge.ocamlcore.org for more information and
@@ -6654,7 +6654,7 @@ open OASISTypes;;
66546654
let setup_t =
66556655
{
66566656
BaseSetup.configure = InternalConfigurePlugin.configure;
6657-
build = OCamlbuildPlugin.build [];
6657+
build = OCamlbuildPlugin.build ["-use-ocamlfind"];
66586658
test = [];
66596659
doc = [];
66606660
install = InternalInstallPlugin.install;
@@ -6668,7 +6668,7 @@ let setup_t =
66686668
package =
66696669
{
66706670
oasis_version = "0.4";
6671-
ocaml_version = None;
6671+
ocaml_version = Some (OASISVersion.VGreaterEqual "4.02");
66726672
findlib_version = None;
66736673
alpha_features = [];
66746674
beta_features = [];
@@ -6724,12 +6724,22 @@ let setup_t =
67246724
install_type = (`Install, "internal", Some "0.4");
67256725
install_custom =
67266726
{
6727-
pre_command = [(OASISExpr.EBool true, None)];
6727+
pre_command =
6728+
[
6729+
(OASISExpr.EBool true,
6730+
Some
6731+
(("$ocamlfind",
6732+
["install"; "ppx_react_html"; "src/syntax/META"])))
6733+
];
67286734
post_command = [(OASISExpr.EBool true, None)]
67296735
};
67306736
uninstall_custom =
67316737
{
6732-
pre_command = [(OASISExpr.EBool true, None)];
6738+
pre_command =
6739+
[
6740+
(OASISExpr.EBool true,
6741+
Some (("$ocamlfind", ["remove"; "ppx_react_html"])))
6742+
];
67336743
post_command = [(OASISExpr.EBool true, None)]
67346744
};
67356745
clean_custom =
@@ -6749,6 +6759,30 @@ let setup_t =
67496759
files_ab = [];
67506760
sections =
67516761
[
6762+
Executable
6763+
({
6764+
cs_name = "ppx_react_html";
6765+
cs_data = PropList.Data.create ();
6766+
cs_plugin_data = []
6767+
},
6768+
{
6769+
bs_build = [(OASISExpr.EBool true, true)];
6770+
bs_install = [(OASISExpr.EBool true, true)];
6771+
bs_path = "src/syntax";
6772+
bs_compiled_object = Byte;
6773+
bs_build_depends =
6774+
[FindlibPackage ("compiler-libs.common", None)];
6775+
bs_build_tools = [ExternalTool "ocamlbuild"];
6776+
bs_c_sources = [];
6777+
bs_data_files = [];
6778+
bs_ccopt = [(OASISExpr.EBool true, [])];
6779+
bs_cclib = [(OASISExpr.EBool true, [])];
6780+
bs_dlllib = [(OASISExpr.EBool true, [])];
6781+
bs_dllpath = [(OASISExpr.EBool true, [])];
6782+
bs_byteopt = [(OASISExpr.EBool true, [])];
6783+
bs_nativeopt = [(OASISExpr.EBool true, [])]
6784+
},
6785+
{exec_custom = false; exec_main_is = "ppx_react_html.ml"});
67526786
Executable
67536787
({
67546788
cs_name = "example";
@@ -6761,12 +6795,10 @@ let setup_t =
67616795
bs_path = "src";
67626796
bs_compiled_object = Byte;
67636797
bs_build_depends =
6764-
[
6765-
FindlibPackage ("js_of_ocaml", None);
6766-
FindlibPackage ("js_of_ocaml.syntax", None)
6767-
];
6798+
[FindlibPackage ("js_of_ocaml", None)];
67686799
bs_build_tools =
6769-
[ExternalTool "ocamlbuild"; ExternalTool "camlp4o"];
6800+
[ExternalTool "ocamlbuild"; ExternalTool "ocamlbuild"
6801+
];
67706802
bs_c_sources = [];
67716803
bs_data_files = [];
67726804
bs_ccopt = [(OASISExpr.EBool true, [])];
@@ -6776,15 +6808,7 @@ let setup_t =
67766808
bs_byteopt =
67776809
[
67786810
(OASISExpr.EBool true,
6779-
[
6780-
"-g";
6781-
"-syntax";
6782-
"camlp4o";
6783-
"-package";
6784-
"js_of_ocaml";
6785-
"-package";
6786-
"js_of_ocaml.syntax"
6787-
])
6811+
["-g"; "-ppx"; "src/syntax/ppx_react_html.byte"])
67886812
];
67896813
bs_nativeopt = [(OASISExpr.EBool true, [])]
67906814
},
@@ -6797,14 +6821,14 @@ let setup_t =
67976821
};
67986822
oasis_fn = Some "_oasis";
67996823
oasis_version = "0.4.5";
6800-
oasis_digest = Some "ܤ\031Íb\135ßc\156å}y\011\147";
6824+
oasis_digest = Some "\153[³ ;Õþïl6\156\\";
68016825
oasis_exec = None;
68026826
oasis_setup_args = [];
68036827
setup_update = false
68046828
};;
68056829

68066830
let setup () = BaseSetup.setup setup_t;;
68076831

6808-
# 6809 "setup.ml"
6832+
# 6833 "setup.ml"
68096833
(* OASIS_STOP *)
68106834
let () = setup ();;

src/core.ml

+39-9
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ module React:REACT = struct
124124
Js.Unsafe.meth_call react "render" [| inj comp; inj node |]
125125
end
126126

127+
127128
module StringComponent = struct
128129
type arg = string
129130
type jsval = Js.js_string Js.t
@@ -134,27 +135,40 @@ end
134135
module CommentList = struct
135136
include StringComponent
136137
let render st =
137-
React.tag "div" O.(empty <| el_of_string "className" "commentList")
138-
[React.text st]
138+
[%html
139+
[%div [%opts className="commentList"]
140+
[
141+
React.text st
142+
]
143+
]
144+
]
139145
end
140146
let comment_list = React.defcomponent (module CommentList)
141147

142148
module CommentForm = struct
143149
include StringComponent
144150
let render st =
145-
React.tag "div" O.(empty <| el_of_string "className" "commentForm")
146-
[React.text st]
151+
[%html
152+
[%div [%opts className="commentForm"]
153+
[
154+
React.text st
155+
]
156+
]
157+
]
147158
end
148159
let comment_form = React.defcomponent (module CommentForm)
149160

150161
module CommentBox = struct
151162
include StringComponent
152163
let render st =
153-
let header = React.tag "h1" O.empty [React.text "Comment: "] in
154-
React.tag "div" O.(empty <| el_of_string "className" "commentBox")
155-
[React.component header;
164+
[%html
165+
[%div [%opts className="commentBox"]
166+
[[%h1 [%opts] ["Comment:"]];
156167
React.component @@ comment_list "This is comment list";
157-
React.component @@ comment_form "This is comment form"]
168+
React.component @@ comment_form "This is comment form"
169+
]
170+
]
171+
]
158172
end
159173

160174
let comment_box = React.defcomponent (module CommentBox)
@@ -164,5 +178,21 @@ let start _:(bool Js.t) =
164178
let () = React.render (comment_box "This is a comment box") div in
165179
Js._false
166180

181+
182+
(* let () = *)
183+
(* Html.window##onload <- Dom.handler start *)
184+
185+
(* XXX: Use hand-written expansion of above expr in the absence of camlp4 *)
167186
let () =
168-
Html.window##onload <- Dom.handler start
187+
let _ =
188+
let module M =
189+
struct
190+
let res =
191+
let _ = (Html.window : 'B Js.t) in
192+
let _ =
193+
fun (x : 'B) -> (x#onload : < set : 'A -> unit; .. > Js.gen_prop)
194+
in (Dom.handler start : 'A)
195+
196+
end
197+
in M.res
198+
in Js.Unsafe.set Html.window "onload" (Dom.handler start)

src/syntax/#META#

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

src/syntax/META.ab

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
version = "0.0.1"
2+
ppx = "ppx_react_html"

0 commit comments

Comments
 (0)