Skip to content

Commit b7e5ae1

Browse files
authored
make playground bundle endpoint customizable (#1099)
* make playground bundle endpoint customizable * fix build phase
1 parent 6e2b248 commit b7e5ae1

File tree

7 files changed

+78
-32
lines changed

7 files changed

+78
-32
lines changed

src/Playground.res

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ module App = {
14191419
let initialReContent = `Js.log("Hello Reason 3.6!");`
14201420

14211421
@react.component
1422-
let make = (~versions: array<string>) => {
1422+
let make = (~bundleBaseUrl: string, ~versions: array<string>) => {
14231423
let router = Next.Router.useRouter()
14241424

14251425
let versions =
@@ -1436,14 +1436,18 @@ let make = (~versions: array<string>) => {
14361436
cmp(b) - cmp(a)
14371437
})
14381438

1439-
let lastStableVersion = versions->Array.find(version => version.preRelease->Option.isNone)
1440-
1441-
let initialVersion = switch Dict.get(router.query, "version") {
1442-
| Some(version) => version->Semver.parse
1443-
| None =>
1444-
switch Url.getVersionFromStorage(Playground) {
1445-
| Some(v) => v->Semver.parse
1446-
| None => lastStableVersion
1439+
let initialVersion = switch versions {
1440+
| [v] => Some(v) // only single version available. maybe local dev.
1441+
| versions => {
1442+
let lastStableVersion = versions->Array.find(version => version.preRelease->Option.isNone)
1443+
switch Dict.get(router.query, "version") {
1444+
| Some(version) => version->Semver.parse
1445+
| None =>
1446+
switch Url.getVersionFromStorage(Playground) {
1447+
| Some(v) => v->Semver.parse
1448+
| None => lastStableVersion
1449+
}
1450+
}
14471451
}
14481452
}
14491453

@@ -1470,6 +1474,7 @@ let make = (~versions: array<string>) => {
14701474
let (actionCount, setActionCount) = React.useState(_ => 0)
14711475
let onAction = _ => setActionCount(prev => prev > 1000000 ? 0 : prev + 1)
14721476
let (compilerState, compilerDispatch) = useCompilerManager(
1477+
~bundleBaseUrl,
14731478
~initialVersion?,
14741479
~initialModuleSystem?,
14751480
~initialLang,

src/Playground.resi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
@react.component
2-
let make: (~versions: array<string>) => React.element
2+
let make: (~bundleBaseUrl: string, ~versions: array<string>) => React.element

src/Try.res

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
type props = {versions: array<string>}
1+
type props = {
2+
bundleBaseUrl: string,
3+
versions: array<string>,
4+
}
25

36
let default = props => {
47
let (isOverlayOpen, setOverlayOpen) = React.useState(() => false)
@@ -19,7 +22,13 @@ let default = props => {
1922
},
2023
)
2124

22-
let playground = React.createElement(lazyPlayground, {versions: props.versions})
25+
let playground = React.createElement(
26+
lazyPlayground,
27+
{
28+
bundleBaseUrl: props.bundleBaseUrl,
29+
versions: props.versions,
30+
},
31+
)
2332

2433
<>
2534
<Meta
@@ -40,14 +49,36 @@ let default = props => {
4049
}
4150

4251
let getStaticProps: Next.GetStaticProps.t<props, _> = async _ => {
52+
let (bundleBaseUrl, versionsBaseUrl) = switch (
53+
Node.Process.Env.playgroundBundleEndpoint,
54+
Node.Process.Env.nodeEnv,
55+
) {
56+
| (Some(baseUrl), _) => (baseUrl, baseUrl)
57+
| (None, "development") => {
58+
// Use remote bundles in dev
59+
let baseUrl = "https://cdn.rescript-lang.org"
60+
(baseUrl, baseUrl)
61+
}
62+
| (None, _) => (
63+
// Use same-origin requests for the bundle
64+
"",
65+
// There is no version endpoint in the build phase
66+
"https://cdn.rescript-lang.org",
67+
)
68+
}
4369
let versions = {
44-
let response = await fetch("https://cdn.rescript-lang.org/playground-bundles/versions.json")
70+
let response = await fetch(versionsBaseUrl + "/playground-bundles/versions.json")
4571
let json = await WebAPI.Response.json(response)
4672
json
4773
->JSON.Decode.array
4874
->Option.getOrThrow
4975
->Array.map(json => json->JSON.Decode.string->Option.getOrThrow)
5076
}
5177

52-
{"props": {versions: versions}}
78+
{
79+
"props": {
80+
bundleBaseUrl,
81+
versions,
82+
},
83+
}
5384
}

src/Try.resi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
type props = {versions: array<string>}
1+
type props = {
2+
bundleBaseUrl: string,
3+
versions: array<string>,
4+
}
25
let default: props => React.element
36
let getStaticProps: Next.GetStaticProps.t<props, 'a>

src/bindings/Node.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ module Process = {
1818
@scope("process") external exit: int => unit = "exit"
1919
module Env = {
2020
@scope(("process", "env")) external nodeEnv: string = "NODE_ENV"
21+
@scope(("process", "env"))
22+
external playgroundBundleEndpoint: option<string> = "PLAYGROUND_BUNDLE_ENDPOINT"
2123
}
2224
}
2325

src/common/CompilerManagerHook.res

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,13 @@ module LoadScript = {
3535
}
3636

3737
module CdnMeta = {
38-
let baseUrl =
39-
Node.Process.Env.nodeEnv === "development"
40-
? "https://cdn.rescript-lang.org"
41-
: "" + "/playground-bundles"
38+
let getCompilerUrl = (baseUrl, version): string =>
39+
`${baseUrl}/${Semver.toString(version)}/compiler.js`
4240

43-
let getCompilerUrl = (version): string => `${baseUrl}/${Semver.toString(version)}/compiler.js`
44-
45-
let getLibraryCmijUrl = (version, libraryName: string): string =>
41+
let getLibraryCmijUrl = (baseUrl, version, libraryName: string): string =>
4642
`${baseUrl}/${Semver.toString(version)}/${libraryName}/cmij.js`
4743

48-
let getStdlibRuntimeUrl = (version, filename) =>
44+
let getStdlibRuntimeUrl = (baseUrl, version, filename) =>
4945
`${baseUrl}/${Semver.toString(version)}/compiler-builtins/stdlib/${filename}`
5046
}
5147

@@ -104,11 +100,11 @@ let getOpenModules = (~apiVersion: Version.t, ~libraries: array<string>): option
104100
We coupled the compiler / library loading to prevent ppl to try loading compiler / cmij files
105101
separately and cause all kinds of race conditions.
106102
*/
107-
let attachCompilerAndLibraries = async (~version, ~libraries: array<string>, ()): result<
103+
let attachCompilerAndLibraries = async (~baseUrl, ~version, ~libraries: array<string>, ()): result<
108104
unit,
109105
array<string>,
110106
> => {
111-
let compilerUrl = CdnMeta.getCompilerUrl(version)
107+
let compilerUrl = CdnMeta.getCompilerUrl(baseUrl, version)
112108

113109
// Useful for debugging our local build
114110
/* let compilerUrl = "/static/linked-bs-bundle.js"; */
@@ -117,7 +113,7 @@ let attachCompilerAndLibraries = async (~version, ~libraries: array<string>, ())
117113
| Error(_) => Error([`Could not load compiler from url ${compilerUrl}`])
118114
| Ok(_) =>
119115
let promises = Array.map(libraries, async lib => {
120-
let cmijUrl = CdnMeta.getLibraryCmijUrl(version, lib)
116+
let cmijUrl = CdnMeta.getLibraryCmijUrl(baseUrl, version, lib)
121117
switch await LoadScript.loadScriptPromise(cmijUrl) {
122118
| Error(_) => Error(`Could not load cmij from url ${cmijUrl}`)
123119
| r => r
@@ -222,6 +218,7 @@ let defaultModuleSystem = "esmodule"
222218
// component to give feedback to the user that an action happened (useful in
223219
// cases where the output didn't visually change)
224220
let useCompilerManager = (
221+
~bundleBaseUrl: string,
225222
~initialVersion: option<Semver.t>=?,
226223
~initialModuleSystem=defaultModuleSystem,
227224
~initialLang: Lang.t=Res,
@@ -405,7 +402,12 @@ let useCompilerManager = (
405402
// Latest version is already running on @rescript/react
406403
let libraries = getLibrariesForVersion(~version)
407404

408-
switch await attachCompilerAndLibraries(~version, ~libraries, ()) {
405+
switch await attachCompilerAndLibraries(
406+
~baseUrl=bundleBaseUrl,
407+
~version,
408+
~libraries,
409+
(),
410+
) {
409411
| Ok() =>
410412
let instance = Compiler.make()
411413
let apiVersion = apiVersion->Version.fromString
@@ -460,14 +462,16 @@ let useCompilerManager = (
460462
| SwitchingCompiler(ready, version) =>
461463
let libraries = getLibrariesForVersion(~version)
462464

463-
switch await attachCompilerAndLibraries(~version, ~libraries, ()) {
465+
switch await attachCompilerAndLibraries(~baseUrl=bundleBaseUrl, ~version, ~libraries, ()) {
464466
| Ok() =>
465467
// Make sure to remove the previous script from the DOM as well
466-
LoadScript.removeScript(~src=CdnMeta.getCompilerUrl(ready.selected.id))
468+
LoadScript.removeScript(~src=CdnMeta.getCompilerUrl(bundleBaseUrl, ready.selected.id))
467469

468470
// We are removing the previous libraries, therefore we use ready.selected here
469471
Array.forEach(ready.selected.libraries, lib =>
470-
LoadScript.removeScript(~src=CdnMeta.getLibraryCmijUrl(ready.selected.id, lib))
472+
LoadScript.removeScript(
473+
~src=CdnMeta.getLibraryCmijUrl(bundleBaseUrl, ready.selected.id, lib),
474+
)
471475
)
472476

473477
let instance = Compiler.make()
@@ -576,7 +580,7 @@ let useCompilerManager = (
576580
}
577581
| version => version
578582
}
579-
CdnMeta.getStdlibRuntimeUrl(compilerVersion, filename)
583+
CdnMeta.getStdlibRuntimeUrl(bundleBaseUrl, compilerVersion, filename)
580584
})
581585

582586
entryPointExists

src/common/CompilerManagerHook.resi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type ready = {
3131
}
3232

3333
module CdnMeta: {
34-
let getStdlibRuntimeUrl: (Semver.t, string) => string
34+
let getStdlibRuntimeUrl: (string, Semver.t, string) => string
3535
}
3636

3737
type state =
@@ -53,6 +53,7 @@ type action =
5353
| RunCode
5454

5555
let useCompilerManager: (
56+
~bundleBaseUrl: string,
5657
~initialVersion: Semver.t=?,
5758
~initialModuleSystem: string=?,
5859
~initialLang: Lang.t=?,

0 commit comments

Comments
 (0)