From bd52160d9125ce7602e44895a3b52665d06fd527 Mon Sep 17 00:00:00 2001 From: dahyeon Date: Sun, 7 Jun 2026 09:01:02 -0700 Subject: [PATCH] fix(web): resolve recharts vite interop --- apps/web/vite.config.ts | 61 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/apps/web/vite.config.ts b/apps/web/vite.config.ts index 828a1bc1..93f8fe34 100644 --- a/apps/web/vite.config.ts +++ b/apps/web/vite.config.ts @@ -1,10 +1,69 @@ import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; import tailwindcss from "@tailwindcss/vite"; +import { createRequire } from "node:module"; +import fs from "node:fs"; +import path from "node:path"; + +const ES_TOOLKIT_COMPAT_RE = /^es-toolkit\/compat\/[A-Za-z0-9_$]+$/; +const VIRTUAL_ES_TOOLKIT_COMPAT_PREFIX = "\0es-toolkit-compat-esm:"; + +function esToolkitCompatEsm() { + return { + name: "es-toolkit-compat-esm", + enforce: "pre" as const, + resolveId(source: string, importer?: string) { + if (!importer || !ES_TOOLKIT_COMPAT_RE.test(source)) { + return null; + } + + const requireFromImporter = createRequire(importer); + const wrapperPath = requireFromImporter.resolve(source); + const wrapperSource = fs.readFileSync(wrapperPath, "utf8"); + const wrapperMatch = wrapperSource.match( + /require\(['"](\.\.\/dist\/compat\/[^'"]+)\.js['"]\)\.([A-Za-z0-9_$]+)/, + ); + + if (!wrapperMatch) { + return null; + } + + const [, esmPathWithoutExtension, exportName] = wrapperMatch; + const esmPath = path.resolve( + path.dirname(wrapperPath), + `${esmPathWithoutExtension}.mjs`, + ); + + return `${VIRTUAL_ES_TOOLKIT_COMPAT_PREFIX}${encodeURIComponent( + exportName, + )}:${encodeURIComponent(esmPath)}`; + }, + load(id: string) { + if (!id.startsWith(VIRTUAL_ES_TOOLKIT_COMPAT_PREFIX)) { + return null; + } + + const [encodedExportName, encodedEsmPath] = id + .slice(VIRTUAL_ES_TOOLKIT_COMPAT_PREFIX.length) + .split(":"); + const exportName = decodeURIComponent(encodedExportName); + const esmPath = decodeURIComponent(encodedEsmPath); + + return `export { ${exportName} as default, ${exportName} } from ${JSON.stringify( + esmPath, + )};`; + }, + }; +} // https://vite.dev/config/ export default defineConfig({ - plugins: [react(), tailwindcss()], + plugins: [esToolkitCompatEsm(), react(), tailwindcss()], + optimizeDeps: { + rolldownOptions: { + plugins: [esToolkitCompatEsm()], + }, + }, server: { host: "0.0.0.0", allowedHosts: ["126.dahyeon.us"],