From 670186f947377b0a2211ce646563ce206f490e51 Mon Sep 17 00:00:00 2001 From: Florian Hammerschmidt Date: Thu, 4 Sep 2025 15:34:44 +0200 Subject: [PATCH] Revert "make playground bundle endpoint customizable (#1099)" This reverts commit b7e5ae103f380e18303ab017100d75281f9bb1fc. --- src/Playground.res | 23 +++++++---------- src/Playground.resi | 2 +- src/Try.res | 39 +++-------------------------- src/Try.resi | 5 +--- src/bindings/Node.res | 2 -- src/common/CompilerManagerHook.res | 36 ++++++++++++-------------- src/common/CompilerManagerHook.resi | 3 +-- 7 files changed, 32 insertions(+), 78 deletions(-) diff --git a/src/Playground.res b/src/Playground.res index b11f75250..f20a1f633 100644 --- a/src/Playground.res +++ b/src/Playground.res @@ -1419,7 +1419,7 @@ module App = { let initialReContent = `Js.log("Hello Reason 3.6!");` @react.component -let make = (~bundleBaseUrl: string, ~versions: array) => { +let make = (~versions: array) => { let router = Next.Router.useRouter() let versions = @@ -1436,18 +1436,14 @@ let make = (~bundleBaseUrl: string, ~versions: array) => { 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 } } @@ -1474,7 +1470,6 @@ let make = (~bundleBaseUrl: string, ~versions: array) => { let (actionCount, setActionCount) = React.useState(_ => 0) let onAction = _ => setActionCount(prev => prev > 1000000 ? 0 : prev + 1) let (compilerState, compilerDispatch) = useCompilerManager( - ~bundleBaseUrl, ~initialVersion?, ~initialModuleSystem?, ~initialLang, diff --git a/src/Playground.resi b/src/Playground.resi index 0c9fe47b4..33288c00c 100644 --- a/src/Playground.resi +++ b/src/Playground.resi @@ -1,2 +1,2 @@ @react.component -let make: (~bundleBaseUrl: string, ~versions: array) => React.element +let make: (~versions: array) => React.element diff --git a/src/Try.res b/src/Try.res index 88ab78268..374b029d6 100644 --- a/src/Try.res +++ b/src/Try.res @@ -1,7 +1,4 @@ -type props = { - bundleBaseUrl: string, - versions: array, -} +type props = {versions: array} let default = props => { let (isOverlayOpen, setOverlayOpen) = React.useState(() => false) @@ -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}) <> { } let getStaticProps: Next.GetStaticProps.t = 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 @@ -75,10 +49,5 @@ let getStaticProps: Next.GetStaticProps.t = async _ => { ->Array.map(json => json->JSON.Decode.string->Option.getOrThrow) } - { - "props": { - bundleBaseUrl, - versions, - }, - } + {"props": {versions: versions}} } diff --git a/src/Try.resi b/src/Try.resi index 9032885ea..5fec66502 100644 --- a/src/Try.resi +++ b/src/Try.resi @@ -1,6 +1,3 @@ -type props = { - bundleBaseUrl: string, - versions: array, -} +type props = {versions: array} let default: props => React.element let getStaticProps: Next.GetStaticProps.t diff --git a/src/bindings/Node.res b/src/bindings/Node.res index 1f54aa73e..9025de6a7 100644 --- a/src/bindings/Node.res +++ b/src/bindings/Node.res @@ -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 = "PLAYGROUND_BUNDLE_ENDPOINT" } } diff --git a/src/common/CompilerManagerHook.res b/src/common/CompilerManagerHook.res index f3fa40316..6484eb8f1 100644 --- a/src/common/CompilerManagerHook.res +++ b/src/common/CompilerManagerHook.res @@ -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}` } @@ -100,11 +104,11 @@ let getOpenModules = (~apiVersion: Version.t, ~libraries: array): 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, ()): result< +let attachCompilerAndLibraries = async (~version, ~libraries: array, ()): result< unit, array, > => { - let compilerUrl = CdnMeta.getCompilerUrl(baseUrl, version) + let compilerUrl = CdnMeta.getCompilerUrl(version) // Useful for debugging our local build /* let compilerUrl = "/static/linked-bs-bundle.js"; */ @@ -113,7 +117,7 @@ let attachCompilerAndLibraries = async (~baseUrl, ~version, ~libraries: array 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 @@ -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=?, ~initialModuleSystem=defaultModuleSystem, ~initialLang: Lang.t=Res, @@ -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 @@ -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() @@ -580,7 +576,7 @@ let useCompilerManager = ( } | version => version } - CdnMeta.getStdlibRuntimeUrl(bundleBaseUrl, compilerVersion, filename) + CdnMeta.getStdlibRuntimeUrl(compilerVersion, filename) }) entryPointExists diff --git a/src/common/CompilerManagerHook.resi b/src/common/CompilerManagerHook.resi index bddb684dd..113a5c7b7 100644 --- a/src/common/CompilerManagerHook.resi +++ b/src/common/CompilerManagerHook.resi @@ -31,7 +31,7 @@ type ready = { } module CdnMeta: { - let getStdlibRuntimeUrl: (string, Semver.t, string) => string + let getStdlibRuntimeUrl: (Semver.t, string) => string } type state = @@ -53,7 +53,6 @@ type action = | RunCode let useCompilerManager: ( - ~bundleBaseUrl: string, ~initialVersion: Semver.t=?, ~initialModuleSystem: string=?, ~initialLang: Lang.t=?,