直接上代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
//  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,
  });
}