直接上代码

//  vite 插件
import dotenv from "dotenv";
import { r } from "../scripts/utils";
import replace from "@rollup/plugin-replace";

const envData = dotenv.config({ path: r(".env") }).parsed ?? {};

export function defineEnv(): any {
  return replace({
    ...Object.entries(envData).reduce((prev, current) => {
      prev[`import.meta.dotenv.${current[0]}`] = `"${current[1]}"`;
      return prev;
    }, {}),
    preventAssignment: true,
  });
}