|
1 | | -import { version } from "../package.json" with { type: "json" }; |
| 1 | +import { version } from "../package.json"; |
2 | 2 | import type { Preset } from "unenv"; |
3 | 3 |
|
4 | 4 | // Built-in APIs provided by workerd. |
@@ -52,46 +52,63 @@ const hybridNodeCompatModules = [ |
52 | 52 | "util", |
53 | 53 | ]; |
54 | 54 |
|
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 | + ), |
70 | 86 |
|
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", |
74 | 90 |
|
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 | + ), |
82 | 98 |
|
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 | +} |
0 commit comments