Skip to content

Commit aec77cc

Browse files
authored
Pass the compatibility_date and compatibility_flags to the unenv preset (#10482)
* Move nodejs_compat logic to class and instantiate per nodejs_compat enabled environment * Add test for https module * Add changeset * More JSDoc * Reuse helper function
1 parent 8f4b3f7 commit aec77cc

File tree

12 files changed

+508
-373
lines changed

12 files changed

+508
-373
lines changed

.changeset/shaky-melons-help.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Pass the `compatibility_date` and `compatibility_flags` to the `unenv` preset. This enables support for the `node:http` and `node:https` modules.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { expect, test } from "vitest";
2+
import { getTextResponse } from "../../../__test-utils__";
3+
4+
test("supports `node:https` module", async () => {
5+
expect(await getTextResponse()).toBe("OK");
6+
});

packages/vite-plugin-cloudflare/playground/node-compat/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"debug:dev": "vite dev -c vite.config.worker-debug.ts",
2323
"debug:preview": "vite preview -c vite.config.worker-debug.ts",
2424
"debug:test": "vitest run -c ../vitest.config.e2e.ts worker-debug",
25+
"https:build": "vite build --app -c vite.config.worker-https.ts",
26+
"https:dev": "vite dev -c vite.config.worker-https.ts",
27+
"https:preview": "vite preview -c vite.config.worker-https.ts",
2528
"postgres:build": "vite build --app -c vite.config.worker-postgres.ts",
2629
"postgres:dev": "vite dev -c vite.config.worker-postgres.ts",
2730
"postgres:preview": "vite preview -c vite.config.worker-postgres.ts",

packages/vite-plugin-cloudflare/playground/node-compat/tsconfig.worker.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"worker-cross-env",
77
"worker-crypto",
88
"worker-debug",
9+
"worker-https",
910
"worker-postgres",
1011
"worker-process",
1112
"worker-random",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { cloudflare } from "@cloudflare/vite-plugin";
2+
import { defineConfig } from "vite";
3+
4+
export default defineConfig({
5+
build: {
6+
outDir: "dist/worker-https",
7+
},
8+
plugins: [
9+
cloudflare({
10+
configPath: "./worker-https/wrangler.jsonc",
11+
inspectorPort: false,
12+
persistState: false,
13+
}),
14+
],
15+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as https from "node:https";
2+
3+
export default {
4+
async fetch() {
5+
// This will throw a 'not implemented' error if the compatibility_date is earlier than '2025-08-15' and the 'enable_nodejs_http_modules' compatibility_flag is not present
6+
const req = https.request({});
7+
req.end();
8+
9+
return new Response("OK");
10+
},
11+
} satisfies ExportedHandler;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "worker",
3+
"main": "./index.ts",
4+
// This is the minimum compatibility_date that has http_modules enabled by default
5+
"compatibility_date": "2025-08-15",
6+
"compatibility_flags": ["nodejs_compat"],
7+
}

packages/vite-plugin-cloudflare/src/cloudflare-environment.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import assert from "node:assert";
22
import * as util from "node:util";
33
import * as vite from "vite";
4-
import { isNodeCompat } from "./node-js-compat";
54
import { INIT_PATH, UNKNOWN_HOST, WORKER_ENTRY_PATH_HEADER } from "./shared";
65
import { getOutputDirectory } from "./utils";
76
import type { WorkerConfig, WorkersResolvedConfig } from "./plugin-config";
@@ -130,14 +129,16 @@ export function createCloudflareEnvironmentOptions({
130129
mode,
131130
environmentName,
132131
isEntryWorker,
132+
hasNodeJsCompat,
133133
}: {
134134
workerConfig: WorkerConfig;
135135
userConfig: vite.UserConfig;
136136
mode: vite.ConfigEnv["mode"];
137137
environmentName: string;
138138
isEntryWorker: boolean;
139+
hasNodeJsCompat: boolean;
139140
}): vite.EnvironmentOptions {
140-
const define = getProcessEnvReplacements(workerConfig, mode);
141+
const define = getProcessEnvReplacements(hasNodeJsCompat, mode);
141142

142143
return {
143144
resolve: {
@@ -205,12 +206,12 @@ export function createCloudflareEnvironmentOptions({
205206
* Gets `process.env` replacement values.
206207
* `process.env.NODE_ENV` is always replaced.
207208
* `process.env` is replaced with an empty object if `nodejs_compat` is not enabled
208-
* @param workerConfig - the Worker config (used to determine if `nodejs_compat` is enabled)
209+
* @param hasNodeJsCompat - whether `nodejs_compat` is enabled
209210
* @param mode - the Vite mode
210211
* @returns replacement values
211212
*/
212213
function getProcessEnvReplacements(
213-
workerConfig: WorkerConfig,
214+
hasNodeJsCompat: boolean,
214215
mode: vite.ConfigEnv["mode"]
215216
): Record<string, string> {
216217
const nodeEnv = process.env.NODE_ENV || mode;
@@ -220,7 +221,7 @@ function getProcessEnvReplacements(
220221
"globalThis.process.env.NODE_ENV": JSON.stringify(nodeEnv),
221222
};
222223

223-
return isNodeCompat(workerConfig)
224+
return hasNodeJsCompat
224225
? nodeEnvReplacements
225226
: {
226227
...nodeEnvReplacements,

0 commit comments

Comments
 (0)