From 228451134da741b856762da9f4747bf8d4fd8679 Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Thu, 21 Nov 2024 16:49:52 +0900 Subject: [PATCH] Fix playground runtime version of dependencies --- website/next-env.d.ts | 2 +- website/src/compilers/RollupBundler.ts | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/website/next-env.d.ts b/website/next-env.d.ts index 4f11a03dc6..a4a7b3f5cf 100644 --- a/website/next-env.d.ts +++ b/website/next-env.d.ts @@ -2,4 +2,4 @@ /// // NOTE: This file should not be edited -// see https://nextjs.org/docs/basic-features/typescript for more information. +// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information. diff --git a/website/src/compilers/RollupBundler.ts b/website/src/compilers/RollupBundler.ts index b60ab45df8..d8d46ae0dc 100644 --- a/website/src/compilers/RollupBundler.ts +++ b/website/src/compilers/RollupBundler.ts @@ -1,5 +1,6 @@ import { RollupBuild, rollup } from "@rollup/browser"; import { VariadicSingleton } from "tstl"; +import lock from "../../package-lock.json"; export namespace RollupBundler { export const build = async (script: string): Promise => { @@ -33,7 +34,26 @@ export namespace RollupBundler { } const esm = new VariadicSingleton(async (url: string) => { - const response: Response = await fetch(url); + const response: Response = await fetch(reformUrl(url)); const text: string = await response.text(); return text; }); + +const reformUrl = (url: string): string => { + const elements: string[] = url.split("https://esm.sh/")[1].split("/"); + if (elements.length === 0) return url; + else if (elements[0].indexOf("@") > 0) return url; + + const library: string = elements[0].startsWith("@") + ? `${elements[0]}/${elements[1]}` + : elements[0]; + const version: string | undefined = + lock.packages[`node_modules/${library}` as "node_modules/typia"]?.version; + if (version === undefined) return url; + + const path: string = [ + `${library}@${version}`, + ...elements.slice(library.startsWith("@") ? 2 : 1).join("/"), + ].join("/"); + return `https://esm.sh/${path}`; +};