Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bd52160d91 |
+60
-1
@@ -1,10 +1,69 @@
|
|||||||
import { defineConfig } from "vite";
|
import { defineConfig } from "vite";
|
||||||
import react from "@vitejs/plugin-react";
|
import react from "@vitejs/plugin-react";
|
||||||
import tailwindcss from "@tailwindcss/vite";
|
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/
|
// https://vite.dev/config/
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [react(), tailwindcss()],
|
plugins: [esToolkitCompatEsm(), react(), tailwindcss()],
|
||||||
|
optimizeDeps: {
|
||||||
|
rolldownOptions: {
|
||||||
|
plugins: [esToolkitCompatEsm()],
|
||||||
|
},
|
||||||
|
},
|
||||||
server: {
|
server: {
|
||||||
host: "0.0.0.0",
|
host: "0.0.0.0",
|
||||||
allowedHosts: ["126.dahyeon.us"],
|
allowedHosts: ["126.dahyeon.us"],
|
||||||
|
|||||||
Reference in new issue
Block a user