Skip to content

Commit f566680

Browse files
[vite-plugin] fix: replace process.env.NODE_ENV for nodejs_compat builds (cloudflare#8823)
1 parent 22b8a80 commit f566680

File tree

17 files changed

+239
-6
lines changed

17 files changed

+239
-6
lines changed

.changeset/little-cities-yell.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
fix: replace `process.env.NODE_ENV` for nodejs_compat builds
6+
7+
make sure that occurrences of `process.env.NODE_ENV` are replaced with the
8+
current `process.env.NODE_ENV` value or `"production"` on builds that include
9+
the `nodejs_compat` flag, this enables libraries checking such value
10+
(e.g. `react-dom`) to be properly treeshaken

packages/vite-plugin-cloudflare/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"dependencies": {
4545
"@cloudflare/unenv-preset": "workspace:*",
4646
"@hattip/adapter-node": "^0.0.49",
47+
"@rollup/plugin-replace": "^6.0.1",
4748
"get-port": "^7.1.0",
4849
"miniflare": "workspace:*",
4950
"picocolors": "^1.1.1",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { readFileSync } from "node:fs";
2+
import { expect, test } from "vitest";
3+
import { isBuild, page } from "../../__test-utils__";
4+
5+
export function testPageContent() {
6+
test("the page content has been server side rendered by react", async () => {
7+
const h1Content = await page.textContent("h1");
8+
expect(h1Content).toEqual("Server Side React");
9+
const pContent = await page.textContent("p");
10+
expect(pContent).toEqual(
11+
"This page was server side rendered by using directly 'renderToString' from 'react-dom/server'"
12+
);
13+
});
14+
}
15+
16+
test.skipIf(!isBuild)(
17+
"the build output does not include react development code (such is tree-shaken away)",
18+
async () => {
19+
const outputJs = readFileSync("./dist/worker/index.js", "utf8");
20+
expect(outputJs).not.toContain("react-dom.development.js");
21+
// the react development code links to the facebook/react repo in a few places
22+
expect(outputJs).not.toContain("https://github.com/facebook/react");
23+
}
24+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./base-tests";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "../base-tests";
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@playground/react-dom-server",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"build": "vite build --app",
7+
"check:types": "tsc --build",
8+
"dev": "vite dev",
9+
"preview": "vite preview"
10+
},
11+
"dependencies": {
12+
"react": "^19.1.0",
13+
"react-dom": "^19.1.0"
14+
},
15+
"devDependencies": {
16+
"@cloudflare/vite-plugin": "workspace:*",
17+
"@cloudflare/workers-tsconfig": "workspace:*",
18+
"@cloudflare/workers-types": "^4.20250405.0",
19+
"@types/react": "^19.1.0",
20+
"@types/react-dom": "^19.1.1",
21+
"typescript": "catalog:default",
22+
"vite": "catalog:vite-plugin",
23+
"wrangler": "workspace:*"
24+
}
25+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import React from "react";
2+
import { renderToString } from "react-dom/server";
3+
4+
export default {
5+
async fetch() {
6+
const txt = renderToString(
7+
React.createElement("div", null, [
8+
React.createElement("h1", null, `Server Side React`),
9+
React.createElement(
10+
"p",
11+
null,
12+
"This page was server side rendered by using directly 'renderToString' from 'react-dom/server'"
13+
),
14+
])
15+
);
16+
17+
return new Response(
18+
`<!DOCTYPE html>
19+
<html>
20+
<head>
21+
<title>React Server Render</title>
22+
</head>
23+
<body>
24+
<div id="app">${txt}</div>
25+
</body>
26+
</html>`,
27+
{
28+
headers: { "Content-Type": "text/html" },
29+
}
30+
);
31+
},
32+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"files": [],
3+
"references": [
4+
{ "path": "./tsconfig.node.json" },
5+
{ "path": "./tsconfig.worker.json" }
6+
]
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": ["@cloudflare/workers-tsconfig/base.json"],
3+
"include": ["vite.config.ts", "__tests__"]
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": ["@cloudflare/workers-tsconfig/worker.json"],
3+
"include": ["src"]
4+
}

0 commit comments

Comments
 (0)