Skip to content

Commit

Permalink
refactor: unify "node only" and ReactDOMServer
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Nov 8, 2023
1 parent 5a1127f commit 506ce06
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 124 deletions.
69 changes: 69 additions & 0 deletions src/ReactDOMServer.re
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
(),
),
);
47 changes: 47 additions & 0 deletions src/ReactDOMServer.rei
Original file line number Diff line number Diff line change
Expand Up @@ -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;
68 changes: 0 additions & 68 deletions src/ReactDOMServerNodeOnly.re

This file was deleted.

49 changes: 0 additions & 49 deletions src/ReactDOMServerNodeOnly.rei

This file was deleted.

7 changes: 3 additions & 4 deletions src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
(modules
React
ReactDOM
ReactDOMServer
ReactDOMTestUtils
ReactTestRenderer
ReasonReactRouter
Expand All @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions test/ReactDOM__test.re
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ describe("ReactDOM", () => {
hasErrored := true;
},
);
let {pipe, abort: _}: ReactDOMServerNodeOnly.pipeableStream =
ReactDOMServerNodeOnly.renderToPipeableStream(
let {pipe, abort: _}: ReactDOMServer.pipeableStream =
ReactDOMServer.renderToPipeableStream(
<div> "Hello world!"->React.string </div>,
);
pipe(stream);
Expand Down
2 changes: 1 addition & 1 deletion test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

0 comments on commit 506ce06

Please sign in to comment.