-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Add @browser_only #185
Open
pedrobslisboa
wants to merge
8
commits into
main
Choose a base branch
from
feat/platform-on-expressions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add @browser_only #185
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
962ac17
feat: add new case on platform
pedrobslisboa 3674caf
feat: add browser_only attribute :tada:
pedrobslisboa ff118b8
doc: improve browser_only docs
pedrobslisboa e81cd90
Merge remote-tracking branch 'origin/main' into feat/platform-on-expr…
pedrobslisboa 18c2fd6
feat: add pexp_ident to @browser_only
pedrobslisboa 820acdb
feat: upgrade ocamlformat to 0.27.0
pedrobslisboa ac25f27
feat: add browser_only to core_type
pedrobslisboa 8dd2ab8
feat: add browser_only to ppat_constraint
pedrobslisboa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,12 +6,12 @@ For example, if you're using Webapi to query the DOM or using LocalStorage. This | |
|
||
{1 Usage} | ||
|
||
The ppx expose a [browser_only] attribute that can be used to discard a function or a value, and [switch%platform] to conditionally execute code based on the platform. | ||
The ppx expose a [[@browser_only]] attribute that can be used to discard functions, values, arguments, etc, and [[switch%platform]] to conditionally execute code based on the platform. | ||
|
||
Add [server-reason-react.browser_ppx] into to your pps field under a dune stanzas (melange.emit, libraries or executable) in your dune files. | ||
Add [[server-reason-react.browser_ppx]] into to your pps field under a dune stanzas (melange.emit, libraries or executable) in your dune files. | ||
|
||
You would need to add it on both "server" and "client" dune files. Adding the [-js] flag [server-reason-react.browser_ppx -js] for the client and without for the server: | ||
{[ | ||
{@txt[ | ||
; server exectuable | ||
(executable | ||
(name server) | ||
|
@@ -26,46 +26,65 @@ You would need to add it on both "server" and "client" dune files. Adding the [- | |
]} | ||
|
||
{2 browser_only} | ||
|
||
A code with [[@browser_only]] attribute will keep the function untouched for the client build, but for the server build, will be transformed into to a dead code. If you try to run it on the server, it will fail as it does not exist on the server, and the compiler will crash. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add another page of documentation for |
||
|
||
{[ | ||
let%browser_only countDomNodes = (id) => { | ||
let elements = Webapi.Element.querySelector("#" ++ id); | ||
let arr_elements = Webapi.Element.toArray(elements); | ||
Array.length(arr_elements); | ||
}; | ||
[@browser_only] | ||
let sayHello = () => "Hello"; | ||
|
||
sayHello(); | ||
]} | ||
|
||
Error: | ||
{@txt[ | ||
20 | sayHello(); | ||
^^^^^^^^ | ||
Error: Unbound value sayHello | ||
]} | ||
|
||
The method tagged by [browser_only] will keep the function untouched for the client build, but for the server build, will be transformed to a function that raises the exception [Runtime.Impossible_in_ssr]. | ||
The [[@browser_only]] attribute can be used in any kind of code, including let bindings, function, module, args, etc. Checkout the {{:https://github.com/ml-in-barcelona/server-reason-react/blob/main/packages/browser-ppx/tests/at_browser_only.t}browser_only tests} for more examples. | ||
|
||
Checkout the following example: | ||
{[ | ||
[@browser_only] | ||
let getName = (person) => person.name; | ||
|
||
let getLastName = ([@browser_only] person) => { | ||
[@browser_only] | ||
let lastName = _ => person.last_name; | ||
|
||
"Doe"; | ||
}; | ||
|
||
If this function runs on the server, it will raise an exception, and the server will crash. In order to prevent this, you can use [try] to catch the exception and provide a default behaviour/value. | ||
let hello_world = "Hello world"; | ||
]} | ||
|
||
Following with the example from above: | ||
The final result of the code above, on server, will be: | ||
{[ | ||
let%browser_only countDomNodes = (id) => { | ||
let elements = Webapi.Element.querySelector("#" ++ id); | ||
let arr_elements = Webapi.Element.toArray(elements); | ||
Array.length(arr_elements); | ||
} | ||
|
||
let main = id => | ||
try(countDomNodes(id)) { | ||
| _ => 0 | ||
}; | ||
(); | ||
|
||
let getLastName = (_) => { | ||
"Doe"; | ||
}; | ||
|
||
let hello_world = "Hello world"; | ||
]} | ||
|
||
{2 switch%platform} | ||
|
||
[switch%platform] allows to conditionally execute code based on the platform. There are some cases where you need to run a specific code only on the server or only on the client. | ||
|
||
{[ | ||
switch%platform (Runtime.platform) { | ||
switch%platform () { | ||
| Server => print_endline("This prints to the terminal") | ||
| Client => Js.log("This prints to the console") | ||
}; | ||
]} | ||
|
||
{2 @platform attribute} | ||
|
||
The [@platform] attribute allows to specify code blocks that should only be included in the JavaScript or native build, respectively. This is useful when you have code that is specific to one platform and should not be included in the other. | ||
The [[@platform]] attribute allows to specify code blocks that should only be included in the JavaScript or native build, respectively. This is useful when you have code that is specific to one platform and should not be included in the other. | ||
|
||
For example, you can define two modules, but only one of them should be kept in the final build based on the platform. | ||
{[ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:3 🎉