From 7baf17b0ee9fdef54a177cfbd70b7df9b9eca60c Mon Sep 17 00:00:00 2001 From: Okiki Ojo Date: Sat, 29 Jul 2023 20:18:06 +0000 Subject: [PATCH] chore: dynamic imports aren't supported on deno deploy --- deps.ts | 5 ++-- mod.ts | 8 +++++- vendor/version.ts | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 vendor/version.ts diff --git a/deps.ts b/deps.ts index 5e66c8f..05cfb9a 100644 --- a/deps.ts +++ b/deps.ts @@ -1,4 +1,5 @@ -export { default as Typescript } from "https://esm.sh/typescript@5" +// @deno-types=https://unpkg.com/typescript@latest/lib/typescript.d.ts +export { default as Typescript } from "https://esm.sh/typescript@latest" export * as lzstring from "https://esm.sh/@amoutonbrady/lz-string@0.1.0" export * as path from "https://deno.land/std/path/mod.ts" @@ -8,7 +9,7 @@ export { oakCors as cors } from "https://deno.land/x/cors/mod.ts" export { parse } from "https://deno.land/std/yaml/parse.ts" -declare module "https://esm.sh/typescript@5" { +declare module "https://esm.sh/typescript@latest" { // Hacking in some internal stuff type TypescriptOption = { name: string; diff --git a/mod.ts b/mod.ts index cc5746f..7ef7a6e 100644 --- a/mod.ts +++ b/mod.ts @@ -1,5 +1,5 @@ // Import necessary modules from dependencies -import { cors, oak, path, parse } from "./deps.ts"; +import { cors, oak, path, parse, type Typescript } from "./deps.ts"; import { twoslasher } from "./vendor/twoslash.ts"; import type { TwoSlashOptions } from "./vendor/twoslash.ts"; @@ -17,6 +17,7 @@ const __dirname = dirname(fromFileUrl(import.meta.url)); interface TwoslashRequestOptions extends TwoSlashOptions { code: string; extension: string; + // version?: string; } // Create a new router (similar to Express.js Router in Node.js) @@ -77,6 +78,11 @@ router const data: TwoslashRequestOptions = type === "form-data" ? (await value.read({ maxSize: 10000000 })).fields : await value; const { code, extension, ...opts } = data; + // let tsModule: typeof Typescript; + // if (version) { + // tsModule = await import(`https://esm.sh/typescript@${versionNumber}`).default; + // } + const twoslash = await twoslasher(code, extension, opts); context.response.body = twoslash; }) diff --git a/vendor/version.ts b/vendor/version.ts new file mode 100644 index 0000000..a5637bd --- /dev/null +++ b/vendor/version.ts @@ -0,0 +1,68 @@ +export async function getTypescriptVersions() { + const response = await fetch( + "https://typescript.azureedge.net/indexes/releases.json", + ); + const releases: { versions: string[] } = await response.json(); + const versions = releases.versions.reverse(); + + // Look through the prereleases to see if the beta and RC are included in the pre-releases + // and add those to the list of versions. + const preReleaseResponse = await fetch( + "https://typescript.azureedge.net/indexes/pre-releases.json", + ); + const preReleases = await preReleaseResponse.json(); + const latestStable = versions[0]; + + // e.g. 4.3.1 -> 4.4.0-beta + // this won't work for 5.0 specifically, but that's an ok edge case for me + const possibleBeta = `${latestStable.split(".")[0]}.${ + Number(latestStable.split(".")[1]) + 1 + }.0-beta`; + const addBeta = preReleases.versions.includes(possibleBeta); + + const possibleRc = `${latestStable.split(".")[0]}.${ + Number(latestStable.split(".")[1]) + 1 + }.1-rc`; + const addRc = preReleases.versions.includes(possibleRc); + + // Get the highest maj/min ignoring patch versions + const latestMajMin = new Map(); + versions.forEach((v) => { + const majMin = v.split(".")[0] + "." + v.split(".")[1]; + if (!latestMajMin.has(majMin)) { + latestMajMin.set(majMin, v); + } + }); + + // prettier-ignore + // Adds RC and Beta to the versions automatically + const supportedVersions = [ + addRc ? possibleRc : "", + addBeta ? possibleBeta : "", + ...latestMajMin.values(), + ].filter(Boolean); + + /** + * The versions of monaco-typescript which we can use + * for backwards compatibility with older versions + * of TS in the playground. + */ + const supportedReleases = [...supportedVersions, "Latest"]; + + /** Returns the latest TypeScript version supported by the sandbox */ + const latestSupportedTypeScriptVersion: string = supportedReleases + .filter((key) => key !== "Nightly" && !key.includes("-")) + .sort() + .pop()!; + + return { + /** Every prod version **/ + versions, + /** The latest major.min version **/ + supportedReleases, + releases, + possibleBeta, + possibleRc, + latestSupportedTypeScriptVersion, + }; +} -- 2.51.2