Skip to content

Commit

Permalink
fix(vite): load .env files into process.env in dev
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish committed Nov 10, 2023
1 parent d437e79 commit c6cfa88
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 171 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-news-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Populate `process.env` from `.env` files on the server in Vite dev
35 changes: 35 additions & 0 deletions integration/vite-build-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ test.describe("Vite build", () => {
throw new Error("Remix should not access remix.config.js when using Vite");
export default {};
`,
".env": `
ENV_VAR_FROM_DOTENV_FILE=true
`,
"vite.config.ts": js`
import { defineConfig } from "vite";
import { unstable_vitePlugin as remix } from "@remix-run/dev";
Expand Down Expand Up @@ -164,6 +167,22 @@ test.describe("Vite build", () => {
background-color: rgb(255, 170, 0);
}
`,
"app/routes/dotenv.tsx": js`
import { json } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react";
export const loader = () => {
return json({
loaderContent: process.env.ENV_VAR_FROM_DOTENV_FILE ?? '.env file was NOT loaded, which is a good thing',
})
}
export default function DotenvRoute() {
const { loaderContent } = useLoaderData();
return <div data-dotenv-route-loader-content>{loaderContent}</div>;
}
`,
},
});

Expand Down Expand Up @@ -254,4 +273,20 @@ test.describe("Vite build", () => {

expect(pageErrors).toEqual([]);
});

test("doesn't load .env file", async ({ page }) => {
let pageErrors: unknown[] = [];
page.on("pageerror", (error) => pageErrors.push(error));

let app = new PlaywrightFixture(appFixture, page);
await app.goto(`/dotenv`);
expect(pageErrors).toEqual([]);

let loaderContent = page.locator("[data-dotenv-route-loader-content]");
await expect(loaderContent).toHaveText(
".env file was NOT loaded, which is a good thing"
);

expect(pageErrors).toEqual([]);
});
});
Loading

0 comments on commit c6cfa88

Please sign in to comment.