Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite): load .env files into process.env in dev #7958

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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