nitropack-nightly 2.11.0-20250303-165232.8dd35e61 → 2.11.0-20250303-174007.c9fdeb77
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/meta/index.d.mts +1 -1
- package/dist/meta/index.d.ts +1 -1
- package/dist/meta/index.mjs +1 -1
- package/dist/presets/_unenv/preset-workerd.d.ts +3 -2
- package/dist/presets/_unenv/preset-workerd.mjs +16 -2
- package/dist/presets/cloudflare/preset.mjs +12 -20
- package/dist/presets/cloudflare/types.d.ts +14 -2
- package/dist/presets/cloudflare/utils.d.ts +2 -1
- package/dist/presets/cloudflare/utils.mjs +46 -17
- package/package.json +1 -1
package/dist/meta/index.d.mts
CHANGED
package/dist/meta/index.d.ts
CHANGED
package/dist/meta/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Preset } from "unenv";
|
|
2
2
|
import type { Plugin } from "rollup";
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
3
|
+
export declare const unenvCfExternals: Preset;
|
|
4
|
+
export declare const unenvWorkerdWithNodeCompat: Preset;
|
|
5
|
+
export declare const workerdHybridNodeCompatPlugin: Plugin;
|
|
@@ -3,7 +3,21 @@ import { join } from "pathe";
|
|
|
3
3
|
import { builtnNodeModules, hybridNodeModules } from "./node-compat/cloudflare.mjs";
|
|
4
4
|
const workerdDir = fileURLToPath(new URL("workerd/", import.meta.url));
|
|
5
5
|
const resolvePresetRuntime = (m) => join(workerdDir, `${m}.mjs`);
|
|
6
|
-
export const
|
|
6
|
+
export const unenvCfExternals = {
|
|
7
|
+
meta: {
|
|
8
|
+
name: "nitro-cloudflare:externals"
|
|
9
|
+
},
|
|
10
|
+
external: [
|
|
11
|
+
"cloudflare:email",
|
|
12
|
+
"cloudflare:sockets",
|
|
13
|
+
"cloudflare:workers",
|
|
14
|
+
"cloudflare:workflows"
|
|
15
|
+
]
|
|
16
|
+
};
|
|
17
|
+
export const unenvWorkerdWithNodeCompat = {
|
|
18
|
+
meta: {
|
|
19
|
+
name: "nitro-cloudflare:node-compat"
|
|
20
|
+
},
|
|
7
21
|
external: builtnNodeModules.map((m) => `node:${m}`),
|
|
8
22
|
alias: {
|
|
9
23
|
// (native)
|
|
@@ -25,7 +39,7 @@ export const unenvWorkerdPreset = {
|
|
|
25
39
|
)
|
|
26
40
|
}
|
|
27
41
|
};
|
|
28
|
-
export const
|
|
42
|
+
export const workerdHybridNodeCompatPlugin = {
|
|
29
43
|
name: "nitro:cloudflare:hybrid-node-compat",
|
|
30
44
|
resolveId(id) {
|
|
31
45
|
if (id.startsWith("cloudflare:")) {
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { defineNitroPreset } from "nitropack/kit";
|
|
2
2
|
import { writeFile } from "nitropack/kit";
|
|
3
3
|
import { resolve } from "pathe";
|
|
4
|
+
import { unenvCfExternals } from "../_unenv/preset-workerd.mjs";
|
|
4
5
|
import {
|
|
6
|
+
enableNodeCompat,
|
|
5
7
|
writeWranglerConfig,
|
|
6
8
|
writeCFRoutes,
|
|
7
9
|
writeCFPagesHeaders,
|
|
8
10
|
writeCFPagesRedirects
|
|
9
11
|
} from "./utils.mjs";
|
|
10
|
-
import { hybridNodePlugin, unenvWorkerdPreset } from "../_unenv/preset-workerd.mjs";
|
|
11
12
|
import cfLegacyPresets from "./preset-legacy.mjs";
|
|
12
13
|
const isWindows = process.platform === "win32";
|
|
13
14
|
const commandWithDir = (command) => isWindows ? `cmd /c "cd ./ && ${command}"` : `(cd ./ && ${command})`;
|
|
@@ -25,7 +26,7 @@ const cloudflarePages = defineNitroPreset(
|
|
|
25
26
|
publicDir: "{{ output.dir }}/{{ baseURL }}",
|
|
26
27
|
serverDir: "{{ output.dir }}/_worker.js"
|
|
27
28
|
},
|
|
28
|
-
unenv:
|
|
29
|
+
unenv: [unenvCfExternals],
|
|
29
30
|
alias: {
|
|
30
31
|
// Hotfix: Cloudflare appends /index.html if mime is not found and things like ico are not in standard lite.js!
|
|
31
32
|
// https://github.com/nitrojs/nitro/pull/933
|
|
@@ -36,7 +37,6 @@ const cloudflarePages = defineNitroPreset(
|
|
|
36
37
|
esmImport: true
|
|
37
38
|
},
|
|
38
39
|
rollupConfig: {
|
|
39
|
-
plugins: [hybridNodePlugin],
|
|
40
40
|
output: {
|
|
41
41
|
entryFileNames: "index.js",
|
|
42
42
|
format: "esm",
|
|
@@ -44,12 +44,11 @@ const cloudflarePages = defineNitroPreset(
|
|
|
44
44
|
}
|
|
45
45
|
},
|
|
46
46
|
hooks: {
|
|
47
|
+
"build:before": async (nitro) => {
|
|
48
|
+
await enableNodeCompat(nitro);
|
|
49
|
+
},
|
|
47
50
|
async compiled(nitro) {
|
|
48
|
-
await writeWranglerConfig(
|
|
49
|
-
nitro,
|
|
50
|
-
true
|
|
51
|
-
/* pages */
|
|
52
|
-
);
|
|
51
|
+
await writeWranglerConfig(nitro, "pages");
|
|
53
52
|
await writeCFRoutes(nitro);
|
|
54
53
|
await writeCFPagesHeaders(nitro);
|
|
55
54
|
await writeCFPagesRedirects(nitro);
|
|
@@ -75,11 +74,6 @@ const cloudflarePagesStatic = defineNitroPreset(
|
|
|
75
74
|
},
|
|
76
75
|
hooks: {
|
|
77
76
|
async compiled(nitro) {
|
|
78
|
-
await writeWranglerConfig(
|
|
79
|
-
nitro,
|
|
80
|
-
true
|
|
81
|
-
/* pages */
|
|
82
|
-
);
|
|
83
77
|
await writeCFPagesHeaders(nitro);
|
|
84
78
|
await writeCFPagesRedirects(nitro);
|
|
85
79
|
}
|
|
@@ -101,9 +95,8 @@ const cloudflareModule = defineNitroPreset(
|
|
|
101
95
|
preview: commandWithDir("npx wrangler dev"),
|
|
102
96
|
deploy: commandWithDir("npx wrangler deploy")
|
|
103
97
|
},
|
|
104
|
-
unenv:
|
|
98
|
+
unenv: [unenvCfExternals],
|
|
105
99
|
rollupConfig: {
|
|
106
|
-
plugins: [hybridNodePlugin],
|
|
107
100
|
output: {
|
|
108
101
|
format: "esm",
|
|
109
102
|
exports: "named",
|
|
@@ -115,12 +108,11 @@ const cloudflareModule = defineNitroPreset(
|
|
|
115
108
|
esmImport: true
|
|
116
109
|
},
|
|
117
110
|
hooks: {
|
|
111
|
+
"build:before": async (nitro) => {
|
|
112
|
+
await enableNodeCompat(nitro);
|
|
113
|
+
},
|
|
118
114
|
async compiled(nitro) {
|
|
119
|
-
await writeWranglerConfig(
|
|
120
|
-
nitro,
|
|
121
|
-
false
|
|
122
|
-
/* module */
|
|
123
|
-
);
|
|
115
|
+
await writeWranglerConfig(nitro, "module");
|
|
124
116
|
await writeFile(
|
|
125
117
|
resolve(nitro.options.output.dir, "package.json"),
|
|
126
118
|
JSON.stringify({ private: true, main: "./server/index.mjs" }, null, 2)
|
|
@@ -19,12 +19,24 @@ export interface CloudflareOptions {
|
|
|
19
19
|
*/
|
|
20
20
|
wrangler?: WranglerConfig;
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* Enable native Node.js compatibility support.
|
|
23
|
+
*
|
|
24
|
+
* Requires `nodejs_compat` compatibility flag (Nitro enables it by default).
|
|
25
|
+
*
|
|
26
|
+
* If disabled, pure unenv polyfills will be used instead.
|
|
27
|
+
*
|
|
28
|
+
* Enabled by default with `compatibilityDate` >= `2025-03-01`.
|
|
29
|
+
*/
|
|
30
|
+
nodeCompat?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Disable the automatic generation of `.wrangler/deploy/config.json`.
|
|
33
|
+
*
|
|
34
|
+
* Enabled by default with `compatibilityDate` >= `2025-03-01` unless explicitly set to `false`.
|
|
23
35
|
*
|
|
24
36
|
* More info: https://developers.cloudflare.com/workers/wrangler/configuration#generated-wrangler-configuration
|
|
25
37
|
*/
|
|
26
38
|
noWranglerDeployConfig?: boolean;
|
|
27
|
-
pages
|
|
39
|
+
pages?: {
|
|
28
40
|
/**
|
|
29
41
|
* Nitro will automatically generate a `_routes.json` that controls which files get served statically and
|
|
30
42
|
* which get served by the Worker. Using this config will override the automatic `_routes.json`. Or, if the
|
|
@@ -2,4 +2,5 @@ import type { Nitro } from "nitropack/types";
|
|
|
2
2
|
export declare function writeCFRoutes(nitro: Nitro): Promise<void>;
|
|
3
3
|
export declare function writeCFPagesHeaders(nitro: Nitro): Promise<void>;
|
|
4
4
|
export declare function writeCFPagesRedirects(nitro: Nitro): Promise<void>;
|
|
5
|
-
export declare function
|
|
5
|
+
export declare function enableNodeCompat(nitro: Nitro): Promise<void>;
|
|
6
|
+
export declare function writeWranglerConfig(nitro: Nitro, cfTarget: "pages" | "module"): Promise<void>;
|
|
@@ -13,6 +13,10 @@ import {
|
|
|
13
13
|
withTrailingSlash,
|
|
14
14
|
withoutLeadingSlash
|
|
15
15
|
} from "ufo";
|
|
16
|
+
import {
|
|
17
|
+
workerdHybridNodeCompatPlugin,
|
|
18
|
+
unenvWorkerdWithNodeCompat
|
|
19
|
+
} from "../_unenv/preset-workerd.mjs";
|
|
16
20
|
export async function writeCFRoutes(nitro) {
|
|
17
21
|
const _cfPagesConfig = nitro.options.cloudflare?.pages || {};
|
|
18
22
|
const routes = {
|
|
@@ -133,13 +137,32 @@ export async function writeCFPagesRedirects(nitro) {
|
|
|
133
137
|
}
|
|
134
138
|
await writeFile(redirectsPath, contents.join("\n"), true);
|
|
135
139
|
}
|
|
136
|
-
|
|
140
|
+
const wranglerConfigAndUnenv2CompatDate = "2025-03-01";
|
|
141
|
+
export async function enableNodeCompat(nitro) {
|
|
142
|
+
const compatDate = nitro.options.compatibilityDate.cloudflare || nitro.options.compatibilityDate.default;
|
|
143
|
+
const nodeCompatEnabled = nitro.options.cloudflare?.nodeCompat ?? compatDate >= wranglerConfigAndUnenv2CompatDate;
|
|
144
|
+
if (compatDate < wranglerConfigAndUnenv2CompatDate && nitro.options.cloudflare?.nodeCompat === void 0) {
|
|
145
|
+
nitro.logger.warn(
|
|
146
|
+
`Current compatibility date "${compatDate}" does not supports native Node.js support in cloudflare workers. Please consider upgrading compatibilityDate to "${wranglerConfigAndUnenv2CompatDate}" or newer.`
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
if (nodeCompatEnabled) {
|
|
150
|
+
nitro.options.unenv.push(unenvWorkerdWithNodeCompat);
|
|
151
|
+
nitro.options.rollupConfig.plugins ??= [];
|
|
152
|
+
nitro.options.rollupConfig.plugins.push(
|
|
153
|
+
workerdHybridNodeCompatPlugin
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
nitro.options.cloudflare ??= {};
|
|
157
|
+
nitro.options.cloudflare.nodeCompat = nodeCompatEnabled;
|
|
158
|
+
}
|
|
159
|
+
export async function writeWranglerConfig(nitro, cfTarget) {
|
|
137
160
|
const wranglerConfigDir = nitro.options.output.serverDir;
|
|
138
161
|
const wranglerConfigPath = join(wranglerConfigDir, "wrangler.json");
|
|
139
162
|
const defaults = {};
|
|
140
163
|
const overrides = {};
|
|
141
|
-
defaults.compatibility_date = nitro.options.compatibilityDate.cloudflare || nitro.options.compatibilityDate.default;
|
|
142
|
-
if (
|
|
164
|
+
const compatDate = defaults.compatibility_date = nitro.options.compatibilityDate.cloudflare || nitro.options.compatibilityDate.default;
|
|
165
|
+
if (cfTarget === "pages") {
|
|
143
166
|
overrides.pages_build_output_dir = relative(
|
|
144
167
|
wranglerConfigDir,
|
|
145
168
|
nitro.options.output.publicDir
|
|
@@ -170,19 +193,21 @@ export async function writeWranglerConfig(nitro, isPages) {
|
|
|
170
193
|
defaults
|
|
171
194
|
);
|
|
172
195
|
const compatFlags = new Set(wranglerConfig.compatibility_flags || []);
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
196
|
+
if (nitro.options.cloudflare?.nodeCompat) {
|
|
197
|
+
if (compatFlags.has("nodejs_compat_v2") && compatFlags.has("no_nodejs_compat_v2")) {
|
|
198
|
+
nitro.logger.warn(
|
|
199
|
+
"[nitro] [cloudflare] Wrangler config `compatibility_flags` contains both `nodejs_compat_v2` and `no_nodejs_compat_v2`. Ignoring `nodejs_compat_v2`."
|
|
200
|
+
);
|
|
201
|
+
compatFlags.delete("nodejs_compat_v2");
|
|
202
|
+
}
|
|
203
|
+
if (compatFlags.has("nodejs_compat_v2")) {
|
|
204
|
+
nitro.logger.warn(
|
|
205
|
+
"[nitro] [cloudflare] Please consider replacing `nodejs_compat_v2` with `nodejs_compat` in your `compatibility_flags` or USE IT AT YOUR OWN RISK as it can cause issues with nitro."
|
|
206
|
+
);
|
|
207
|
+
} else {
|
|
208
|
+
compatFlags.add("nodejs_compat");
|
|
209
|
+
compatFlags.add("no_nodejs_compat_v2");
|
|
210
|
+
}
|
|
186
211
|
}
|
|
187
212
|
wranglerConfig.compatibility_flags = [...compatFlags];
|
|
188
213
|
await writeFile(
|
|
@@ -190,7 +215,11 @@ export async function writeWranglerConfig(nitro, isPages) {
|
|
|
190
215
|
JSON.stringify(wranglerConfig, null, 2),
|
|
191
216
|
true
|
|
192
217
|
);
|
|
193
|
-
|
|
218
|
+
let shouldWriteWranglerDeployConfig = compatDate >= wranglerConfigAndUnenv2CompatDate;
|
|
219
|
+
if (nitro.options.cloudflare?.noWranglerDeployConfig) {
|
|
220
|
+
shouldWriteWranglerDeployConfig = false;
|
|
221
|
+
}
|
|
222
|
+
if (shouldWriteWranglerDeployConfig) {
|
|
194
223
|
const configPath = join(
|
|
195
224
|
nitro.options.rootDir,
|
|
196
225
|
".wrangler/deploy/config.json"
|
package/package.json
CHANGED