From 506ce06f92023c313ce372e4288b989ec1ededcf Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Tue, 7 Nov 2023 17:58:00 -0800 Subject: [PATCH] refactor: unify "node only" and ReactDOMServer --- src/ReactDOMServer.re | 69 ++++++++++++++++++++++++++++++++++ src/ReactDOMServer.rei | 47 +++++++++++++++++++++++ src/ReactDOMServerNodeOnly.re | 68 --------------------------------- src/ReactDOMServerNodeOnly.rei | 49 ------------------------ src/dune | 7 ++-- test/ReactDOM__test.re | 4 +- test/dune | 2 +- 7 files changed, 122 insertions(+), 124 deletions(-) delete mode 100644 src/ReactDOMServerNodeOnly.re delete mode 100644 src/ReactDOMServerNodeOnly.rei diff --git a/src/ReactDOMServer.re b/src/ReactDOMServer.re index 95a1cd02d..e53898ad9 100644 --- a/src/ReactDOMServer.re +++ b/src/ReactDOMServer.re @@ -4,3 +4,72 @@ external renderToString: React.element => string = "renderToString"; [@mel.module "react-dom/server"] external renderToStaticMarkup: React.element => string = "renderToStaticMarkup"; + +[@deriving abstract] +type options = { + [@mel.optional] + bootstrapScriptContent: option(string), + [@mel.optional] + bootstrapScripts: option(array(string)), + [@mel.optional] + bootstrapModules: option(array(string)), + [@mel.optional] + identifierPrefix: option(string), + [@mel.optional] + namespaceURI: option(string), + [@mel.optional] + nonce: option(string), + [@mel.optional] + onAllReady: option(unit => unit), + [@mel.optional] + onError: option(Js.Exn.t => unit), + [@mel.optional] + onShellReady: option(unit => unit), + [@mel.optional] + onShellError: option(Js.Exn.t => unit), + [@mel.optional] + progressiveChunkSize: option(int), +}; + +type pipeableStream = { + /* Using empty object instead of Node.stream since Melange don't provide a binding to node's Stream (https://nodejs.org/api/stream.html) */ + pipe: Js.t({.}) => unit, + abort: unit => unit, +}; + +[@mel.module "react-dom/server"] +external renderToPipeableStream: (React.element, options) => pipeableStream = + "renderToPipeableStream"; + +let renderToPipeableStream = + ( + ~bootstrapScriptContent=?, + ~bootstrapScripts=?, + ~bootstrapModules=?, + ~identifierPrefix=?, + ~namespaceURI=?, + ~nonce=?, + ~onAllReady=?, + ~onError=?, + ~onShellReady=?, + ~onShellError=?, + ~progressiveChunkSize=?, + element, + ) => + renderToPipeableStream( + element, + options( + ~bootstrapScriptContent?, + ~bootstrapScripts?, + ~bootstrapModules?, + ~identifierPrefix?, + ~namespaceURI?, + ~nonce?, + ~onAllReady?, + ~onError?, + ~onShellReady?, + ~onShellError?, + ~progressiveChunkSize?, + (), + ), + ); diff --git a/src/ReactDOMServer.rei b/src/ReactDOMServer.rei index b967a1465..49567a750 100644 --- a/src/ReactDOMServer.rei +++ b/src/ReactDOMServer.rei @@ -5,4 +5,51 @@ external renderToString: React.element => string = "renderToString"; external renderToStaticMarkup: React.element => string = "renderToStaticMarkup"; +[@deriving abstract] +type options = { + [@mel.optional] + bootstrapScriptContent: option(string), + [@mel.optional] + bootstrapScripts: option(array(string)), + [@mel.optional] + bootstrapModules: option(array(string)), + [@mel.optional] + identifierPrefix: option(string), + [@mel.optional] + namespaceURI: option(string), + [@mel.optional] + nonce: option(string), + [@mel.optional] + onAllReady: option(unit => unit), + [@mel.optional] + onError: option(Js.Exn.t => unit), + [@mel.optional] + onShellReady: option(unit => unit), + [@mel.optional] + onShellError: option(Js.Exn.t => unit), + [@mel.optional] + progressiveChunkSize: option(int), +}; +type pipeableStream = { + /* Using empty object instead of Node.stream since Melange don't provide a binding to node's Stream (https://nodejs.org/api/stream.html) */ + pipe: Js.t({.}) => unit, + abort: unit => unit, +}; + +let renderToPipeableStream: + ( + ~bootstrapScriptContent: string=?, + ~bootstrapScripts: array(string)=?, + ~bootstrapModules: array(string)=?, + ~identifierPrefix: string=?, + ~namespaceURI: string=?, + ~nonce: string=?, + ~onAllReady: unit => unit=?, + ~onError: Js.Exn.t => unit=?, + ~onShellReady: unit => unit=?, + ~onShellError: Js.Exn.t => unit=?, + ~progressiveChunkSize: int=?, + React.element + ) => + pipeableStream; diff --git a/src/ReactDOMServerNodeOnly.re b/src/ReactDOMServerNodeOnly.re deleted file mode 100644 index 3d00c0121..000000000 --- a/src/ReactDOMServerNodeOnly.re +++ /dev/null @@ -1,68 +0,0 @@ -[@deriving abstract] -type options = { - [@mel.optional] - bootstrapScriptContent: option(string), - [@mel.optional] - bootstrapScripts: option(array(string)), - [@mel.optional] - bootstrapModules: option(array(string)), - [@mel.optional] - identifierPrefix: option(string), - [@mel.optional] - namespaceURI: option(string), - [@mel.optional] - nonce: option(string), - [@mel.optional] - onAllReady: option(unit => unit), - [@mel.optional] - onError: option(Js.Exn.t => unit), - [@mel.optional] - onShellReady: option(unit => unit), - [@mel.optional] - onShellError: option(Js.Exn.t => unit), - [@mel.optional] - progressiveChunkSize: option(int), -}; - -type pipeableStream = { - /* Using empty object instead of Node.stream since Melange don't provide a binding to node's Stream (https://nodejs.org/api/stream.html) */ - pipe: Js.t({.}) => unit, - abort: unit => unit, -}; - -[@mel.module "react-dom/server"] -external renderToPipeableStream: (React.element, options) => pipeableStream = - "renderToPipeableStream"; - -let renderToPipeableStream = - ( - ~bootstrapScriptContent=?, - ~bootstrapScripts=?, - ~bootstrapModules=?, - ~identifierPrefix=?, - ~namespaceURI=?, - ~nonce=?, - ~onAllReady=?, - ~onError=?, - ~onShellReady=?, - ~onShellError=?, - ~progressiveChunkSize=?, - element, - ) => - renderToPipeableStream( - element, - options( - ~bootstrapScriptContent?, - ~bootstrapScripts?, - ~bootstrapModules?, - ~identifierPrefix?, - ~namespaceURI?, - ~nonce?, - ~onAllReady?, - ~onError?, - ~onShellReady?, - ~onShellError?, - ~progressiveChunkSize?, - (), - ), - ); diff --git a/src/ReactDOMServerNodeOnly.rei b/src/ReactDOMServerNodeOnly.rei deleted file mode 100644 index c1cef959e..000000000 --- a/src/ReactDOMServerNodeOnly.rei +++ /dev/null @@ -1,49 +0,0 @@ -[@deriving abstract] -type options = { - [@mel.optional] - bootstrapScriptContent: option(string), - [@mel.optional] - bootstrapScripts: option(array(string)), - [@mel.optional] - bootstrapModules: option(array(string)), - [@mel.optional] - identifierPrefix: option(string), - [@mel.optional] - namespaceURI: option(string), - [@mel.optional] - nonce: option(string), - [@mel.optional] - onAllReady: option(unit => unit), - [@mel.optional] - onError: option(Js.Exn.t => unit), - [@mel.optional] - onShellReady: option(unit => unit), - [@mel.optional] - onShellError: option(Js.Exn.t => unit), - [@mel.optional] - progressiveChunkSize: option(int), -}; - -type pipeableStream = { - /* Using empty object instead of Node.stream since Melange don't provide a binding to node's Stream (https://nodejs.org/api/stream.html) */ - pipe: Js.t({.}) => unit, - abort: unit => unit, -}; - -let renderToPipeableStream: - ( - ~bootstrapScriptContent: string=?, - ~bootstrapScripts: array(string)=?, - ~bootstrapModules: array(string)=?, - ~identifierPrefix: string=?, - ~namespaceURI: string=?, - ~nonce: string=?, - ~onAllReady: unit => unit=?, - ~onError: Js.Exn.t => unit=?, - ~onShellReady: unit => unit=?, - ~onShellError: Js.Exn.t => unit=?, - ~progressiveChunkSize: int=?, - React.element - ) => - - pipeableStream; diff --git a/src/dune b/src/dune index 9b13b2e8e..1db5e0edb 100644 --- a/src/dune +++ b/src/dune @@ -7,7 +7,6 @@ (modules React ReactDOM - ReactDOMServer ReactDOMTestUtils ReactTestRenderer ReasonReactRouter @@ -18,9 +17,9 @@ (modes melange)) (library - (name ReactDOMServerNodeOnly) - (public_name reason-react.node) - (modules ReactDOMServerNodeOnly) + (name ReactDOMServer) + (public_name reason-react.dom-server) + (modules ReactDOMServer) (libraries react) (preprocess (pps melange.ppx)) diff --git a/test/ReactDOM__test.re b/test/ReactDOM__test.re index 334b35533..9f1a3c6d9 100644 --- a/test/ReactDOM__test.re +++ b/test/ReactDOM__test.re @@ -53,8 +53,8 @@ describe("ReactDOM", () => { hasErrored := true; }, ); - let {pipe, abort: _}: ReactDOMServerNodeOnly.pipeableStream = - ReactDOMServerNodeOnly.renderToPipeableStream( + let {pipe, abort: _}: ReactDOMServer.pipeableStream = + ReactDOMServer.renderToPipeableStream(
"Hello world!"->React.string
, ); pipe(stream); diff --git a/test/dune b/test/dune index a50a84e20..855214a18 100644 --- a/test/dune +++ b/test/dune @@ -3,6 +3,6 @@ (target test) (module_systems (commonjs bs.js)) - (libraries reason-react reason-react.node jest melange.belt) + (libraries reason-react reason-react.dom-server jest melange.belt) (preprocess (pps melange.ppx reason-react-ppx)))