fix(web): resolve recharts vite interop
This commit is contained in:
1 parent
c3a3eb46dd
commit
bd52160d91
1 file changed
+60
-1
+60
-1
@@ -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"],
|
||||
|
||||
Reference in new issue
Block a user