Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions src/Playground.res
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ module App = {
let initialReContent = `Js.log("Hello Reason 3.6!");`

@react.component
let make = (~bundleBaseUrl: string, ~versions: array<string>) => {
let make = (~versions: array<string>) => {
let router = Next.Router.useRouter()

let versions =
Expand All @@ -1436,18 +1436,14 @@ let make = (~bundleBaseUrl: string, ~versions: array<string>) => {
cmp(b) - cmp(a)
})

let initialVersion = switch versions {
| [v] => Some(v) // only single version available. maybe local dev.
| versions => {
let lastStableVersion = versions->Array.find(version => version.preRelease->Option.isNone)
switch Dict.get(router.query, "version") {
| Some(version) => version->Semver.parse
| None =>
switch Url.getVersionFromStorage(Playground) {
| Some(v) => v->Semver.parse
| None => lastStableVersion
}
}
let lastStableVersion = versions->Array.find(version => version.preRelease->Option.isNone)

let initialVersion = switch Dict.get(router.query, "version") {
| Some(version) => version->Semver.parse
| None =>
switch Url.getVersionFromStorage(Playground) {
| Some(v) => v->Semver.parse
| None => lastStableVersion
}
}

Expand All @@ -1474,7 +1470,6 @@ let make = (~bundleBaseUrl: string, ~versions: array<string>) => {
let (actionCount, setActionCount) = React.useState(_ => 0)
let onAction = _ => setActionCount(prev => prev > 1000000 ? 0 : prev + 1)
let (compilerState, compilerDispatch) = useCompilerManager(
~bundleBaseUrl,
~initialVersion?,
~initialModuleSystem?,
~initialLang,
Expand Down
2 changes: 1 addition & 1 deletion src/Playground.resi
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@react.component
let make: (~bundleBaseUrl: string, ~versions: array<string>) => React.element
let make: (~versions: array<string>) => React.element
39 changes: 4 additions & 35 deletions src/Try.res
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
type props = {
bundleBaseUrl: string,
versions: array<string>,
}
type props = {versions: array<string>}

let default = props => {
let (isOverlayOpen, setOverlayOpen) = React.useState(() => false)
Expand All @@ -22,13 +19,7 @@ let default = props => {
},
)

let playground = React.createElement(
lazyPlayground,
{
bundleBaseUrl: props.bundleBaseUrl,
versions: props.versions,
},
)
let playground = React.createElement(lazyPlayground, {versions: props.versions})

<>
<Meta
Expand All @@ -49,36 +40,14 @@ let default = props => {
}

let getStaticProps: Next.GetStaticProps.t<props, _> = async _ => {
let (bundleBaseUrl, versionsBaseUrl) = switch (
Node.Process.Env.playgroundBundleEndpoint,
Node.Process.Env.nodeEnv,
) {
| (Some(baseUrl), _) => (baseUrl, baseUrl)
| (None, "development") => {
// Use remote bundles in dev
let baseUrl = "https://cdn.rescript-lang.org"
(baseUrl, baseUrl)
}
| (None, _) => (
// Use same-origin requests for the bundle
"",
// There is no version endpoint in the build phase
"https://cdn.rescript-lang.org",
)
}
let versions = {
let response = await fetch(versionsBaseUrl + "/playground-bundles/versions.json")
let response = await fetch("https://cdn.rescript-lang.org/playground-bundles/versions.json")
let json = await WebAPI.Response.json(response)
json
->JSON.Decode.array
->Option.getOrThrow
->Array.map(json => json->JSON.Decode.string->Option.getOrThrow)
}

{
"props": {
bundleBaseUrl,
versions,
},
}
{"props": {versions: versions}}
}
5 changes: 1 addition & 4 deletions src/Try.resi
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
type props = {
bundleBaseUrl: string,
versions: array<string>,
}
type props = {versions: array<string>}
let default: props => React.element
let getStaticProps: Next.GetStaticProps.t<props, 'a>
2 changes: 0 additions & 2 deletions src/bindings/Node.res
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ module Process = {
@scope("process") external exit: int => unit = "exit"
module Env = {
@scope(("process", "env")) external nodeEnv: string = "NODE_ENV"
@scope(("process", "env"))
external playgroundBundleEndpoint: option<string> = "PLAYGROUND_BUNDLE_ENDPOINT"
}
}

Expand Down
36 changes: 16 additions & 20 deletions src/common/CompilerManagerHook.res
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ module LoadScript = {
}

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

let getLibraryCmijUrl = (baseUrl, version, libraryName: string): string =>
let getCompilerUrl = (version): string => `${baseUrl}/${Semver.toString(version)}/compiler.js`

let getLibraryCmijUrl = (version, libraryName: string): string =>
`${baseUrl}/${Semver.toString(version)}/${libraryName}/cmij.js`

let getStdlibRuntimeUrl = (baseUrl, version, filename) =>
let getStdlibRuntimeUrl = (version, filename) =>
`${baseUrl}/${Semver.toString(version)}/compiler-builtins/stdlib/${filename}`
}

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

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

switch await attachCompilerAndLibraries(
~baseUrl=bundleBaseUrl,
~version,
~libraries,
(),
) {
switch await attachCompilerAndLibraries(~version, ~libraries, ()) {
| Ok() =>
let instance = Compiler.make()
let apiVersion = apiVersion->Version.fromString
Expand Down Expand Up @@ -462,16 +460,14 @@ let useCompilerManager = (
| SwitchingCompiler(ready, version) =>
let libraries = getLibrariesForVersion(~version)

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

// We are removing the previous libraries, therefore we use ready.selected here
Array.forEach(ready.selected.libraries, lib =>
LoadScript.removeScript(
~src=CdnMeta.getLibraryCmijUrl(bundleBaseUrl, ready.selected.id, lib),
)
LoadScript.removeScript(~src=CdnMeta.getLibraryCmijUrl(ready.selected.id, lib))
)

let instance = Compiler.make()
Expand Down Expand Up @@ -580,7 +576,7 @@ let useCompilerManager = (
}
| version => version
}
CdnMeta.getStdlibRuntimeUrl(bundleBaseUrl, compilerVersion, filename)
CdnMeta.getStdlibRuntimeUrl(compilerVersion, filename)
})

entryPointExists
Expand Down
3 changes: 1 addition & 2 deletions src/common/CompilerManagerHook.resi
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ready = {
}

module CdnMeta: {
let getStdlibRuntimeUrl: (string, Semver.t, string) => string
let getStdlibRuntimeUrl: (Semver.t, string) => string
}

type state =
Expand All @@ -53,7 +53,6 @@ type action =
| RunCode

let useCompilerManager: (
~bundleBaseUrl: string,
~initialVersion: Semver.t=?,
~initialModuleSystem: string=?,
~initialLang: Lang.t=?,
Expand Down