Skip to content

Commit

Permalink
fix(web): not found env variable (#863)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackHole1 committed Aug 13, 2021
1 parent 76b4ca6 commit 1d5f879
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions web/flat-web/scripts/vite-plugin-dotenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export function dotenv(envDir: string = process.cwd()): Plugin {
name: "flat:dotenv",
enforce: "pre",
config(config, { mode }) {
const targetFile = path.join(envDir, `.env.${mode}.local`);
if (fs.existsSync(targetFile) && fs.statSync(targetFile).isFile()) {
const parsed = dotenvReal.parse(fs.readFileSync(targetFile));
const envConfigContent = getEnvConfigContent(envDir, mode);

if (envConfigContent) {
const parsed = dotenvReal.parse(envConfigContent);
dotenvExpand({ parsed });
const env = { ...parsed };
const define: Record<string, string | {}> = {};
Expand All @@ -24,3 +25,18 @@ export function dotenv(envDir: string = process.cwd()): Plugin {
},
};
}

const getEnvConfigContent = (envDir: string, mode: string): string | null => {
const configFileList = [
path.join(envDir, `.env.${mode}.local`),
path.join(envDir, `.env.${mode}`),
];

for (const filepath of configFileList) {
if (fs.existsSync(filepath) && fs.statSync(filepath).isFile()) {
return fs.readFileSync(filepath, "utf-8");
}
}

return null;
};

0 comments on commit 1d5f879

Please sign in to comment.