Skip to content

Commit dbdbb8c

Browse files
vicbedmundhung
andauthored
pass the compatibility date and flags to the unenv preset (#10048)
Co-authored-by: Edmund Hung <[email protected]>
1 parent f8a80a8 commit dbdbb8c

File tree

23 files changed

+160
-83
lines changed

23 files changed

+160
-83
lines changed

.changeset/gentle-mice-create.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
"@cloudflare/unenv-preset": minor
4+
"wrangler": patch
5+
---
6+
7+
pass the compatibility date and flags to the unenv preset

.prettierignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,4 @@ fixtures/pages-redirected-config/build/*
4848
fixtures/redirected-config-worker/build/*
4949
fixtures/redirected-config-worker-with-environments/build/*
5050

51-
# Import attributes generate an error
52-
packages/unenv-preset/src/preset.ts
53-
5451
packages/vite-plugin-cloudflare/playground/**/*.d.ts

packages/unenv-preset/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
],
4040
"scripts": {
4141
"build": "unbuild",
42-
"check:lint": "eslint",
42+
"check:lint": "eslint . --max-warnings=0",
4343
"check:type": "tsc --noEmit"
4444
},
4545
"devDependencies": {

packages/unenv-preset/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
export { cloudflare } from "./preset";
1+
import { getCloudflarePreset } from "./preset";
2+
3+
export { getCloudflarePreset } from "./preset";
4+
5+
export const cloudflare = getCloudflarePreset({});

packages/unenv-preset/src/preset.ts

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { version } from "../package.json" with { type: "json" };
1+
import { version } from "../package.json";
22
import type { Preset } from "unenv";
33

44
// Built-in APIs provided by workerd.
@@ -52,46 +52,63 @@ const hybridNodeCompatModules = [
5252
"util",
5353
];
5454

55-
export const cloudflare: Preset = {
56-
meta: {
57-
name: "unenv:cloudflare",
58-
version,
59-
url: __filename,
60-
},
61-
alias: {
62-
// `nodeCompatModules` are implemented in workerd.
63-
// Create aliases to override polyfills defined in based environments.
64-
...Object.fromEntries(
65-
nodeCompatModules.flatMap((p) => [
66-
[p, p],
67-
[`node:${p}`, `node:${p}`],
68-
])
69-
),
55+
/**
56+
* Creates the Cloudflare preset for the given compatibility date and compatibility flags
57+
*
58+
* @param compatibilityDate workerd compatibility date
59+
* @param compatibilityFlags workerd compatibility flags
60+
* @returns The cloudflare preset
61+
*/
62+
export function getCloudflarePreset({
63+
// eslint-disable-next-line unused-imports/no-unused-vars
64+
compatibilityDate = "2024-09-03",
65+
// eslint-disable-next-line unused-imports/no-unused-vars
66+
compatibilityFlags = [],
67+
}: {
68+
compatibilityDate?: string;
69+
compatibilityFlags?: string[];
70+
}): Preset {
71+
return {
72+
meta: {
73+
name: "unenv:cloudflare",
74+
version,
75+
url: __filename,
76+
},
77+
alias: {
78+
// `nodeCompatModules` are implemented in workerd.
79+
// Create aliases to override polyfills defined in based environments.
80+
...Object.fromEntries(
81+
nodeCompatModules.flatMap((p) => [
82+
[p, p],
83+
[`node:${p}`, `node:${p}`],
84+
])
85+
),
7086

71-
// The `node:sys` module is just a deprecated alias for `node:util` which we implemented using a hybrid polyfill
72-
sys: "@cloudflare/unenv-preset/node/util",
73-
"node:sys": "@cloudflare/unenv-preset/node/util",
87+
// The `node:sys` module is just a deprecated alias for `node:util` which we implemented using a hybrid polyfill
88+
sys: "@cloudflare/unenv-preset/node/util",
89+
"node:sys": "@cloudflare/unenv-preset/node/util",
7490

75-
// `hybridNodeCompatModules` are implemented by the cloudflare preset.
76-
...Object.fromEntries(
77-
hybridNodeCompatModules.flatMap((m) => [
78-
[m, `@cloudflare/unenv-preset/node/${m}`],
79-
[`node:${m}`, `@cloudflare/unenv-preset/node/${m}`],
80-
])
81-
),
91+
// `hybridNodeCompatModules` are implemented by the cloudflare preset.
92+
...Object.fromEntries(
93+
hybridNodeCompatModules.flatMap((m) => [
94+
[m, `@cloudflare/unenv-preset/node/${m}`],
95+
[`node:${m}`, `@cloudflare/unenv-preset/node/${m}`],
96+
])
97+
),
8298

83-
// To override the npm shim from unenv
84-
debug: "@cloudflare/unenv-preset/npm/debug",
85-
},
86-
inject: {
87-
// Setting symbols implemented by workerd to `false` so that `inject`s defined in base presets are not used.
88-
Buffer: false,
89-
global: false,
90-
clearImmediate: false,
91-
setImmediate: false,
92-
console: "@cloudflare/unenv-preset/node/console",
93-
process: "@cloudflare/unenv-preset/node/process",
94-
},
95-
polyfill: ["@cloudflare/unenv-preset/polyfill/performance"],
96-
external: nodeCompatModules.flatMap((p) => [p, `node:${p}`]),
97-
};
99+
// To override the npm shim from unenv
100+
debug: "@cloudflare/unenv-preset/npm/debug",
101+
},
102+
inject: {
103+
// Setting symbols implemented by workerd to `false` so that `inject`s defined in base presets are not used.
104+
Buffer: false,
105+
global: false,
106+
clearImmediate: false,
107+
setImmediate: false,
108+
console: "@cloudflare/unenv-preset/node/console",
109+
process: "@cloudflare/unenv-preset/node/process",
110+
},
111+
polyfill: ["@cloudflare/unenv-preset/polyfill/performance"],
112+
external: nodeCompatModules.flatMap((p) => [p, `node:${p}`]),
113+
};
114+
}

packages/unenv-preset/src/runtime/node/async_hooks.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export {
1616
triggerAsyncId,
1717
} from "unenv/node/async_hooks";
1818

19-
// @ts-ignore typings are not up to date, but this API exists, see: https://github.com/cloudflare/workerd/pull/2147
2019
const workerdAsyncHooks = process.getBuiltinModule("node:async_hooks");
2120

2221
// TODO: Ideally this list is not hardcoded but instead is generated when the preset is being generated in the `env()` call

packages/unenv-preset/src/runtime/node/console.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export {
2626
// This code relies on the that rollup/esbuild/webpack don't evaluate string concatenation
2727
// so they don't recognize the below as `globalThis.console` which they would try to rewrite
2828
// into unenv/node/console, thus creating a circular dependency, and breaking this polyfill.
29+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2930
const workerdConsole = (globalThis as any)[
3031
"con" + "sole"
3132
] as typeof nodeConsole;

packages/unenv-preset/src/runtime/node/crypto.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const {
1919
checkPrime,
2020
checkPrimeSync,
2121
constants,
22-
// @ts-expect-error
22+
// @ts-expect-error Node types do not match unenv
2323
Cipheriv,
2424
createCipheriv,
2525
createDecipheriv,
@@ -33,7 +33,7 @@ export const {
3333
createSecretKey,
3434
createSign,
3535
createVerify,
36-
// @ts-expect-error
36+
// @ts-expect-error Node types do not match unenv
3737
Decipheriv,
3838
diffieHellman,
3939
DiffieHellman,
@@ -86,7 +86,7 @@ export const {
8686

8787
// See https://github.com/cloudflare/workerd/issues/3751
8888
export const webcrypto = {
89-
// @ts-expect-error
89+
// @ts-expect-error Node types do not match unenv
9090
CryptoKey: unenvCryptoWebcrypto.CryptoKey,
9191
getRandomValues,
9292
randomUUID,

packages/unenv-preset/src/runtime/node/process.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Process as UnenvProcess } from "unenv/node/internal/process/process";
88
// This code relies on the that rollup/esbuild/webpack don't evaluate string concatenation
99
// so they don't recognize the below as `globalThis.process` which they would try to rewrite
1010
// into unenv/node/process, thus creating a circular dependency, and breaking this polyfill.
11+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1112
const globalProcess: NodeJS.Process = (globalThis as any)["pro" + "cess"];
1213

1314
export const getBuiltinModule: NodeJS.Process["getBuiltinModule"] =

0 commit comments

Comments
 (0)