Skip to content

Commit

Permalink
fix(remix-dev/vite): default NODE_ENV in build (#8405)
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish authored Jan 2, 2024
1 parent b2e98b6 commit 81c7a99
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/fair-bobcats-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Vite: Default `NODE_ENV` to `"production"` when running `remix vite:build` command
79 changes: 79 additions & 0 deletions integration/vite-node-env-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { test, expect } from "@playwright/test";
import getPort from "get-port";

import {
createProject,
viteDev,
viteBuild,
viteRemixServe,
VITE_CONFIG,
} from "./helpers/vite.js";

let files = {
"app/routes/node_env.tsx": String.raw`
export default function NodeEnvRoute() {
return <div data-node-env>{process.env.NODE_ENV}</div>
}
`,
};

test.describe(async () => {
let devPort: number;
let cwd: string;
let stop: () => Promise<void> | void;

test.beforeAll(async () => {
devPort = await getPort();
cwd = await createProject({
"vite.config.js": await VITE_CONFIG({ port: devPort }),
...files,
});
});

test.describe(() => {
test.beforeAll(async () => {
stop = await viteDev({ cwd, port: devPort });
});
test.afterAll(async () => await stop());

test("Vite / NODE_ENV / dev", async ({ page }) => {
let pageErrors: unknown[] = [];
page.on("pageerror", (error) => pageErrors.push(error));

await page.goto(`http://localhost:${devPort}/node_env`, {
waitUntil: "networkidle",
});
expect(pageErrors).toEqual([]);

let nodeEnvContent = page.locator("[data-node-env]");
await expect(nodeEnvContent).toHaveText("development");

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

test.describe(() => {
let buildPort: number;
test.beforeAll(async () => {
await viteBuild({ cwd });
buildPort = await getPort();
stop = await viteRemixServe({ cwd, port: buildPort });
});
test.afterAll(async () => await stop());

test("Vite / NODE_ENV / build", async ({ page }) => {
let pageErrors: unknown[] = [];
page.on("pageerror", (error) => pageErrors.push(error));

await page.goto(`http://localhost:${buildPort}/node_env`, {
waitUntil: "networkidle",
});
expect(pageErrors).toEqual([]);

let nodeEnvContent = page.locator("[data-node-env]");
await expect(nodeEnvContent).toHaveText("production");

expect(pageErrors).toEqual([]);
});
});
});
4 changes: 3 additions & 1 deletion packages/remix-dev/vite/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ async function extractConfig({
// process so we don't need to have a separate Remix config
let viteConfig = await vite.resolveConfig(
{ mode, configFile, root },
"build"
"build", // command
"production", // default mode
"production" // default NODE_ENV
);

let pluginConfig = viteConfig[
Expand Down

0 comments on commit 81c7a99

Please sign in to comment.