From e58458573c9345c6647d462495dc070bc921b33a Mon Sep 17 00:00:00 2001 From: David Sancho Date: Tue, 30 Jul 2024 11:57:00 +0200 Subject: [PATCH] Fix demo with dream (#150) --- Makefile | 8 +- demo/README.md | 51 +++ demo/server/{Page.re => Document.re} | 11 +- demo/server/Esbuild.re | 3 +- demo/server/comments.re | 101 +++--- demo/server/dune | 11 +- demo/server/server.re | 303 +++++++----------- demo/universal/native/lib/App.re | 34 +- demo/universal/native/lib/Arrow.re | 27 ++ demo/universal/native/lib/Context.re | 2 +- demo/universal/native/lib/Counter.re | 10 +- demo/universal/native/lib/Header.re | 253 --------------- demo/universal/native/lib/Layout.re | 31 ++ demo/universal/native/lib/Link.re | 43 ++- demo/universal/native/lib/RR.re | 8 +- demo/universal/native/lib/Root.re | 17 - demo/universal/native/lib/Router.re | 4 + demo/universal/native/lib/SubHeader.re | 69 ---- demo/universal/native/lib/Text.re | 66 ++++ demo/universal/native/lib/Theme.re | 15 +- dune-project | 2 +- packages/reactDom/test/test_renderToString.ml | 10 +- server-reason-react.opam | 14 +- 23 files changed, 431 insertions(+), 662 deletions(-) create mode 100644 demo/README.md rename demo/server/{Page.re => Document.re} (85%) create mode 100644 demo/universal/native/lib/Arrow.re delete mode 100644 demo/universal/native/lib/Header.re create mode 100644 demo/universal/native/lib/Layout.re delete mode 100644 demo/universal/native/lib/Root.re create mode 100644 demo/universal/native/lib/Router.re delete mode 100644 demo/universal/native/lib/SubHeader.re create mode 100644 demo/universal/native/lib/Text.re diff --git a/Makefile b/Makefile index 14632bd47..558c0b5f1 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,10 @@ build: ## Build the project, including non installable libraries and executables build-prod: ## Build for production (--profile=prod) $(DUNE) build packages --profile=prod +.PHONY: build-demo +build-demo: ## Build the project, including non installable libraries and executables + $(DUNE) build packages --profile=dev @install @client + .PHONY: dev dev: ## Build in watch mode $(DUNE) build -w --profile=dev @all @@ -87,11 +91,11 @@ lib-test: ## Run library tests $(DUNE) exec test/test.exe .PHONY: demo -demo: build ## Run demo executable +demo: build-demo ## Run demo executable $(DUNE) exec demo/server/server.exe --display-separate-messages --no-print-directory --profile=dev .PHONY: demo-watch -demo-watch: build ## Run demo executable in watch mode +demo-watch: build-demo ## Run demo executable in watch mode $(DUNE) exec demo/server/server.exe --display-separate-messages --no-print-directory --display=quiet --watch --profile=dev .PHONY: subst diff --git a/demo/README.md b/demo/README.md new file mode 100644 index 000000000..67c4cb708 --- /dev/null +++ b/demo/README.md @@ -0,0 +1,51 @@ +# server-reason-react demo + +The app consist of 3 folders: `shared`, `server` and `client`, which encapsulates each compilation target defined by dune. + +## `client/` + +A folder that contains the code executed in the client only. It's defined in dune as a `melange.emit` to emit JavaScript from Reason via Melange. It uses all the ReScript goodies: Belt, Js, etc. Currently is tiny: client only renders the `Shared_js.App` component: + +```re +switch (ReactDOM.querySelector("#root")) { +| Some(el) => ReactDOM.render(, el) +| None => () +}; +``` + +## `server/` + +An executable that expose a dream app with a home route which serves an HTML page. Written in [server-reason-react](https://github.com/ml-in-barcelona/server-reason-react) and send it as a string with `ReactDOM.renderToString` + +## `universal/` + +The folder contains the library for shared code between `client` and `server`. dune generates two libraries `Shared_js` and `Shared_native` (by using `copy_files#`) with separate dependencies for each: + +```dune +; universal/js/dune +(library + (name shared_js) + (modes melange) + (libraries reason-react) + (preprocess (pps reason-react-ppx))) + +(copy_files# "../*.re") +``` + +```dune +; universal/native/dune +(library + (name shared_native) + (modes native) + (libraries + server-reason-react.react + server-reason-react.reactDom) + (preprocess + (pps server-reason-react.ppx))) + +(copy_files# "../*.re") +``` + +`Shared_js` is compiled by Melange to JavaScript while `Shared_native` compiled by OCaml to native. + + diff --git a/demo/server/Page.re b/demo/server/Document.re similarity index 85% rename from demo/server/Page.re rename to demo/server/Document.re index d08c01ed0..4b90dfb50 100644 --- a/demo/server/Page.re +++ b/demo/server/Document.re @@ -4,6 +4,7 @@ let globalStyles = {js| padding: 0; width: 100vw; height: 100vh; + background-color: #161615; /* Theme.Color.black; */ } * { @@ -12,6 +13,15 @@ let globalStyles = {js| -moz-osx-font-smoothing: grayscale; box-sizing: border-box; } + + @keyframes spin { + from { + transform: rotate(0deg); + } + to { + transform: rotate(360deg); + } + } |js}; [@react.component] @@ -30,7 +40,6 @@ let make = (~children, ~script=?) => { dangerouslySetInnerHTML={"__html": globalStyles} />