@analogjs/vite-plugin-nitro 3.0.0-alpha.7 → 3.0.0-alpha.9
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/package.json +3 -6
- package/src/index.d.ts +1 -1
- package/src/index.js +6 -2
- package/src/index.js.map +1 -1
- package/src/lib/build-server.js +97 -140
- package/src/lib/build-server.js.map +1 -1
- package/src/lib/build-sitemap.js +48 -60
- package/src/lib/build-sitemap.js.map +1 -1
- package/src/lib/build-ssr.js +16 -18
- package/src/lib/build-ssr.js.map +1 -1
- package/src/lib/hooks/post-rendering-hook.js +10 -6
- package/src/lib/hooks/post-rendering-hook.js.map +1 -1
- package/src/lib/options.d.ts +8 -0
- package/src/lib/plugins/dev-server-plugin.js +91 -101
- package/src/lib/plugins/dev-server-plugin.js.map +1 -1
- package/src/lib/plugins/page-endpoints.js +26 -46
- package/src/lib/plugins/page-endpoints.js.map +1 -1
- package/src/lib/utils/get-content-files.js +88 -97
- package/src/lib/utils/get-content-files.js.map +1 -1
- package/src/lib/utils/get-page-handlers.js +70 -84
- package/src/lib/utils/get-page-handlers.js.map +1 -1
- package/src/lib/utils/node-web-bridge.js +34 -45
- package/src/lib/utils/node-web-bridge.js.map +1 -1
- package/src/lib/utils/register-dev-middleware.js +41 -47
- package/src/lib/utils/register-dev-middleware.js.map +1 -1
- package/src/lib/utils/renderers.js +55 -51
- package/src/lib/utils/renderers.js.map +1 -1
- package/src/lib/utils/rolldown.d.ts +2 -0
- package/src/lib/utils/rolldown.js +12 -0
- package/src/lib/utils/rolldown.js.map +1 -0
- package/src/lib/vite-plugin-nitro.js +442 -676
- package/src/lib/vite-plugin-nitro.js.map +1 -1
- package/README.md +0 -125
- package/src/lib/options.js +0 -2
- package/src/lib/options.js.map +0 -1
- package/src/lib/utils/load-esm.js +0 -23
- package/src/lib/utils/load-esm.js.map +0 -1
|
@@ -1,705 +1,471 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
1
|
+
import { buildServer, isVercelPreset } from "./build-server.js";
|
|
2
|
+
import { getBundleOptionsKey, isRolldown } from "./utils/rolldown.js";
|
|
3
|
+
import { buildSSRApp } from "./build-ssr.js";
|
|
4
|
+
import { apiMiddleware, clientRenderer, ssrRenderer } from "./utils/renderers.js";
|
|
5
|
+
import { pageEndpointsPlugin } from "./plugins/page-endpoints.js";
|
|
6
|
+
import { getPageHandlers } from "./utils/get-page-handlers.js";
|
|
7
|
+
import { buildSitemap } from "./build-sitemap.js";
|
|
8
|
+
import { toWebRequest, writeWebResponseToNode } from "./utils/node-web-bridge.js";
|
|
9
|
+
import { devServerPlugin } from "./plugins/dev-server-plugin.js";
|
|
10
|
+
import { getMatchingContentFilesWithFrontMatter } from "./utils/get-content-files.js";
|
|
11
|
+
import { build, createDevServer, createNitro } from "nitro/builder";
|
|
12
|
+
import { mergeConfig, normalizePath } from "vite";
|
|
13
|
+
import { relative, resolve } from "node:path";
|
|
14
|
+
import { pathToFileURL } from "node:url";
|
|
15
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
16
|
+
//#region packages/vite-plugin-nitro/src/lib/vite-plugin-nitro.ts
|
|
16
17
|
function createNitroMiddlewareHandler(handler) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
return {
|
|
19
|
+
route: "/**",
|
|
20
|
+
handler,
|
|
21
|
+
middleware: true
|
|
22
|
+
};
|
|
22
23
|
}
|
|
23
24
|
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
25
|
+
* Creates a `rollup:before` hook that marks specified packages as external
|
|
26
|
+
* in Nitro's bundler config (applied to both the server build and the
|
|
27
|
+
* prerender build).
|
|
28
|
+
*
|
|
29
|
+
* ## Subpath matching (Rolldown compatibility)
|
|
30
|
+
*
|
|
31
|
+
* When `bundlerConfig.external` is an **array**, Rollup automatically
|
|
32
|
+
* prefix-matches entries — `'rxjs'` in the array will also externalise
|
|
33
|
+
* `'rxjs/operators'`, `'rxjs/internal/Observable'`, etc.
|
|
34
|
+
*
|
|
35
|
+
* Rolldown (the default bundler in Nitro v3) does **not** do this. It
|
|
36
|
+
* treats array entries as exact strings. To keep behaviour consistent
|
|
37
|
+
* across both bundlers, the **function** branch already needed explicit
|
|
38
|
+
* subpath matching. We now use the same `isExternal` helper for all
|
|
39
|
+
* branches so that `'rxjs'` reliably matches `'rxjs/operators'`
|
|
40
|
+
* regardless of whether the existing `external` value is a function,
|
|
41
|
+
* array, or absent.
|
|
42
|
+
*
|
|
43
|
+
* Without this, the Nitro prerender build fails on Windows CI with:
|
|
44
|
+
*
|
|
45
|
+
* [RESOLVE_ERROR] Could not resolve 'rxjs/operators'
|
|
46
|
+
*/
|
|
46
47
|
function createRollupBeforeHook(externalEntries) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
else if (typeof existing === 'function') {
|
|
58
|
-
bundlerConfig.external = (source, importer, isResolved) => existing(source, importer, isResolved) || isExternal(source);
|
|
59
|
-
}
|
|
60
|
-
else if (Array.isArray(existing)) {
|
|
61
|
-
bundlerConfig.external = [...existing, ...externalEntries];
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
bundlerConfig.external = [existing, ...externalEntries];
|
|
65
|
-
}
|
|
66
|
-
};
|
|
48
|
+
const isExternal = (source) => externalEntries.some((entry) => source === entry || source.startsWith(entry + "/"));
|
|
49
|
+
return (_nitro, bundlerConfig) => {
|
|
50
|
+
sanitizeNitroBundlerConfig(_nitro, bundlerConfig);
|
|
51
|
+
if (externalEntries.length === 0) return;
|
|
52
|
+
const existing = bundlerConfig.external;
|
|
53
|
+
if (!existing) bundlerConfig.external = externalEntries;
|
|
54
|
+
else if (typeof existing === "function") bundlerConfig.external = (source, importer, isResolved) => existing(source, importer, isResolved) || isExternal(source);
|
|
55
|
+
else if (Array.isArray(existing)) bundlerConfig.external = [...existing, ...externalEntries];
|
|
56
|
+
else bundlerConfig.external = [existing, ...externalEntries];
|
|
57
|
+
};
|
|
67
58
|
}
|
|
68
59
|
function appendNoExternals(noExternals, ...entries) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
return Array.isArray(noExternals)
|
|
73
|
-
? [...noExternals, ...entries]
|
|
74
|
-
: noExternals;
|
|
60
|
+
if (!noExternals) return entries;
|
|
61
|
+
return Array.isArray(noExternals) ? [...noExternals, ...entries] : noExternals;
|
|
75
62
|
}
|
|
76
63
|
/**
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
64
|
+
* Patches Nitro's internal Rollup/Rolldown bundler config to work around
|
|
65
|
+
* incompatibilities in the Nitro v3 alpha series.
|
|
66
|
+
*
|
|
67
|
+
* Called from the `rollup:before` hook, this function runs against the *final*
|
|
68
|
+
* bundler config that Nitro assembles for its server/prerender builds — it
|
|
69
|
+
* does NOT touch the normal Vite client or SSR environment configs.
|
|
70
|
+
*
|
|
71
|
+
* Each workaround is narrowly scoped and safe to remove once the corresponding
|
|
72
|
+
* upstream Nitro issue is resolved.
|
|
73
|
+
*/
|
|
87
74
|
function sanitizeNitroBundlerConfig(_nitro, bundlerConfig) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
if ('codeSplitting' in output) {
|
|
103
|
-
delete output['codeSplitting'];
|
|
104
|
-
}
|
|
105
|
-
// ── 2. Remove invalid `output.manualChunks` ─────────────────────────
|
|
106
|
-
//
|
|
107
|
-
// Nitro's default config enables manual chunking for node_modules. Under
|
|
108
|
-
// Nitro v3 alpha + Rollup 4.59 this crashes during the prerender rebundle:
|
|
109
|
-
//
|
|
110
|
-
// Cannot read properties of undefined (reading 'included')
|
|
111
|
-
//
|
|
112
|
-
// A single server bundle is acceptable for Analog's use case, so we strip
|
|
113
|
-
// `manualChunks` until the upstream bug is fixed.
|
|
114
|
-
if ('manualChunks' in output) {
|
|
115
|
-
delete output['manualChunks'];
|
|
116
|
-
}
|
|
117
|
-
// ── 3. Escape route params in `output.chunkFileNames` ───────────────
|
|
118
|
-
//
|
|
119
|
-
// Nitro's `getChunkName()` derives chunk filenames from route patterns,
|
|
120
|
-
// using its internal `routeToFsPath()` helper to convert route params
|
|
121
|
-
// (`:productId` → `[productId]`) and catch-alls (`**` → `[...]`).
|
|
122
|
-
//
|
|
123
|
-
// Rollup/Rolldown interprets *any* `[token]` in the string returned by a
|
|
124
|
-
// `chunkFileNames` function as a placeholder. Only a handful are valid —
|
|
125
|
-
// `[name]`, `[hash]`, `[format]`, `[ext]` — so route-derived tokens like
|
|
126
|
-
// `[productId]` or `[...]` trigger a build error:
|
|
127
|
-
//
|
|
128
|
-
// "[productId]" is not a valid placeholder in the "output.chunkFileNames" pattern.
|
|
129
|
-
//
|
|
130
|
-
// We wrap the original function to replace non-standard `[token]` patterns
|
|
131
|
-
// with `_token_`, preserving the intended filename while avoiding the
|
|
132
|
-
// placeholder validation error.
|
|
133
|
-
//
|
|
134
|
-
// Example: `_routes/products/[productId].mjs` → `_routes/products/_productId_.mjs`
|
|
135
|
-
const VALID_ROLLUP_PLACEHOLDER = /^\[(?:name|hash|format|ext)\]$/;
|
|
136
|
-
const chunkFileNames = output['chunkFileNames'];
|
|
137
|
-
if (typeof chunkFileNames === 'function') {
|
|
138
|
-
const originalFn = chunkFileNames;
|
|
139
|
-
output['chunkFileNames'] = (...args) => {
|
|
140
|
-
const result = originalFn(...args);
|
|
141
|
-
if (typeof result !== 'string')
|
|
142
|
-
return result;
|
|
143
|
-
return result.replace(/\[[^\]]+\]/g, (match) => VALID_ROLLUP_PLACEHOLDER.test(match)
|
|
144
|
-
? match
|
|
145
|
-
: `_${match.slice(1, -1)}_`);
|
|
146
|
-
};
|
|
147
|
-
}
|
|
75
|
+
const output = bundlerConfig["output"];
|
|
76
|
+
if (!output || Array.isArray(output) || typeof output !== "object") return;
|
|
77
|
+
if ("codeSplitting" in output) delete output["codeSplitting"];
|
|
78
|
+
if ("manualChunks" in output) delete output["manualChunks"];
|
|
79
|
+
const VALID_ROLLUP_PLACEHOLDER = /^\[(?:name|hash|format|ext)\]$/;
|
|
80
|
+
const chunkFileNames = output["chunkFileNames"];
|
|
81
|
+
if (typeof chunkFileNames === "function") {
|
|
82
|
+
const originalFn = chunkFileNames;
|
|
83
|
+
output["chunkFileNames"] = (...args) => {
|
|
84
|
+
const result = originalFn(...args);
|
|
85
|
+
if (typeof result !== "string") return result;
|
|
86
|
+
return result.replace(/\[[^\]]+\]/g, (match) => VALID_ROLLUP_PLACEHOLDER.test(match) ? match : `_${match.slice(1, -1)}_`);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
148
89
|
}
|
|
149
90
|
function resolveClientOutputPath(cachedPath, workspaceRoot, rootDir, configuredOutDir, ssrBuild) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (!ssrBuild) {
|
|
154
|
-
return resolve(workspaceRoot, rootDir, configuredOutDir || 'dist/client');
|
|
155
|
-
}
|
|
156
|
-
// SSR builds write server assets to dist/<app>/ssr, but the renderer template
|
|
157
|
-
// still needs the client index.html emitted to dist/<app>/client.
|
|
158
|
-
return resolve(workspaceRoot, 'dist', rootDir, 'client');
|
|
91
|
+
if (cachedPath) return cachedPath;
|
|
92
|
+
if (!ssrBuild) return resolve(workspaceRoot, rootDir, configuredOutDir || "dist/client");
|
|
93
|
+
return resolve(workspaceRoot, "dist", rootDir, "client");
|
|
159
94
|
}
|
|
160
95
|
/**
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
96
|
+
* Converts the built SSR entry path into a specifier that Nitro's bundler
|
|
97
|
+
* can resolve, including all relative `./assets/*` chunk imports inside
|
|
98
|
+
* the entry.
|
|
99
|
+
*
|
|
100
|
+
* The returned path **must** be an absolute filesystem path with forward
|
|
101
|
+
* slashes (e.g. `D:/a/analog/dist/apps/blog-app/ssr/main.server.js`).
|
|
102
|
+
* This lets Rollup/Rolldown determine the entry's directory and resolve
|
|
103
|
+
* sibling chunk imports like `./assets/core-DTazUigR.js` correctly.
|
|
104
|
+
*
|
|
105
|
+
* ## Why not pathToFileURL() on Windows?
|
|
106
|
+
*
|
|
107
|
+
* Earlier versions converted the path to a `file:///D:/a/...` URL on
|
|
108
|
+
* Windows, which worked with Nitro v2 + Rollup. Nitro v3 switched its
|
|
109
|
+
* default bundler to Rolldown, and Rolldown does **not** extract the
|
|
110
|
+
* importer directory from `file://` URLs. This caused every relative
|
|
111
|
+
* import inside the SSR entry to fail during the prerender build:
|
|
112
|
+
*
|
|
113
|
+
* [RESOLVE_ERROR] Could not resolve './assets/core-DTazUigR.js'
|
|
114
|
+
* in ../../dist/apps/blog-app/ssr/main.server.js
|
|
115
|
+
*
|
|
116
|
+
* `normalizePath()` (from Vite) simply converts backslashes to forward
|
|
117
|
+
* slashes, which both Rollup and Rolldown handle correctly on all
|
|
118
|
+
* platforms.
|
|
119
|
+
*/
|
|
185
120
|
function toNitroSsrEntrypointSpecifier(ssrEntryPath) {
|
|
186
|
-
|
|
121
|
+
return normalizePath(ssrEntryPath);
|
|
187
122
|
}
|
|
188
123
|
function applySsrEntryAlias(nitroConfig, options, workspaceRoot, rootDir) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
}
|
|
124
|
+
const ssrOutDir = options?.ssrBuildDir || resolve(workspaceRoot, "dist", rootDir, "ssr");
|
|
125
|
+
if (options?.ssr || nitroConfig.prerender?.routes?.length) {
|
|
126
|
+
const ssrEntry = toNitroSsrEntrypointSpecifier(resolveBuiltSsrEntryPath(ssrOutDir));
|
|
127
|
+
nitroConfig.alias = {
|
|
128
|
+
...nitroConfig.alias,
|
|
129
|
+
"#analog/ssr": ssrEntry
|
|
130
|
+
};
|
|
131
|
+
}
|
|
198
132
|
}
|
|
199
133
|
function resolveBuiltSsrEntryPath(ssrOutDir) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
return ssrEntryPath;
|
|
134
|
+
const candidatePaths = [
|
|
135
|
+
resolve(ssrOutDir, "main.server.mjs"),
|
|
136
|
+
resolve(ssrOutDir, "main.server.js"),
|
|
137
|
+
resolve(ssrOutDir, "main.server")
|
|
138
|
+
];
|
|
139
|
+
const ssrEntryPath = candidatePaths.find((candidatePath) => existsSync(candidatePath));
|
|
140
|
+
if (!ssrEntryPath) throw new Error(`Unable to locate the built SSR entry in "${ssrOutDir}". Expected one of: ${candidatePaths.join(", ")}`);
|
|
141
|
+
return ssrEntryPath;
|
|
210
142
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
// - `nitroConfig.prerender?.routes?.length` → prerender-only
|
|
463
|
-
if (ssrBuild ||
|
|
464
|
-
options?.ssr ||
|
|
465
|
-
nitroConfig.prerender?.routes?.length) {
|
|
466
|
-
if (process.platform === 'win32') {
|
|
467
|
-
nitroConfig.noExternals = appendNoExternals(nitroConfig.noExternals, 'std-env');
|
|
468
|
-
}
|
|
469
|
-
rollupExternalEntries.push('rxjs', 'node-fetch-native/dist/polyfill');
|
|
470
|
-
nitroConfig = {
|
|
471
|
-
...nitroConfig,
|
|
472
|
-
moduleSideEffects: ['zone.js/node', 'zone.js/fesm2015/zone-node'],
|
|
473
|
-
handlers: [
|
|
474
|
-
...(hasAPIDir
|
|
475
|
-
? []
|
|
476
|
-
: useAPIMiddleware
|
|
477
|
-
? [createNitroMiddlewareHandler('#ANALOG_API_MIDDLEWARE')]
|
|
478
|
-
: []),
|
|
479
|
-
...pageHandlers,
|
|
480
|
-
// Preserve the renderer catch-all handler added above
|
|
481
|
-
{
|
|
482
|
-
handler: rendererHandler,
|
|
483
|
-
route: '/**',
|
|
484
|
-
lazy: true,
|
|
485
|
-
},
|
|
486
|
-
],
|
|
487
|
-
};
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
nitroConfig = mergeConfig(nitroConfig, nitroOptions);
|
|
491
|
-
return {
|
|
492
|
-
environments: {
|
|
493
|
-
client: {
|
|
494
|
-
build: {
|
|
495
|
-
outDir: config?.build?.outDir ||
|
|
496
|
-
resolve(workspaceRoot, 'dist', rootDir, 'client'),
|
|
497
|
-
},
|
|
498
|
-
},
|
|
499
|
-
ssr: {
|
|
500
|
-
build: {
|
|
501
|
-
ssr: true,
|
|
502
|
-
[vite.rolldownVersion ? 'rolldownOptions' : 'rollupOptions']: {
|
|
503
|
-
input: options?.entryServer ||
|
|
504
|
-
resolve(workspaceRoot, rootDir, `${sourceRoot}/main.server.ts`),
|
|
505
|
-
},
|
|
506
|
-
outDir: options?.ssrBuildDir ||
|
|
507
|
-
resolve(workspaceRoot, 'dist', rootDir, 'ssr'),
|
|
508
|
-
},
|
|
509
|
-
},
|
|
510
|
-
},
|
|
511
|
-
builder: {
|
|
512
|
-
sharedPlugins: true,
|
|
513
|
-
buildApp: async (builder) => {
|
|
514
|
-
environmentBuild = true;
|
|
515
|
-
const builds = [builder.build(builder.environments['client'])];
|
|
516
|
-
if (options?.ssr || nitroConfig.prerender?.routes?.length) {
|
|
517
|
-
builds.push(builder.build(builder.environments['ssr']));
|
|
518
|
-
}
|
|
519
|
-
await Promise.all(builds);
|
|
520
|
-
applySsrEntryAlias(nitroConfig, options, workspaceRoot, rootDir);
|
|
521
|
-
await buildServer(options, nitroConfig, routeSourceFiles);
|
|
522
|
-
if (nitroConfig.prerender?.routes?.length &&
|
|
523
|
-
options?.prerender?.sitemap) {
|
|
524
|
-
const publicDir = nitroConfig.output?.publicDir;
|
|
525
|
-
if (!publicDir) {
|
|
526
|
-
throw new Error('Nitro public output directory is required to build the sitemap.');
|
|
527
|
-
}
|
|
528
|
-
console.log('Building Sitemap...');
|
|
529
|
-
// sitemap needs to be built after all directories are built
|
|
530
|
-
await buildSitemap(config, options.prerender.sitemap, nitroConfig.prerender.routes, publicDir, routeSitemaps);
|
|
531
|
-
}
|
|
532
|
-
console.log(`\n\nThe '@analogjs/platform' server has been successfully built.`);
|
|
533
|
-
},
|
|
534
|
-
},
|
|
535
|
-
};
|
|
536
|
-
},
|
|
537
|
-
async configureServer(viteServer) {
|
|
538
|
-
if (isServe && !isTest) {
|
|
539
|
-
const nitro = await createNitro({
|
|
540
|
-
dev: true,
|
|
541
|
-
// Nitro's Vite builder now rejects `build()` in dev mode, but Analog's
|
|
542
|
-
// dev integration still relies on the builder-driven reload hooks.
|
|
543
|
-
// Force the server worker onto Rollup for this dev-only path.
|
|
544
|
-
builder: 'rollup',
|
|
545
|
-
...nitroConfig,
|
|
546
|
-
});
|
|
547
|
-
const server = createDevServer(nitro);
|
|
548
|
-
await build(nitro);
|
|
549
|
-
const apiHandler = async (req, res) => {
|
|
550
|
-
// Nitro v3's dev server is fetch-first, so adapt Vite's Node
|
|
551
|
-
// request once and let Nitro respond with a standard Web Response.
|
|
552
|
-
const response = await server.fetch(toWebRequest(req));
|
|
553
|
-
await writeWebResponseToNode(res, response);
|
|
554
|
-
};
|
|
555
|
-
if (hasAPIDir) {
|
|
556
|
-
viteServer.middlewares.use((req, res, next) => {
|
|
557
|
-
if (req.url?.startsWith(`${prefix}${apiPrefix}`)) {
|
|
558
|
-
void apiHandler(req, res).catch((error) => next(error));
|
|
559
|
-
return;
|
|
560
|
-
}
|
|
561
|
-
next();
|
|
562
|
-
});
|
|
563
|
-
}
|
|
564
|
-
else {
|
|
565
|
-
viteServer.middlewares.use(apiPrefix, (req, res, next) => {
|
|
566
|
-
void apiHandler(req, res).catch((error) => next(error));
|
|
567
|
-
});
|
|
568
|
-
}
|
|
569
|
-
viteServer.httpServer?.once('listening', () => {
|
|
570
|
-
process.env['ANALOG_HOST'] = !viteServer.config.server.host
|
|
571
|
-
? 'localhost'
|
|
572
|
-
: viteServer.config.server.host;
|
|
573
|
-
process.env['ANALOG_PORT'] = `${viteServer.config.server.port}`;
|
|
574
|
-
});
|
|
575
|
-
// handle upgrades if websockets are enabled
|
|
576
|
-
if (nitroOptions?.experimental?.websocket) {
|
|
577
|
-
viteServer.httpServer?.on('upgrade', server.upgrade);
|
|
578
|
-
}
|
|
579
|
-
console.log(`\n\nThe server endpoints are accessible under the "${prefix}${apiPrefix}" path.`);
|
|
580
|
-
}
|
|
581
|
-
},
|
|
582
|
-
async closeBundle() {
|
|
583
|
-
// Skip when build is triggered by the Environment API
|
|
584
|
-
if (environmentBuild) {
|
|
585
|
-
return;
|
|
586
|
-
}
|
|
587
|
-
if (ssrBuild) {
|
|
588
|
-
return;
|
|
589
|
-
}
|
|
590
|
-
if (isBuild) {
|
|
591
|
-
if (options?.ssr) {
|
|
592
|
-
console.log('Building SSR application...');
|
|
593
|
-
await buildSSRApp(config, options);
|
|
594
|
-
}
|
|
595
|
-
if (nitroConfig.prerender?.routes?.length &&
|
|
596
|
-
options?.prerender?.sitemap) {
|
|
597
|
-
console.log('Building Sitemap...');
|
|
598
|
-
// sitemap needs to be built after all directories are built
|
|
599
|
-
await buildSitemap(config, options.prerender.sitemap, nitroConfig.prerender.routes, clientOutputPath, routeSitemaps);
|
|
600
|
-
}
|
|
601
|
-
applySsrEntryAlias(nitroConfig, options, workspaceRoot, rootDir);
|
|
602
|
-
await buildServer(options, nitroConfig, routeSourceFiles);
|
|
603
|
-
console.log(`\n\nThe '@analogjs/platform' server has been successfully built.`);
|
|
604
|
-
}
|
|
605
|
-
},
|
|
606
|
-
},
|
|
607
|
-
{
|
|
608
|
-
name: '@analogjs/vite-plugin-nitro-api-prefix',
|
|
609
|
-
config() {
|
|
610
|
-
return {
|
|
611
|
-
define: {
|
|
612
|
-
ANALOG_API_PREFIX: `"${baseURL.substring(1)}${apiPrefix.substring(1)}"`,
|
|
613
|
-
},
|
|
614
|
-
};
|
|
615
|
-
},
|
|
616
|
-
},
|
|
617
|
-
];
|
|
143
|
+
function nitro(options, nitroOptions) {
|
|
144
|
+
const workspaceRoot = options?.workspaceRoot ?? process.cwd();
|
|
145
|
+
const sourceRoot = options?.sourceRoot ?? "src";
|
|
146
|
+
let isTest = process.env.NODE_ENV === "test" || !!process.env["VITEST"];
|
|
147
|
+
const baseURL = process.env["NITRO_APP_BASE_URL"] || "";
|
|
148
|
+
const prefix = baseURL ? baseURL.substring(0, baseURL.length - 1) : "";
|
|
149
|
+
const apiPrefix = `/${options?.apiPrefix || "api"}`;
|
|
150
|
+
const useAPIMiddleware = typeof options?.useAPIMiddleware !== "undefined" ? options?.useAPIMiddleware : true;
|
|
151
|
+
const viteRolldownOutput = options?.vite?.build?.rolldownOptions?.output;
|
|
152
|
+
const viteRolldownOutputConfig = viteRolldownOutput && !Array.isArray(viteRolldownOutput) ? viteRolldownOutput : void 0;
|
|
153
|
+
const codeSplitting = viteRolldownOutputConfig?.codeSplitting;
|
|
154
|
+
let isBuild = false;
|
|
155
|
+
let isServe = false;
|
|
156
|
+
let ssrBuild = false;
|
|
157
|
+
let config;
|
|
158
|
+
let nitroConfig;
|
|
159
|
+
let environmentBuild = false;
|
|
160
|
+
let hasAPIDir = false;
|
|
161
|
+
let clientOutputPath = "";
|
|
162
|
+
let rendererIndexEntry = "";
|
|
163
|
+
const rollupExternalEntries = [];
|
|
164
|
+
const routeSitemaps = {};
|
|
165
|
+
const routeSourceFiles = {};
|
|
166
|
+
let rootDir = workspaceRoot;
|
|
167
|
+
return [
|
|
168
|
+
options?.ssr ? devServerPlugin({
|
|
169
|
+
entryServer: options?.entryServer,
|
|
170
|
+
index: options?.index,
|
|
171
|
+
routeRules: nitroOptions?.routeRules
|
|
172
|
+
}) : false,
|
|
173
|
+
{
|
|
174
|
+
name: "@analogjs/vite-plugin-nitro",
|
|
175
|
+
async config(userConfig, { mode, command }) {
|
|
176
|
+
isServe = command === "serve";
|
|
177
|
+
isBuild = command === "build";
|
|
178
|
+
ssrBuild = userConfig.build?.ssr === true;
|
|
179
|
+
config = userConfig;
|
|
180
|
+
isTest = isTest ? isTest : mode === "test";
|
|
181
|
+
rootDir = relative(workspaceRoot, config.root || ".") || ".";
|
|
182
|
+
hasAPIDir = existsSync(resolve(workspaceRoot, rootDir, `${sourceRoot}/server/routes/${options?.apiPrefix || "api"}`));
|
|
183
|
+
const buildPreset = process.env["BUILD_PRESET"] ?? nitroOptions?.preset ?? (process.env["VERCEL"] ? "vercel" : void 0);
|
|
184
|
+
const pageHandlers = getPageHandlers({
|
|
185
|
+
workspaceRoot,
|
|
186
|
+
sourceRoot,
|
|
187
|
+
rootDir,
|
|
188
|
+
additionalPagesDirs: options?.additionalPagesDirs,
|
|
189
|
+
hasAPIDir
|
|
190
|
+
});
|
|
191
|
+
const resolvedClientOutputPath = resolveClientOutputPath(clientOutputPath, workspaceRoot, rootDir, config.build?.outDir, ssrBuild);
|
|
192
|
+
rendererIndexEntry = normalizePath(resolve(resolvedClientOutputPath, "index.html"));
|
|
193
|
+
nitroConfig = {
|
|
194
|
+
rootDir: normalizePath(rootDir),
|
|
195
|
+
preset: buildPreset,
|
|
196
|
+
compatibilityDate: "2025-11-19",
|
|
197
|
+
logLevel: nitroOptions?.logLevel || 0,
|
|
198
|
+
serverDir: normalizePath(`${sourceRoot}/server`),
|
|
199
|
+
scanDirs: [normalizePath(`${rootDir}/${sourceRoot}/server`), ...(options?.additionalAPIDirs || []).map((dir) => normalizePath(`${workspaceRoot}${dir}`))],
|
|
200
|
+
output: {
|
|
201
|
+
dir: normalizePath(resolve(workspaceRoot, "dist", rootDir, "analog")),
|
|
202
|
+
publicDir: normalizePath(resolve(workspaceRoot, "dist", rootDir, "analog/public"))
|
|
203
|
+
},
|
|
204
|
+
buildDir: normalizePath(resolve(workspaceRoot, "dist", rootDir, ".nitro")),
|
|
205
|
+
typescript: { generateTsConfig: false },
|
|
206
|
+
runtimeConfig: {
|
|
207
|
+
apiPrefix: apiPrefix.substring(1),
|
|
208
|
+
prefix
|
|
209
|
+
},
|
|
210
|
+
renderer: false,
|
|
211
|
+
imports: { autoImport: false },
|
|
212
|
+
hooks: { "rollup:before": createRollupBeforeHook(rollupExternalEntries) },
|
|
213
|
+
rollupConfig: {
|
|
214
|
+
onwarn(warning) {
|
|
215
|
+
if (warning.message.includes("empty chunk") && warning.message.endsWith(".server")) return;
|
|
216
|
+
},
|
|
217
|
+
plugins: [pageEndpointsPlugin()]
|
|
218
|
+
},
|
|
219
|
+
handlers: [...hasAPIDir ? [] : useAPIMiddleware ? [createNitroMiddlewareHandler("#ANALOG_API_MIDDLEWARE")] : [], ...pageHandlers],
|
|
220
|
+
routeRules: hasAPIDir ? void 0 : useAPIMiddleware ? void 0 : { [`${prefix}${apiPrefix}/**`]: { proxy: { to: "/**" } } },
|
|
221
|
+
virtual: {
|
|
222
|
+
"#ANALOG_SSR_RENDERER": ssrRenderer(rendererIndexEntry),
|
|
223
|
+
"#ANALOG_CLIENT_RENDERER": clientRenderer(rendererIndexEntry),
|
|
224
|
+
...hasAPIDir ? {} : { "#ANALOG_API_MIDDLEWARE": apiMiddleware }
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
if (isVercelPreset(buildPreset)) nitroConfig = withVercelOutputAPI(nitroConfig, workspaceRoot);
|
|
228
|
+
if (isCloudflarePreset(buildPreset)) nitroConfig = withCloudflareOutput(nitroConfig);
|
|
229
|
+
if (isNetlifyPreset(buildPreset) && rootDir === "." && !existsSync(resolve(workspaceRoot, "netlify.toml"))) nitroConfig = withNetlifyOutputAPI(nitroConfig, workspaceRoot);
|
|
230
|
+
if (isFirebaseAppHosting()) nitroConfig = withAppHostingOutput(nitroConfig);
|
|
231
|
+
if (!ssrBuild && !isTest) clientOutputPath = resolvedClientOutputPath;
|
|
232
|
+
if (isBuild) {
|
|
233
|
+
nitroConfig.publicAssets = [{
|
|
234
|
+
dir: normalizePath(clientOutputPath),
|
|
235
|
+
maxAge: 0
|
|
236
|
+
}];
|
|
237
|
+
const rendererHandler = options?.ssr ? "#ANALOG_SSR_RENDERER" : "#ANALOG_CLIENT_RENDERER";
|
|
238
|
+
nitroConfig.handlers = [...nitroConfig.handlers || [], {
|
|
239
|
+
handler: rendererHandler,
|
|
240
|
+
route: "/**",
|
|
241
|
+
lazy: true
|
|
242
|
+
}];
|
|
243
|
+
if (isEmptyPrerenderRoutes(options)) {
|
|
244
|
+
nitroConfig.prerender = {};
|
|
245
|
+
nitroConfig.prerender.routes = ["/"];
|
|
246
|
+
}
|
|
247
|
+
if (options?.prerender) {
|
|
248
|
+
nitroConfig.prerender = nitroConfig.prerender ?? {};
|
|
249
|
+
nitroConfig.prerender.crawlLinks = options?.prerender?.discover;
|
|
250
|
+
let routes = [];
|
|
251
|
+
const prerenderRoutes = options?.prerender?.routes;
|
|
252
|
+
if (isArrayWithElements(prerenderRoutes)) routes = prerenderRoutes;
|
|
253
|
+
else if (typeof prerenderRoutes === "function") routes = await prerenderRoutes();
|
|
254
|
+
nitroConfig.prerender.routes = routes.reduce((prev, current) => {
|
|
255
|
+
if (!current) return prev;
|
|
256
|
+
if (typeof current === "string") {
|
|
257
|
+
prev.push(current);
|
|
258
|
+
return prev;
|
|
259
|
+
}
|
|
260
|
+
if ("route" in current) {
|
|
261
|
+
if (current.sitemap) routeSitemaps[current.route] = current.sitemap;
|
|
262
|
+
if (current.outputSourceFile) {
|
|
263
|
+
const sourcePath = resolve(workspaceRoot, rootDir, current.outputSourceFile);
|
|
264
|
+
routeSourceFiles[current.route] = readFileSync(sourcePath, "utf8");
|
|
265
|
+
}
|
|
266
|
+
prev.push(current.route);
|
|
267
|
+
if ("staticData" in current) prev.push(`${apiPrefix}/_analog/pages/${current.route}`);
|
|
268
|
+
return prev;
|
|
269
|
+
}
|
|
270
|
+
getMatchingContentFilesWithFrontMatter(workspaceRoot, rootDir, current.contentDir).forEach((f) => {
|
|
271
|
+
const result = current.transform(f);
|
|
272
|
+
if (result) {
|
|
273
|
+
if (current.sitemap) routeSitemaps[result] = current.sitemap && typeof current.sitemap === "function" ? current.sitemap?.(f) : current.sitemap;
|
|
274
|
+
if (current.outputSourceFile) {
|
|
275
|
+
const sourceContent = current.outputSourceFile(f);
|
|
276
|
+
if (sourceContent) routeSourceFiles[result] = sourceContent;
|
|
277
|
+
}
|
|
278
|
+
prev.push(result);
|
|
279
|
+
if ("staticData" in current) prev.push(`${apiPrefix}/_analog/pages/${result}`);
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
return prev;
|
|
283
|
+
}, []);
|
|
284
|
+
}
|
|
285
|
+
if (ssrBuild || options?.ssr || nitroConfig.prerender?.routes?.length) {
|
|
286
|
+
if (process.platform === "win32") nitroConfig.noExternals = appendNoExternals(nitroConfig.noExternals, "std-env");
|
|
287
|
+
rollupExternalEntries.push("rxjs", "node-fetch-native/dist/polyfill");
|
|
288
|
+
nitroConfig = {
|
|
289
|
+
...nitroConfig,
|
|
290
|
+
moduleSideEffects: ["zone.js/node", "zone.js/fesm2015/zone-node"],
|
|
291
|
+
handlers: [
|
|
292
|
+
...hasAPIDir ? [] : useAPIMiddleware ? [createNitroMiddlewareHandler("#ANALOG_API_MIDDLEWARE")] : [],
|
|
293
|
+
...pageHandlers,
|
|
294
|
+
{
|
|
295
|
+
handler: rendererHandler,
|
|
296
|
+
route: "/**",
|
|
297
|
+
lazy: true
|
|
298
|
+
}
|
|
299
|
+
]
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
nitroConfig = mergeConfig(nitroConfig, nitroOptions);
|
|
304
|
+
return {
|
|
305
|
+
environments: {
|
|
306
|
+
client: { build: {
|
|
307
|
+
outDir: config?.build?.outDir || resolve(workspaceRoot, "dist", rootDir, "client"),
|
|
308
|
+
...isRolldown() && codeSplitting !== void 0 ? { rolldownOptions: { output: {
|
|
309
|
+
...viteRolldownOutputConfig,
|
|
310
|
+
codeSplitting
|
|
311
|
+
} } } : {}
|
|
312
|
+
} },
|
|
313
|
+
ssr: { build: {
|
|
314
|
+
ssr: true,
|
|
315
|
+
[getBundleOptionsKey()]: { input: options?.entryServer || resolve(workspaceRoot, rootDir, `${sourceRoot}/main.server.ts`) },
|
|
316
|
+
outDir: options?.ssrBuildDir || resolve(workspaceRoot, "dist", rootDir, "ssr")
|
|
317
|
+
} }
|
|
318
|
+
},
|
|
319
|
+
builder: {
|
|
320
|
+
sharedPlugins: true,
|
|
321
|
+
buildApp: async (builder) => {
|
|
322
|
+
environmentBuild = true;
|
|
323
|
+
const builds = [builder.build(builder.environments["client"])];
|
|
324
|
+
if (options?.ssr || nitroConfig.prerender?.routes?.length) builds.push(builder.build(builder.environments["ssr"]));
|
|
325
|
+
await Promise.all(builds);
|
|
326
|
+
applySsrEntryAlias(nitroConfig, options, workspaceRoot, rootDir);
|
|
327
|
+
await buildServer(options, nitroConfig, routeSourceFiles);
|
|
328
|
+
if (nitroConfig.prerender?.routes?.length && options?.prerender?.sitemap) {
|
|
329
|
+
const publicDir = nitroConfig.output?.publicDir;
|
|
330
|
+
if (!publicDir) throw new Error("Nitro public output directory is required to build the sitemap.");
|
|
331
|
+
console.log("Building Sitemap...");
|
|
332
|
+
await buildSitemap(config, options.prerender.sitemap, nitroConfig.prerender.routes, publicDir, routeSitemaps);
|
|
333
|
+
}
|
|
334
|
+
console.log(`\n\nThe '@analogjs/platform' server has been successfully built.`);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
},
|
|
339
|
+
async configureServer(viteServer) {
|
|
340
|
+
if (isServe && !isTest) {
|
|
341
|
+
const nitro = await createNitro({
|
|
342
|
+
dev: true,
|
|
343
|
+
builder: "rollup",
|
|
344
|
+
...nitroConfig
|
|
345
|
+
});
|
|
346
|
+
const server = createDevServer(nitro);
|
|
347
|
+
await build(nitro);
|
|
348
|
+
const apiHandler = async (req, res) => {
|
|
349
|
+
await writeWebResponseToNode(res, await server.fetch(toWebRequest(req)));
|
|
350
|
+
};
|
|
351
|
+
if (hasAPIDir) viteServer.middlewares.use((req, res, next) => {
|
|
352
|
+
if (req.url?.startsWith(`${prefix}${apiPrefix}`)) {
|
|
353
|
+
apiHandler(req, res).catch((error) => next(error));
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
next();
|
|
357
|
+
});
|
|
358
|
+
else viteServer.middlewares.use(apiPrefix, (req, res, next) => {
|
|
359
|
+
apiHandler(req, res).catch((error) => next(error));
|
|
360
|
+
});
|
|
361
|
+
viteServer.httpServer?.once("listening", () => {
|
|
362
|
+
process.env["ANALOG_HOST"] = !viteServer.config.server.host ? "localhost" : viteServer.config.server.host;
|
|
363
|
+
process.env["ANALOG_PORT"] = `${viteServer.config.server.port}`;
|
|
364
|
+
});
|
|
365
|
+
if (nitroOptions?.experimental?.websocket) viteServer.httpServer?.on("upgrade", server.upgrade);
|
|
366
|
+
console.log(`\n\nThe server endpoints are accessible under the "${prefix}${apiPrefix}" path.`);
|
|
367
|
+
}
|
|
368
|
+
},
|
|
369
|
+
async closeBundle() {
|
|
370
|
+
if (environmentBuild) return;
|
|
371
|
+
if (ssrBuild) return;
|
|
372
|
+
if (isBuild) {
|
|
373
|
+
if (options?.ssr) {
|
|
374
|
+
console.log("Building SSR application...");
|
|
375
|
+
await buildSSRApp(config, options);
|
|
376
|
+
}
|
|
377
|
+
if (nitroConfig.prerender?.routes?.length && options?.prerender?.sitemap) {
|
|
378
|
+
console.log("Building Sitemap...");
|
|
379
|
+
await buildSitemap(config, options.prerender.sitemap, nitroConfig.prerender.routes, clientOutputPath, routeSitemaps);
|
|
380
|
+
}
|
|
381
|
+
applySsrEntryAlias(nitroConfig, options, workspaceRoot, rootDir);
|
|
382
|
+
await buildServer(options, nitroConfig, routeSourceFiles);
|
|
383
|
+
console.log(`\n\nThe '@analogjs/platform' server has been successfully built.`);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
},
|
|
387
|
+
{
|
|
388
|
+
name: "@analogjs/vite-plugin-nitro-api-prefix",
|
|
389
|
+
config() {
|
|
390
|
+
return { define: { ANALOG_API_PREFIX: `"${baseURL.substring(1)}${apiPrefix.substring(1)}"` } };
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
];
|
|
618
394
|
}
|
|
619
395
|
function isEmptyPrerenderRoutes(options) {
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
}
|
|
623
|
-
return !options.prerender?.routes;
|
|
396
|
+
if (!options || isArrayWithElements(options?.prerender?.routes)) return false;
|
|
397
|
+
return !options.prerender?.routes;
|
|
624
398
|
}
|
|
625
399
|
function isArrayWithElements(arr) {
|
|
626
|
-
|
|
400
|
+
return !!(Array.isArray(arr) && arr.length);
|
|
627
401
|
}
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
...nitroConfig?.output,
|
|
645
|
-
dir: normalizePath(resolve(workspaceRoot, '.vercel', 'output')),
|
|
646
|
-
publicDir: normalizePath(resolve(workspaceRoot, '.vercel', 'output/static')),
|
|
647
|
-
},
|
|
402
|
+
var withVercelOutputAPI = (nitroConfig, workspaceRoot) => ({
|
|
403
|
+
...nitroConfig,
|
|
404
|
+
preset: nitroConfig?.preset ?? "vercel",
|
|
405
|
+
vercel: {
|
|
406
|
+
...nitroConfig?.vercel,
|
|
407
|
+
entryFormat: nitroConfig?.vercel?.entryFormat ?? "node",
|
|
408
|
+
functions: {
|
|
409
|
+
runtime: nitroConfig?.vercel?.functions?.runtime ?? "nodejs24.x",
|
|
410
|
+
...nitroConfig?.vercel?.functions
|
|
411
|
+
}
|
|
412
|
+
},
|
|
413
|
+
output: {
|
|
414
|
+
...nitroConfig?.output,
|
|
415
|
+
dir: normalizePath(resolve(workspaceRoot, ".vercel", "output")),
|
|
416
|
+
publicDir: normalizePath(resolve(workspaceRoot, ".vercel", "output/static"))
|
|
417
|
+
}
|
|
648
418
|
});
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
...nitroConfig,
|
|
657
|
-
output: {
|
|
658
|
-
...nitroConfig?.output,
|
|
659
|
-
serverDir: '{{ output.publicDir }}/_worker.js',
|
|
660
|
-
},
|
|
419
|
+
var isCloudflarePreset = (buildPreset) => process.env["CF_PAGES"] || buildPreset && (buildPreset.toLowerCase().includes("cloudflare-pages") || buildPreset.toLowerCase().includes("cloudflare_pages"));
|
|
420
|
+
var withCloudflareOutput = (nitroConfig) => ({
|
|
421
|
+
...nitroConfig,
|
|
422
|
+
output: {
|
|
423
|
+
...nitroConfig?.output,
|
|
424
|
+
serverDir: "{{ output.publicDir }}/_worker.js"
|
|
425
|
+
}
|
|
661
426
|
});
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
};
|
|
427
|
+
var isFirebaseAppHosting = () => !!process.env["NG_BUILD_LOGS_JSON"];
|
|
428
|
+
var withAppHostingOutput = (nitroConfig) => {
|
|
429
|
+
let hasOutput = false;
|
|
430
|
+
return {
|
|
431
|
+
...nitroConfig,
|
|
432
|
+
serveStatic: true,
|
|
433
|
+
rollupConfig: {
|
|
434
|
+
...nitroConfig.rollupConfig,
|
|
435
|
+
output: {
|
|
436
|
+
...nitroConfig.rollupConfig?.output,
|
|
437
|
+
entryFileNames: "server.mjs"
|
|
438
|
+
}
|
|
439
|
+
},
|
|
440
|
+
hooks: {
|
|
441
|
+
...nitroConfig.hooks,
|
|
442
|
+
compiled: () => {
|
|
443
|
+
if (!hasOutput) {
|
|
444
|
+
const buildOutput = {
|
|
445
|
+
errors: [],
|
|
446
|
+
warnings: [],
|
|
447
|
+
outputPaths: {
|
|
448
|
+
root: pathToFileURL(`${nitroConfig.output?.dir}`),
|
|
449
|
+
browser: pathToFileURL(`${nitroConfig.output?.publicDir}`),
|
|
450
|
+
server: pathToFileURL(`${nitroConfig.output?.dir}/server`)
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
console.log(JSON.stringify(buildOutput, null, 2));
|
|
454
|
+
hasOutput = true;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
};
|
|
695
459
|
};
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
},
|
|
460
|
+
var isNetlifyPreset = (buildPreset) => process.env["NETLIFY"] || buildPreset && buildPreset.toLowerCase().includes("netlify");
|
|
461
|
+
var withNetlifyOutputAPI = (nitroConfig, workspaceRoot) => ({
|
|
462
|
+
...nitroConfig,
|
|
463
|
+
output: {
|
|
464
|
+
...nitroConfig?.output,
|
|
465
|
+
dir: normalizePath(resolve(workspaceRoot, "netlify/functions"))
|
|
466
|
+
}
|
|
704
467
|
});
|
|
468
|
+
//#endregion
|
|
469
|
+
export { nitro };
|
|
470
|
+
|
|
705
471
|
//# sourceMappingURL=vite-plugin-nitro.js.map
|