chore: import upstream snapshot with attribution
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "@fixture/node-env",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"check:type": "tsc",
|
||||
"dev": "wrangler dev",
|
||||
"test:ci": "vitest run",
|
||||
"test:watch": "vitest"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "^18.3.1",
|
||||
"react-dom": "^18.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@cloudflare/workers-tsconfig": "workspace:*",
|
||||
"@cloudflare/workers-types": "catalog:default",
|
||||
"@types/node": "catalog:default",
|
||||
"@types/react": "^18.3.3",
|
||||
"@types/react-dom": "^18.2.0",
|
||||
"miniflare": "workspace:*",
|
||||
"typescript": "catalog:default",
|
||||
"vitest": "catalog:default",
|
||||
"wrangler": "workspace:*"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import React from "react";
|
||||
import { renderToString } from "react-dom/server";
|
||||
|
||||
export default {
|
||||
async fetch(request) {
|
||||
const url = new URL(request.url);
|
||||
|
||||
if (url.pathname === "/ssr") {
|
||||
const content = renderToString(
|
||||
React.createElement("h1", null, "Hello world")
|
||||
);
|
||||
|
||||
return new Response(content);
|
||||
}
|
||||
|
||||
return new Response(
|
||||
`The value of process.env.NODE_ENV is "${process.env.NODE_ENV}"`
|
||||
);
|
||||
},
|
||||
} satisfies ExportedHandler;
|
||||
@@ -0,0 +1,137 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import * as fs from "node:fs";
|
||||
import * as path from "node:path";
|
||||
import { Miniflare } from "miniflare";
|
||||
import { describe, it, vi } from "vitest";
|
||||
import { runWranglerDev } from "../../shared/src/run-wrangler-long-lived";
|
||||
|
||||
describe("`process.env.NODE_ENV` replacement in development", () => {
|
||||
it("replaces `process.env.NODE_ENV` with `development` if it is `undefined`", async ({
|
||||
expect,
|
||||
}) => {
|
||||
vi.stubEnv("NODE_ENV", undefined);
|
||||
|
||||
const { ip, port, stop } = await runWranglerDev(
|
||||
path.resolve(__dirname, ".."),
|
||||
["--port=0", "--inspector-port=0"]
|
||||
);
|
||||
|
||||
await vi.waitFor(async () => {
|
||||
const response = await fetch(`http://${ip}:${port}/`);
|
||||
const text = await response.text();
|
||||
expect(text).toBe(`The value of process.env.NODE_ENV is "development"`);
|
||||
});
|
||||
|
||||
await stop();
|
||||
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
it("replaces `process.env.NODE_ENV` with the given value if it is set", async ({
|
||||
expect,
|
||||
}) => {
|
||||
vi.stubEnv("NODE_ENV", "some-value");
|
||||
|
||||
const { ip, port, stop } = await runWranglerDev(
|
||||
path.resolve(__dirname, ".."),
|
||||
["--port=0", "--inspector-port=0"]
|
||||
);
|
||||
|
||||
await vi.waitFor(async () => {
|
||||
const response = await fetch(`http://${ip}:${port}/`);
|
||||
const text = await response.text();
|
||||
expect(text).toBe(`The value of process.env.NODE_ENV is "some-value"`);
|
||||
});
|
||||
|
||||
await stop();
|
||||
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
});
|
||||
|
||||
describe("`process.env.NODE_ENV` replacement in production", () => {
|
||||
const url = "http://localhost";
|
||||
|
||||
it("replaces `process.env.NODE_ENV` with `production` if it is `undefined`", async ({
|
||||
expect,
|
||||
}) => {
|
||||
vi.stubEnv("NODE_ENV", undefined);
|
||||
|
||||
spawnSync("npx wrangler build", {
|
||||
shell: true,
|
||||
stdio: "pipe",
|
||||
});
|
||||
|
||||
const miniflare = new Miniflare({
|
||||
modules: [
|
||||
{
|
||||
type: "ESModule",
|
||||
path: "./dist/index.js",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await miniflare.ready;
|
||||
|
||||
await vi.waitFor(async () => {
|
||||
const response = await miniflare.dispatchFetch(url);
|
||||
const text = await response.text();
|
||||
expect(text).toBe(`The value of process.env.NODE_ENV is "production"`);
|
||||
});
|
||||
|
||||
await miniflare.dispose();
|
||||
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
it("replaces `process.env.NODE_ENV` with the given value if it is set", async ({
|
||||
expect,
|
||||
}) => {
|
||||
vi.stubEnv("NODE_ENV", "some-value");
|
||||
|
||||
spawnSync("npx wrangler build", {
|
||||
shell: true,
|
||||
stdio: "pipe",
|
||||
});
|
||||
|
||||
const miniflare = new Miniflare({
|
||||
modules: [
|
||||
{
|
||||
type: "ESModule",
|
||||
path: "./dist/index.js",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
await miniflare.ready;
|
||||
|
||||
await vi.waitFor(async () => {
|
||||
const response = await miniflare.dispatchFetch(url);
|
||||
const text = await response.text();
|
||||
expect(text).toBe(`The value of process.env.NODE_ENV is "some-value"`);
|
||||
});
|
||||
|
||||
await miniflare.dispose();
|
||||
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
it("tree shakes React when `process.env.NODE_ENV` is `production`", ({
|
||||
expect,
|
||||
}) => {
|
||||
vi.stubEnv("NODE_ENV", undefined);
|
||||
|
||||
spawnSync("npx wrangler build", {
|
||||
shell: true,
|
||||
stdio: "pipe",
|
||||
});
|
||||
|
||||
const outputJs = fs.readFileSync("./dist/index.js", "utf8");
|
||||
|
||||
expect(outputJs).not.toContain("react-dom.development.js");
|
||||
// the React development code links to the facebook/react repo
|
||||
expect(outputJs).not.toContain("https://github.com/facebook/react");
|
||||
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "@cloudflare/workers-tsconfig/tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"types": ["@cloudflare/workers-types/experimental", "node"]
|
||||
},
|
||||
"include": ["src", "tests"]
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { defineProject, mergeConfig } from "vitest/config";
|
||||
import configShared from "../../vitest.shared";
|
||||
|
||||
export default mergeConfig(
|
||||
configShared,
|
||||
defineProject({
|
||||
test: {},
|
||||
})
|
||||
);
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "node-env",
|
||||
"compatibility_date": "2025-10-01",
|
||||
"main": "./src/index.ts",
|
||||
}
|
||||
Reference in New Issue
Block a user