nitro-nightly 3.0.1-20251217-214519-a335d7df → 3.0.1-20251217-223324-2058e206
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/_build/rolldown.mjs +2 -2
- package/dist/_build/rollup.mjs +2 -2
- package/dist/_build/shared3.mjs +1125 -55
- package/dist/_build/shared4.mjs +59 -0
- package/dist/_build/vite.build.mjs +1 -1
- package/dist/_build/vite.plugin.mjs +3 -48
- package/dist/types/index.d.mts +10 -47
- package/dist/vite.d.mts +1 -5
- package/dist/vite.mjs +2 -1
- package/lib/vite.d.mts +26 -0
- package/package.json +4 -3
- package/dist/_nitro3.mjs +0 -1158
- /package/lib/{deps/h3.d.mts → h3.d.mts} +0 -0
- /package/lib/{deps/h3.mjs → h3.mjs} +0 -0
package/dist/_build/shared3.mjs
CHANGED
|
@@ -1,59 +1,1129 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { i as __toESM } from "../_rolldown.mjs";
|
|
2
|
+
import { C as isAbsolute, O as relative, T as normalize, h as resolveModulePath, k as resolve, w as join } from "../_libs/c12.mjs";
|
|
3
|
+
import { i as unplugin } from "../_libs/unimport.mjs";
|
|
2
4
|
import { t as glob } from "../_libs/tinyglobby.mjs";
|
|
3
|
-
import { i as a } from "../_libs/std-env.mjs";
|
|
4
|
-
import { t as
|
|
5
|
-
import { t as
|
|
6
|
-
import { t as
|
|
5
|
+
import { i as a, r as T, t as A } from "../_libs/std-env.mjs";
|
|
6
|
+
import { t as src_default } from "../_libs/mime.mjs";
|
|
7
|
+
import { i as genSafeVariableName, t as genImport } from "../_libs/knitwork.mjs";
|
|
8
|
+
import { t as require_etag } from "../_libs/etag.mjs";
|
|
9
|
+
import { t as replace } from "../_libs/@rollup/plugin-replace.mjs";
|
|
10
|
+
import { t as unwasm } from "../_libs/unwasm.mjs";
|
|
11
|
+
import { builtinModules, createRequire } from "node:module";
|
|
12
|
+
import { consola } from "consola";
|
|
13
|
+
import { camelCase } from "scule";
|
|
7
14
|
import { promises } from "node:fs";
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
import { joinURL, withTrailingSlash } from "ufo";
|
|
16
|
+
import { pathToFileURL } from "node:url";
|
|
17
|
+
import { readFile } from "node:fs/promises";
|
|
18
|
+
import { pkgDir, presetsDir, runtimeDependencies, runtimeDir } from "nitro/meta";
|
|
19
|
+
import { hash } from "ohash";
|
|
20
|
+
import { defineEnv } from "unenv";
|
|
21
|
+
import { connectors } from "db0";
|
|
22
|
+
import { RENDER_CONTEXT_KEYS, compileTemplateToString, hasTemplateSyntax } from "rendu";
|
|
23
|
+
import { builtinDrivers, normalizeKey } from "unstorage";
|
|
24
|
+
import { transformSync } from "oxc-transform";
|
|
25
|
+
|
|
26
|
+
//#region src/utils/regex.ts
|
|
27
|
+
function escapeRegExp(string) {
|
|
28
|
+
return string.replace(/[-\\^$*+?.()|[\]{}]/g, String.raw`\$&`);
|
|
29
|
+
}
|
|
30
|
+
function pathRegExp(string) {
|
|
31
|
+
if (A) string = string.replace(/\\/g, "/");
|
|
32
|
+
let escaped = escapeRegExp(string);
|
|
33
|
+
if (A) escaped = escaped.replace(/\//g, String.raw`[/\\]`);
|
|
34
|
+
return escaped;
|
|
35
|
+
}
|
|
36
|
+
function toPathRegExp(input) {
|
|
37
|
+
if (input instanceof RegExp) return input;
|
|
38
|
+
if (typeof input === "string") return new RegExp(pathRegExp(input));
|
|
39
|
+
throw new TypeError("Expected a string or RegExp", { cause: input });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/build/config.ts
|
|
44
|
+
function baseBuildConfig(nitro) {
|
|
45
|
+
const extensions = [
|
|
46
|
+
".ts",
|
|
47
|
+
".mjs",
|
|
48
|
+
".js",
|
|
49
|
+
".json",
|
|
50
|
+
".node",
|
|
51
|
+
".tsx",
|
|
52
|
+
".jsx"
|
|
53
|
+
];
|
|
54
|
+
const isNodeless = nitro.options.node === false;
|
|
55
|
+
const importMetaInjections = {
|
|
56
|
+
dev: nitro.options.dev,
|
|
57
|
+
preset: nitro.options.preset,
|
|
58
|
+
prerender: nitro.options.preset === "nitro-prerender",
|
|
59
|
+
nitro: true,
|
|
60
|
+
server: true,
|
|
61
|
+
client: false,
|
|
62
|
+
baseURL: nitro.options.baseURL,
|
|
63
|
+
_asyncContext: nitro.options.experimental.asyncContext,
|
|
64
|
+
_tasks: nitro.options.experimental.tasks
|
|
65
|
+
};
|
|
66
|
+
const replacements = {
|
|
67
|
+
...Object.fromEntries(Object.entries(importMetaInjections).map(([key, val]) => [`import.meta.${key}`, JSON.stringify(val)])),
|
|
68
|
+
...nitro.options.replace
|
|
69
|
+
};
|
|
70
|
+
const { env } = defineEnv({
|
|
71
|
+
nodeCompat: isNodeless,
|
|
72
|
+
resolve: true,
|
|
73
|
+
presets: nitro.options.unenv,
|
|
74
|
+
overrides: { alias: nitro.options.alias }
|
|
16
75
|
});
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
76
|
+
return {
|
|
77
|
+
extensions,
|
|
78
|
+
isNodeless,
|
|
79
|
+
replacements,
|
|
80
|
+
env,
|
|
81
|
+
aliases: resolveAliases({ ...env.alias }),
|
|
82
|
+
noExternal: getNoExternals(nitro),
|
|
83
|
+
ignoreWarningCodes: new Set([
|
|
84
|
+
"EVAL",
|
|
85
|
+
"CIRCULAR_DEPENDENCY",
|
|
86
|
+
"THIS_IS_UNDEFINED",
|
|
87
|
+
"EMPTY_BUNDLE"
|
|
88
|
+
])
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
function getNoExternals(nitro) {
|
|
92
|
+
const noExternal = [
|
|
93
|
+
/\.[mc]?tsx?$/,
|
|
94
|
+
/^(?:[\0#~.]|virtual:)/,
|
|
95
|
+
/* @__PURE__ */ new RegExp("^" + pathRegExp(pkgDir) + "(?!.*node_modules)"),
|
|
96
|
+
...[nitro.options.rootDir, ...nitro.options.scanDirs.filter((dir) => dir.includes("node_modules") || !dir.startsWith(nitro.options.rootDir))].map((dir) => /* @__PURE__ */ new RegExp("^" + pathRegExp(dir) + "(?!.*node_modules)"))
|
|
97
|
+
];
|
|
98
|
+
if (nitro.options.wasm !== false) noExternal.push(/\.wasm$/);
|
|
99
|
+
if (Array.isArray(nitro.options.noExternals)) noExternal.push(...nitro.options.noExternals.filter(Boolean).map((item) => toPathRegExp(item)));
|
|
100
|
+
return noExternal.sort((a$1, b) => a$1.source.length - b.source.length);
|
|
101
|
+
}
|
|
102
|
+
function resolveAliases(_aliases) {
|
|
103
|
+
const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a$1], [b]) => b.split("/").length - a$1.split("/").length || b.length - a$1.length));
|
|
104
|
+
for (const key in aliases) for (const alias in aliases) {
|
|
105
|
+
if (![
|
|
106
|
+
"~",
|
|
107
|
+
"@",
|
|
108
|
+
"#"
|
|
109
|
+
].includes(alias[0])) continue;
|
|
110
|
+
if (alias === "@" && !aliases[key].startsWith("@/")) continue;
|
|
111
|
+
if (aliases[key].startsWith(alias)) aliases[key] = aliases[alias] + aliases[key].slice(alias.length);
|
|
112
|
+
}
|
|
113
|
+
return aliases;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/build/chunks.ts
|
|
118
|
+
const virtualRe = /^\0|^virtual:/;
|
|
119
|
+
const NODE_MODULES_RE$1 = /node_modules[/\\][^.]/;
|
|
120
|
+
function libChunkName(id) {
|
|
121
|
+
return `_libs/${id.match(/.*(?:[/\\])node_modules(?:[/\\])(?<package>@[^/\\]+[/\\][^/\\]+|[^/\\.][^/\\]*)/)?.groups?.package || "common"}`;
|
|
122
|
+
}
|
|
123
|
+
function getChunkName(chunk, nitro) {
|
|
124
|
+
if (chunk.name.startsWith("_libs/")) return `${chunk.name}.mjs`;
|
|
125
|
+
if (chunk.name === "rolldown-runtime") return "_runtime/rolldown.mjs";
|
|
126
|
+
if (chunk.moduleIds.length === 0) return `_chunks/${chunk.name}.mjs`;
|
|
127
|
+
const ids = chunk.moduleIds.filter((id) => !virtualRe.test(id));
|
|
128
|
+
if (ids.length === 0) {
|
|
129
|
+
if (chunk.moduleIds.every((id) => id.includes("virtual:raw"))) return `_raw/[name].mjs`;
|
|
130
|
+
return `_virtual/[name].mjs`;
|
|
131
|
+
}
|
|
132
|
+
if (ids.every((id) => id.endsWith(".wasm"))) return `_wasm/[name].mjs`;
|
|
133
|
+
if (ids.every((id) => id.includes("vite/services"))) return `_ssr/[name].mjs`;
|
|
134
|
+
if (ids.every((id) => id.startsWith(nitro.options.buildDir))) return `_build/[name].mjs`;
|
|
135
|
+
if (ids.every((id) => id.startsWith(runtimeDir) || id.startsWith(presetsDir))) return `_runtime/[name].mjs`;
|
|
136
|
+
const mainId = ids.at(-1);
|
|
137
|
+
if (mainId) {
|
|
138
|
+
const routeHandler = nitro.routing.routes.routes.flatMap((h) => h.data).find((h) => h.handler === mainId);
|
|
139
|
+
if (routeHandler?.route) return `_routes/${routeToFsPath(routeHandler.route)}.mjs`;
|
|
140
|
+
if (Object.entries(nitro.options.tasks).find(([_, task]) => task.handler === mainId)) return `_tasks/[name].mjs`;
|
|
141
|
+
}
|
|
142
|
+
return `_chunks/[name].mjs`;
|
|
143
|
+
}
|
|
144
|
+
function routeToFsPath(route) {
|
|
145
|
+
return route.split("/").slice(1).map((s) => `${s.replace(/[:*]+/g, "$").replace(/[^$a-zA-Z0-9_.[\]/]/g, "_")}`).join("/") || "index";
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region src/build/virtual/database.ts
|
|
150
|
+
function database(nitro) {
|
|
151
|
+
return {
|
|
152
|
+
id: "#nitro/virtual/database",
|
|
153
|
+
template: () => {
|
|
154
|
+
if (!nitro.options.experimental.database) return `export const connectionConfigs = {};`;
|
|
155
|
+
const dbConfigs = nitro.options.dev && nitro.options.devDatabase || nitro.options.database;
|
|
156
|
+
const connectorsNames = [...new Set(Object.values(dbConfigs || {}).map((config) => config?.connector))].filter(Boolean);
|
|
157
|
+
for (const name of connectorsNames) if (!connectors[name]) throw new Error(`Database connector "${name}" is invalid.`);
|
|
158
|
+
return `
|
|
159
|
+
${connectorsNames.map((name) => `import ${camelCase(name)}Connector from "${connectors[name]}";`).join("\n")}
|
|
160
|
+
|
|
161
|
+
export const connectionConfigs = {
|
|
162
|
+
${Object.entries(dbConfigs || {}).filter(([, config]) => !!config?.connector).map(([name, { connector, options }]) => `${name}: {
|
|
163
|
+
connector: ${camelCase(connector)}Connector,
|
|
164
|
+
options: ${JSON.stringify(options)}
|
|
165
|
+
}`).join(",\n")}
|
|
166
|
+
};
|
|
167
|
+
`;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/build/virtual/error-handler.ts
|
|
174
|
+
function errorHandler(nitro) {
|
|
175
|
+
return {
|
|
176
|
+
id: "#nitro/virtual/error-handler",
|
|
177
|
+
template: () => {
|
|
178
|
+
const errorHandlers = Array.isArray(nitro.options.errorHandler) ? nitro.options.errorHandler : [nitro.options.errorHandler];
|
|
179
|
+
const builtinHandler = join(runtimeDir, `internal/error/${nitro.options.dev ? "dev" : "prod"}`);
|
|
180
|
+
return `
|
|
181
|
+
${errorHandlers.map((h, i) => `import errorHandler$${i} from "${h}";`).join("\n")}
|
|
182
|
+
|
|
183
|
+
const errorHandlers = [${errorHandlers.map((_, i) => `errorHandler$${i}`).join(", ")}];
|
|
184
|
+
|
|
185
|
+
import { defaultHandler } from "${builtinHandler}";
|
|
186
|
+
|
|
187
|
+
export default async function(error, event) {
|
|
188
|
+
for (const handler of errorHandlers) {
|
|
189
|
+
try {
|
|
190
|
+
const response = await handler(error, event, { defaultHandler });
|
|
191
|
+
if (response) {
|
|
192
|
+
return response;
|
|
193
|
+
}
|
|
194
|
+
} catch(error) {
|
|
195
|
+
// Handler itself thrown, log and continue
|
|
196
|
+
console.error(error);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
// H3 will handle fallback
|
|
200
|
+
}
|
|
201
|
+
`;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
//#endregion
|
|
207
|
+
//#region src/build/virtual/feature-flags.ts
|
|
208
|
+
function featureFlags(nitro) {
|
|
209
|
+
return {
|
|
210
|
+
id: "#nitro/virtual/feature-flags",
|
|
211
|
+
template: () => {
|
|
212
|
+
const featureFlags$1 = {
|
|
213
|
+
hasRoutes: nitro.routing.routes.hasRoutes(),
|
|
214
|
+
hasRouteRules: nitro.routing.routeRules.hasRoutes(),
|
|
215
|
+
hasRoutedMiddleware: nitro.routing.routedMiddleware.hasRoutes(),
|
|
216
|
+
hasGlobalMiddleware: nitro.routing.globalMiddleware.length > 0,
|
|
217
|
+
hasPlugins: nitro.options.plugins.length > 0,
|
|
218
|
+
hasHooks: nitro.options.features?.runtimeHooks ?? nitro.options.plugins.length > 0,
|
|
219
|
+
hasWebSocket: nitro.options.features?.websocket ?? nitro.options.experimental.websocket ?? false
|
|
220
|
+
};
|
|
221
|
+
return Object.entries(featureFlags$1).map(([key, value]) => `export const ${key} = ${Boolean(value)};`).join("\n");
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
//#endregion
|
|
227
|
+
//#region src/build/virtual/plugins.ts
|
|
228
|
+
function plugins(nitro) {
|
|
229
|
+
return {
|
|
230
|
+
id: "#nitro/virtual/plugins",
|
|
231
|
+
template: () => {
|
|
232
|
+
const nitroPlugins = [...new Set(nitro.options.plugins)];
|
|
233
|
+
return `
|
|
234
|
+
${nitroPlugins.map((plugin) => `import _${hash(plugin).replace(/-/g, "")} from "${plugin}";`).join("\n")}
|
|
235
|
+
|
|
236
|
+
export const plugins = [
|
|
237
|
+
${nitroPlugins.map((plugin) => `_${hash(plugin).replace(/-/g, "")}`).join(",\n")}
|
|
238
|
+
]
|
|
239
|
+
`;
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
//#endregion
|
|
245
|
+
//#region src/build/virtual/polyfills.ts
|
|
246
|
+
function polyfills(_nitro, polyfills$1) {
|
|
247
|
+
return {
|
|
248
|
+
id: "#nitro/virtual/polyfills",
|
|
249
|
+
moduleSideEffects: true,
|
|
250
|
+
template: () => {
|
|
251
|
+
return polyfills$1.map((p) => `import '${p}';`).join("\n") || `/* No polyfills */`;
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
//#endregion
|
|
257
|
+
//#region src/build/virtual/public-assets.ts
|
|
258
|
+
var import_etag$1 = /* @__PURE__ */ __toESM(require_etag(), 1);
|
|
259
|
+
const readAssetHandler = {
|
|
260
|
+
true: "node",
|
|
261
|
+
node: "node",
|
|
262
|
+
false: "null",
|
|
263
|
+
deno: "deno",
|
|
264
|
+
inline: "inline"
|
|
265
|
+
};
|
|
266
|
+
function publicAssets(nitro) {
|
|
267
|
+
return [
|
|
268
|
+
{
|
|
269
|
+
id: "#nitro/virtual/public-assets-data",
|
|
270
|
+
template: async () => {
|
|
271
|
+
const assets = {};
|
|
272
|
+
const files = await glob("**", {
|
|
273
|
+
cwd: nitro.options.output.publicDir,
|
|
274
|
+
absolute: false,
|
|
275
|
+
dot: true
|
|
276
|
+
});
|
|
277
|
+
for (const id of files) {
|
|
278
|
+
let mimeType = src_default.getType(id.replace(/\.(gz|br)$/, "")) || "text/plain";
|
|
279
|
+
if (mimeType.startsWith("text")) mimeType += "; charset=utf-8";
|
|
280
|
+
const fullPath = resolve(nitro.options.output.publicDir, id);
|
|
281
|
+
const assetData = await promises.readFile(fullPath);
|
|
282
|
+
const etag = (0, import_etag$1.default)(assetData);
|
|
283
|
+
const stat$2 = await promises.stat(fullPath);
|
|
284
|
+
const assetId = joinURL(nitro.options.baseURL, decodeURIComponent(id));
|
|
285
|
+
let encoding;
|
|
286
|
+
if (id.endsWith(".gz")) encoding = "gzip";
|
|
287
|
+
else if (id.endsWith(".br")) encoding = "br";
|
|
288
|
+
assets[assetId] = {
|
|
289
|
+
type: nitro._prerenderMeta?.[assetId]?.contentType || mimeType,
|
|
290
|
+
encoding,
|
|
291
|
+
etag,
|
|
292
|
+
mtime: stat$2.mtime.toJSON(),
|
|
293
|
+
size: stat$2.size,
|
|
294
|
+
path: relative(nitro.options.output.serverDir, fullPath),
|
|
295
|
+
data: nitro.options.serveStatic === "inline" ? assetData.toString("base64") : void 0
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
return `export default ${JSON.stringify(assets, null, 2)};`;
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
{
|
|
302
|
+
id: "#nitro/virtual/public-assets",
|
|
303
|
+
template: () => {
|
|
304
|
+
const publicAssetBases = Object.fromEntries(nitro.options.publicAssets.filter((dir) => !dir.fallthrough && dir.baseURL !== "/").map((dir) => [withTrailingSlash(joinURL(nitro.options.baseURL, dir.baseURL || "/")), { maxAge: dir.maxAge }]));
|
|
305
|
+
return `
|
|
306
|
+
import assets from '#nitro/virtual/public-assets-data'
|
|
307
|
+
export { readAsset } from "${`#nitro/virtual/public-assets-${readAssetHandler[nitro.options.serveStatic] || "null"}`}"
|
|
308
|
+
export const publicAssetBases = ${JSON.stringify(publicAssetBases)}
|
|
309
|
+
|
|
310
|
+
export function isPublicAssetURL(id = '') {
|
|
311
|
+
if (assets[id]) {
|
|
312
|
+
return true
|
|
313
|
+
}
|
|
314
|
+
for (const base in publicAssetBases) {
|
|
315
|
+
if (id.startsWith(base)) { return true }
|
|
316
|
+
}
|
|
317
|
+
return false
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export function getPublicAssetMeta(id = '') {
|
|
321
|
+
for (const base in publicAssetBases) {
|
|
322
|
+
if (id.startsWith(base)) { return publicAssetBases[base] }
|
|
323
|
+
}
|
|
324
|
+
return {}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
export function getAsset (id) {
|
|
328
|
+
return assets[id]
|
|
329
|
+
}
|
|
330
|
+
`;
|
|
331
|
+
}
|
|
332
|
+
},
|
|
333
|
+
{
|
|
334
|
+
id: "#nitro/virtual/public-assets-node",
|
|
335
|
+
template: () => {
|
|
336
|
+
return `
|
|
337
|
+
import { promises as fsp } from 'node:fs'
|
|
338
|
+
import { fileURLToPath } from 'node:url'
|
|
339
|
+
import { resolve, dirname } from 'node:path'
|
|
340
|
+
import assets from '#nitro/virtual/public-assets-data'
|
|
341
|
+
export function readAsset (id) {
|
|
342
|
+
const serverDir = dirname(fileURLToPath(globalThis.__nitro_main__))
|
|
343
|
+
return fsp.readFile(resolve(serverDir, assets[id].path))
|
|
344
|
+
}`;
|
|
345
|
+
}
|
|
346
|
+
},
|
|
347
|
+
{
|
|
348
|
+
id: "#nitro/virtual/public-assets-deno",
|
|
349
|
+
template: () => {
|
|
350
|
+
return `
|
|
351
|
+
import assets from '#nitro/virtual/public-assets-data'
|
|
352
|
+
export function readAsset (id) {
|
|
353
|
+
// https://deno.com/deploy/docs/serve-static-assets
|
|
354
|
+
const path = '.' + decodeURIComponent(new URL(\`../public\${id}\`, 'file://').pathname)
|
|
355
|
+
return Deno.readFile(path);
|
|
356
|
+
}`;
|
|
357
|
+
}
|
|
358
|
+
},
|
|
359
|
+
{
|
|
360
|
+
id: "#nitro/virtual/public-assets-null",
|
|
361
|
+
template: () => {
|
|
362
|
+
return `
|
|
363
|
+
export function readAsset (id) {
|
|
364
|
+
return Promise.resolve(null);
|
|
365
|
+
}`;
|
|
366
|
+
}
|
|
367
|
+
},
|
|
368
|
+
{
|
|
369
|
+
id: "#nitro/virtual/public-assets-inline",
|
|
370
|
+
template: () => {
|
|
371
|
+
return `
|
|
372
|
+
import assets from '#nitro/virtual/public-assets-data'
|
|
373
|
+
export function readAsset (id) {
|
|
374
|
+
if (!assets[id]) { return undefined }
|
|
375
|
+
if (assets[id]._data) { return assets[id]._data }
|
|
376
|
+
if (!assets[id].data) { return assets[id].data }
|
|
377
|
+
assets[id]._data = Uint8Array.from(atob(assets[id].data), (c) => c.charCodeAt(0))
|
|
378
|
+
return assets[id]._data
|
|
379
|
+
}`;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
];
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
//#endregion
|
|
386
|
+
//#region src/build/virtual/renderer-template.ts
|
|
387
|
+
function rendererTemplate(nitro) {
|
|
388
|
+
return {
|
|
389
|
+
id: "#nitro/virtual/renderer-template",
|
|
390
|
+
template: async () => {
|
|
391
|
+
const template = nitro.options.renderer?.template;
|
|
392
|
+
if (typeof template !== "string") return `
|
|
393
|
+
export const rendererTemplate = () => '<!-- renderer.template is not set -->';
|
|
394
|
+
export const rendererTemplateFile = undefined;
|
|
395
|
+
export const isStaticTemplate = true;`;
|
|
396
|
+
if (nitro.options.dev) return `
|
|
397
|
+
import { readFile } from 'node:fs/promises';
|
|
398
|
+
export const rendererTemplate = () => readFile(${JSON.stringify(template)}, "utf8");
|
|
399
|
+
export const rendererTemplateFile = ${JSON.stringify(template)};
|
|
400
|
+
export const isStaticTemplate = ${JSON.stringify(nitro.options.renderer?.static)};
|
|
401
|
+
`;
|
|
402
|
+
else {
|
|
403
|
+
const html = await readFile(template, "utf8");
|
|
404
|
+
if (nitro.options.renderer?.static ?? !hasTemplateSyntax(html)) return `
|
|
405
|
+
import { HTTPResponse } from "h3";
|
|
406
|
+
export const rendererTemplate = () => new HTTPResponse(${JSON.stringify(html)}, { headers: { "content-type": "text/html; charset=utf-8" } });
|
|
407
|
+
`;
|
|
408
|
+
else return `
|
|
409
|
+
import { renderToResponse } from 'rendu'
|
|
410
|
+
import { serverFetch } from 'nitro/app'
|
|
411
|
+
const template = ${compileTemplateToString(html, { contextKeys: [...RENDER_CONTEXT_KEYS] })};
|
|
412
|
+
export const rendererTemplate = (request) => renderToResponse(template, { request, context: { serverFetch } })
|
|
413
|
+
`;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
//#endregion
|
|
420
|
+
//#region src/build/virtual/routing-meta.ts
|
|
421
|
+
function routingMeta(nitro) {
|
|
422
|
+
return {
|
|
423
|
+
id: "#nitro/virtual/routing-meta",
|
|
424
|
+
template: () => {
|
|
425
|
+
const routeHandlers = uniqueBy$1(Object.values(nitro.routing.routes.routes).flatMap((h) => h.data), "_importHash");
|
|
426
|
+
return `
|
|
427
|
+
${routeHandlers.map((h) => `import ${h._importHash}Meta from "${h.handler}?meta";`).join("\n")}
|
|
428
|
+
export const handlersMeta = [
|
|
429
|
+
${routeHandlers.map((h) => `{ route: ${JSON.stringify(h.route)}, method: ${JSON.stringify(h.method?.toLowerCase())}, meta: ${h._importHash}Meta }`).join(",\n")}
|
|
430
|
+
];
|
|
431
|
+
`.trim();
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
function uniqueBy$1(arr, key) {
|
|
436
|
+
return [...new Map(arr.map((item) => [item[key], item])).values()];
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
//#endregion
|
|
440
|
+
//#region src/build/virtual/routing.ts
|
|
441
|
+
const RuntimeRouteRules = [
|
|
442
|
+
"headers",
|
|
443
|
+
"redirect",
|
|
444
|
+
"proxy",
|
|
445
|
+
"cache"
|
|
446
|
+
];
|
|
447
|
+
function routing(nitro) {
|
|
448
|
+
return {
|
|
449
|
+
id: "#nitro/virtual/routing",
|
|
450
|
+
template: () => {
|
|
451
|
+
const allHandlers = uniqueBy([
|
|
452
|
+
...Object.values(nitro.routing.routes.routes).flatMap((h) => h.data),
|
|
453
|
+
...Object.values(nitro.routing.routedMiddleware.routes).map((h) => h.data),
|
|
454
|
+
...nitro.routing.globalMiddleware
|
|
455
|
+
], "_importHash");
|
|
456
|
+
return `
|
|
457
|
+
import * as __routeRules__ from "#nitro/runtime/route-rules";
|
|
458
|
+
import * as srvxNode from "srvx/node"
|
|
459
|
+
import * as h3 from "h3";
|
|
460
|
+
|
|
461
|
+
export const findRouteRules = ${nitro.routing.routeRules.compileToString({
|
|
462
|
+
serialize: serializeRouteRule,
|
|
463
|
+
matchAll: true
|
|
464
|
+
})}
|
|
465
|
+
|
|
466
|
+
const multiHandler = (...handlers) => {
|
|
467
|
+
const final = handlers.pop()
|
|
468
|
+
const middleware = handlers.filter(Boolean).map(h => h3.toMiddleware(h));
|
|
469
|
+
return (ev) => h3.callMiddleware(ev, middleware, final);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
${allHandlers.filter((h) => !h.lazy).map((h) => `import ${h._importHash} from "${h.handler}";`).join("\n")}
|
|
473
|
+
|
|
474
|
+
${allHandlers.filter((h) => h.lazy).map((h) => `const ${h._importHash} = h3.defineLazyEventHandler(() => import("${h.handler}")${h.format === "node" ? ".then(m => srvxNode.toFetchHandler(m.default))" : ""});`).join("\n")}
|
|
475
|
+
|
|
476
|
+
export const findRoute = ${nitro.routing.routes.compileToString({ serialize: serializeHandler })}
|
|
477
|
+
|
|
478
|
+
export const findRoutedMiddleware = ${nitro.routing.routedMiddleware.compileToString({
|
|
479
|
+
serialize: serializeHandler,
|
|
480
|
+
matchAll: true
|
|
481
|
+
})};
|
|
482
|
+
|
|
483
|
+
export const globalMiddleware = [
|
|
484
|
+
${nitro.routing.globalMiddleware.map((h) => h.lazy ? h._importHash : `h3.toEventHandler(${h._importHash})`).join(",")}
|
|
485
|
+
].filter(Boolean);
|
|
486
|
+
`;
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
function uniqueBy(arr, key) {
|
|
491
|
+
return [...new Map(arr.map((item) => [item[key], item])).values()];
|
|
492
|
+
}
|
|
493
|
+
function serializeHandler(h) {
|
|
494
|
+
const meta = Array.isArray(h) ? h[0] : h;
|
|
495
|
+
return `{${[
|
|
496
|
+
`route:${JSON.stringify(meta.route)}`,
|
|
497
|
+
meta.method && `method:${JSON.stringify(meta.method)}`,
|
|
498
|
+
meta.meta && `meta:${JSON.stringify(meta.meta)}`,
|
|
499
|
+
`handler:${Array.isArray(h) ? `multiHandler(${h.map((handler) => serializeHandlerFn(handler)).join(",")})` : serializeHandlerFn(h)}`
|
|
500
|
+
].filter(Boolean).join(",")}}`;
|
|
501
|
+
}
|
|
502
|
+
function serializeHandlerFn(h) {
|
|
503
|
+
let code = h._importHash;
|
|
504
|
+
if (!h.lazy) {
|
|
505
|
+
if (h.format === "node") code = `srvxNode.toFetchHandler(${code})`;
|
|
506
|
+
code = `h3.toEventHandler(${code})`;
|
|
507
|
+
}
|
|
508
|
+
return code;
|
|
509
|
+
}
|
|
510
|
+
function serializeRouteRule(h) {
|
|
511
|
+
return `[${Object.entries(h).filter(([name, options]) => options !== void 0 && name[0] !== "_").map(([name, options]) => {
|
|
512
|
+
return `{${[
|
|
513
|
+
`name:${JSON.stringify(name)}`,
|
|
514
|
+
`route:${JSON.stringify(h._route)}`,
|
|
515
|
+
h._method && `method:${JSON.stringify(h._method)}`,
|
|
516
|
+
RuntimeRouteRules.includes(name) && `handler:__routeRules__.${name}`,
|
|
517
|
+
`options:${JSON.stringify(options)}`
|
|
518
|
+
].filter(Boolean).join(",")}}`;
|
|
519
|
+
}).join(",")}]`;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
//#endregion
|
|
523
|
+
//#region src/build/virtual/runtime-config.ts
|
|
524
|
+
function runtimeConfig(nitro) {
|
|
525
|
+
return {
|
|
526
|
+
id: "#nitro/virtual/runtime-config",
|
|
527
|
+
template: () => {
|
|
528
|
+
return `export const runtimeConfig = ${JSON.stringify(nitro.options.runtimeConfig || {})};`;
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
//#endregion
|
|
534
|
+
//#region src/build/virtual/server-assets.ts
|
|
535
|
+
var import_etag = /* @__PURE__ */ __toESM(require_etag(), 1);
|
|
536
|
+
function serverAssets(nitro) {
|
|
537
|
+
return {
|
|
538
|
+
id: "#nitro/virtual/server-assets",
|
|
539
|
+
template: async () => {
|
|
540
|
+
if (nitro.options.dev || nitro.options.preset === "nitro-prerender") return `
|
|
541
|
+
import { createStorage } from 'unstorage'
|
|
542
|
+
import fsDriver from 'unstorage/drivers/fs'
|
|
543
|
+
const serverAssets = ${JSON.stringify(nitro.options.serverAssets)}
|
|
544
|
+
export const assets = createStorage()
|
|
545
|
+
for (const asset of serverAssets) {
|
|
546
|
+
assets.mount(asset.baseName, fsDriver({ base: asset.dir, ignore: (asset?.ignore || []) }))
|
|
547
|
+
}`;
|
|
548
|
+
const assets = {};
|
|
549
|
+
for (const asset of nitro.options.serverAssets) {
|
|
550
|
+
const files = await glob(asset.pattern || "**/*", {
|
|
551
|
+
cwd: asset.dir,
|
|
552
|
+
absolute: false,
|
|
553
|
+
ignore: asset.ignore
|
|
554
|
+
});
|
|
555
|
+
for (const _id of files) {
|
|
556
|
+
const fsPath = resolve(asset.dir, _id);
|
|
557
|
+
const id = asset.baseName + "/" + _id;
|
|
558
|
+
assets[id] = {
|
|
559
|
+
fsPath,
|
|
560
|
+
meta: {}
|
|
561
|
+
};
|
|
562
|
+
let type = src_default.getType(id) || "text/plain";
|
|
563
|
+
if (type.startsWith("text")) type += "; charset=utf-8";
|
|
564
|
+
const etag = (0, import_etag.default)(await promises.readFile(fsPath));
|
|
565
|
+
const mtime = await promises.stat(fsPath).then((s) => s.mtime.toJSON());
|
|
566
|
+
assets[id].meta = {
|
|
567
|
+
type,
|
|
568
|
+
etag,
|
|
569
|
+
mtime
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
return `
|
|
574
|
+
const _assets = {\n${Object.entries(assets).map(([id, asset]) => ` [${JSON.stringify(normalizeKey(id))}]: {\n import: () => import(${JSON.stringify("raw:" + asset.fsPath)}).then(r => r.default || r),\n meta: ${JSON.stringify(asset.meta)}\n }`).join(",\n")}\n}
|
|
575
|
+
|
|
576
|
+
const normalizeKey = ${normalizeKey.toString()}
|
|
577
|
+
|
|
578
|
+
export const assets = {
|
|
579
|
+
getKeys() {
|
|
580
|
+
return Promise.resolve(Object.keys(_assets))
|
|
581
|
+
},
|
|
582
|
+
hasItem (id) {
|
|
583
|
+
id = normalizeKey(id)
|
|
584
|
+
return Promise.resolve(id in _assets)
|
|
585
|
+
},
|
|
586
|
+
getItem (id) {
|
|
587
|
+
id = normalizeKey(id)
|
|
588
|
+
return Promise.resolve(_assets[id] ? _assets[id].import() : null)
|
|
589
|
+
},
|
|
590
|
+
getMeta (id) {
|
|
591
|
+
id = normalizeKey(id)
|
|
592
|
+
return Promise.resolve(_assets[id] ? _assets[id].meta : {})
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
`;
|
|
596
|
+
}
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
//#endregion
|
|
601
|
+
//#region src/build/virtual/storage.ts
|
|
602
|
+
function storage(nitro) {
|
|
603
|
+
return {
|
|
604
|
+
id: "#nitro/virtual/storage",
|
|
605
|
+
template: () => {
|
|
606
|
+
const mounts = [];
|
|
607
|
+
const storageMounts = nitro.options.dev || nitro.options.preset === "nitro-prerender" ? {
|
|
608
|
+
...nitro.options.storage,
|
|
609
|
+
...nitro.options.devStorage
|
|
610
|
+
} : nitro.options.storage;
|
|
611
|
+
for (const path in storageMounts) {
|
|
612
|
+
const { driver: driverName, ...driverOpts } = storageMounts[path];
|
|
613
|
+
mounts.push({
|
|
614
|
+
path,
|
|
615
|
+
driver: builtinDrivers[driverName] || driverName,
|
|
616
|
+
opts: driverOpts
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
return `
|
|
620
|
+
import { createStorage } from 'unstorage'
|
|
621
|
+
import { assets } from '#nitro/virtual/server-assets'
|
|
622
|
+
|
|
623
|
+
${[...new Set(mounts.map((m) => m.driver))].map((i) => genImport(i, genSafeVariableName(i))).join("\n")}
|
|
624
|
+
|
|
625
|
+
export function initStorage() {
|
|
626
|
+
const storage = createStorage({})
|
|
627
|
+
storage.mount('/assets', assets)
|
|
628
|
+
${mounts.map((m) => `storage.mount('${m.path}', ${genSafeVariableName(m.driver)}(${JSON.stringify(m.opts)}))`).join("\n")}
|
|
629
|
+
return storage
|
|
630
|
+
}
|
|
631
|
+
`;
|
|
632
|
+
}
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
//#endregion
|
|
637
|
+
//#region src/build/virtual/tasks.ts
|
|
638
|
+
function tasks(nitro) {
|
|
639
|
+
return {
|
|
640
|
+
id: "#nitro/virtual/tasks",
|
|
641
|
+
template: () => {
|
|
642
|
+
const _scheduledTasks = Object.entries(nitro.options.scheduledTasks || {}).map(([cron, _tasks]) => {
|
|
643
|
+
return {
|
|
644
|
+
cron,
|
|
645
|
+
tasks: (Array.isArray(_tasks) ? _tasks : [_tasks]).filter((name) => {
|
|
646
|
+
if (!nitro.options.tasks[name]) {
|
|
647
|
+
nitro.logger.warn(`Scheduled task \`${name}\` is not defined!`);
|
|
648
|
+
return false;
|
|
649
|
+
}
|
|
650
|
+
return true;
|
|
651
|
+
})
|
|
652
|
+
};
|
|
653
|
+
}).filter((e) => e.tasks.length > 0);
|
|
654
|
+
const scheduledTasks = _scheduledTasks.length > 0 ? _scheduledTasks : false;
|
|
655
|
+
return `
|
|
656
|
+
export const scheduledTasks = ${JSON.stringify(scheduledTasks)};
|
|
657
|
+
|
|
658
|
+
export const tasks = {
|
|
659
|
+
${Object.entries(nitro.options.tasks).map(([name, task]) => `"${name}": {
|
|
660
|
+
meta: {
|
|
661
|
+
description: ${JSON.stringify(task.description)},
|
|
662
|
+
},
|
|
663
|
+
resolve: ${task.handler ? `() => import("${normalize(task.handler)}").then(r => r.default || r)` : "undefined"},
|
|
664
|
+
}`).join(",\n")}
|
|
665
|
+
};`;
|
|
666
|
+
}
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
//#endregion
|
|
671
|
+
//#region src/build/virtual/_all.ts
|
|
672
|
+
function virtualTemplates(nitro, _polyfills) {
|
|
673
|
+
const nitroTemplates = [
|
|
674
|
+
database,
|
|
675
|
+
errorHandler,
|
|
676
|
+
featureFlags,
|
|
677
|
+
plugins,
|
|
678
|
+
polyfills,
|
|
679
|
+
publicAssets,
|
|
680
|
+
rendererTemplate,
|
|
681
|
+
routingMeta,
|
|
682
|
+
routing,
|
|
683
|
+
runtimeConfig,
|
|
684
|
+
serverAssets,
|
|
685
|
+
storage,
|
|
686
|
+
tasks
|
|
687
|
+
].flatMap((t) => t(nitro, _polyfills));
|
|
688
|
+
const customTemplates = Object.entries(nitro.options.virtual).map(([id, template]) => ({
|
|
689
|
+
id,
|
|
690
|
+
template
|
|
691
|
+
}));
|
|
692
|
+
return [...nitroTemplates, ...customTemplates];
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
//#endregion
|
|
696
|
+
//#region src/build/plugins/route-meta.ts
|
|
697
|
+
const PREFIX$1 = "\0nitro:route-meta:";
|
|
698
|
+
function routeMeta(nitro) {
|
|
699
|
+
return {
|
|
700
|
+
name: "nitro:route-meta",
|
|
701
|
+
resolveId: {
|
|
702
|
+
filter: { id: /^(?!\u0000)(.+)\?meta$/ },
|
|
703
|
+
async handler(id, importer, resolveOpts) {
|
|
704
|
+
if (id.endsWith("?meta")) {
|
|
705
|
+
const resolved = await this.resolve(id.replace("?meta", ""), importer, resolveOpts);
|
|
706
|
+
if (!resolved) return;
|
|
707
|
+
return PREFIX$1 + resolved.id;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
},
|
|
711
|
+
load: {
|
|
712
|
+
filter: { id: /* @__PURE__ */ new RegExp(`^${escapeRegExp(PREFIX$1)}`) },
|
|
713
|
+
handler(id) {
|
|
714
|
+
if (id.startsWith(PREFIX$1)) {
|
|
715
|
+
const fullPath = id.slice(18);
|
|
716
|
+
if (isAbsolute(fullPath)) return readFile(fullPath, { encoding: "utf8" });
|
|
717
|
+
else return "export default undefined;";
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
},
|
|
721
|
+
transform: {
|
|
722
|
+
filter: { id: /* @__PURE__ */ new RegExp(`^${escapeRegExp(PREFIX$1)}`) },
|
|
723
|
+
async handler(code, id) {
|
|
724
|
+
let meta = null;
|
|
725
|
+
try {
|
|
726
|
+
const transformRes = transformSync(id, code);
|
|
727
|
+
if (transformRes.errors?.length > 0) {
|
|
728
|
+
for (const error of transformRes.errors) this.warn(error);
|
|
729
|
+
return {
|
|
730
|
+
code: `export default {};`,
|
|
731
|
+
map: null
|
|
732
|
+
};
|
|
733
|
+
}
|
|
734
|
+
const ast = this.parse(transformRes.code);
|
|
735
|
+
for (const node of ast.body) if (node.type === "ExpressionStatement" && node.expression.type === "CallExpression" && node.expression.callee.type === "Identifier" && node.expression.callee.name === "defineRouteMeta" && node.expression.arguments.length === 1) {
|
|
736
|
+
meta = astToObject(node.expression.arguments[0]);
|
|
737
|
+
break;
|
|
738
|
+
}
|
|
739
|
+
} catch (error) {
|
|
740
|
+
nitro.logger.warn(`[handlers-meta] Cannot extra route meta for: ${id}: ${error}`);
|
|
741
|
+
}
|
|
742
|
+
return {
|
|
743
|
+
code: `export default ${JSON.stringify(meta)};`,
|
|
744
|
+
map: null
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
function astToObject(node) {
|
|
751
|
+
switch (node.type) {
|
|
752
|
+
case "ObjectExpression": {
|
|
753
|
+
const obj = {};
|
|
754
|
+
for (const prop of node.properties) if (prop.type === "Property") {
|
|
755
|
+
const key = prop.key.name ?? prop.key.value;
|
|
756
|
+
obj[key] = astToObject(prop.value);
|
|
757
|
+
}
|
|
758
|
+
return obj;
|
|
759
|
+
}
|
|
760
|
+
case "ArrayExpression": return node.elements.map((el) => astToObject(el)).filter(Boolean);
|
|
761
|
+
case "Literal": return node.value;
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
//#endregion
|
|
766
|
+
//#region src/build/plugins/server-main.ts
|
|
767
|
+
function serverMain(nitro) {
|
|
768
|
+
return {
|
|
769
|
+
name: "nitro:server-main",
|
|
770
|
+
renderChunk(code, chunk) {
|
|
771
|
+
if (chunk.isEntry) return {
|
|
772
|
+
code: `globalThis.__nitro_main__ = import.meta.url; ${code}`,
|
|
773
|
+
map: null
|
|
774
|
+
};
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
//#endregion
|
|
780
|
+
//#region src/build/plugins/virtual.ts
|
|
781
|
+
function virtual(input) {
|
|
782
|
+
const modules = /* @__PURE__ */ new Map();
|
|
783
|
+
for (const mod of input) {
|
|
784
|
+
const render = () => typeof mod.template === "function" ? mod.template() : mod.template;
|
|
785
|
+
modules.set(mod.id, {
|
|
786
|
+
module: mod,
|
|
787
|
+
render
|
|
28
788
|
});
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
789
|
+
}
|
|
790
|
+
const include = [/^#nitro\/virtual/];
|
|
791
|
+
const extraIds = [...modules.keys()].filter((key) => !key.startsWith("#nitro/virtual"));
|
|
792
|
+
if (extraIds.length > 0) include.push(/* @__PURE__ */ new RegExp(`^(${extraIds.map((id) => pathRegExp(id)).join("|")})$`));
|
|
793
|
+
return {
|
|
794
|
+
name: "nitro:virtual",
|
|
795
|
+
api: { modules },
|
|
796
|
+
resolveId: {
|
|
797
|
+
order: "pre",
|
|
798
|
+
filter: { id: include },
|
|
799
|
+
handler: (id) => {
|
|
800
|
+
const mod = modules.get(id);
|
|
801
|
+
if (mod) return {
|
|
802
|
+
id,
|
|
803
|
+
moduleSideEffects: mod.module.moduleSideEffects ?? false
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
},
|
|
807
|
+
load: {
|
|
808
|
+
order: "pre",
|
|
809
|
+
filter: { id: include },
|
|
810
|
+
handler: async (id) => {
|
|
811
|
+
const mod = modules.get(id);
|
|
812
|
+
if (!mod) throw new Error(`Virtual module ${id} not found.`);
|
|
813
|
+
return {
|
|
814
|
+
code: await mod.render(),
|
|
815
|
+
map: null
|
|
816
|
+
};
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
};
|
|
820
|
+
}
|
|
821
|
+
function virtualDeps() {
|
|
822
|
+
const cache = /* @__PURE__ */ new Map();
|
|
823
|
+
return {
|
|
824
|
+
name: "nitro:virtual-deps",
|
|
825
|
+
resolveId: {
|
|
826
|
+
order: "pre",
|
|
827
|
+
filter: { id: /* @__PURE__ */ new RegExp(`^(#nitro|${runtimeDependencies.map((dep) => pathRegExp(dep)).join("|")})`) },
|
|
828
|
+
handler(id, importer) {
|
|
829
|
+
if (!importer || !importer.startsWith("#nitro/virtual")) return;
|
|
830
|
+
let resolved = cache.get(id);
|
|
831
|
+
if (!resolved) resolved = this.resolve(id, runtimeDir).then((_resolved) => {
|
|
832
|
+
cache.set(id, _resolved);
|
|
833
|
+
return _resolved;
|
|
834
|
+
}).catch((error) => {
|
|
835
|
+
cache.delete(id);
|
|
836
|
+
throw error;
|
|
837
|
+
});
|
|
838
|
+
return resolved;
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
//#endregion
|
|
845
|
+
//#region src/build/plugins/sourcemap-min.ts
|
|
846
|
+
function sourcemapMinify() {
|
|
847
|
+
return {
|
|
848
|
+
name: "nitro:sourcemap-minify",
|
|
849
|
+
generateBundle(_options, bundle) {
|
|
850
|
+
for (const [key, asset] of Object.entries(bundle)) {
|
|
851
|
+
if (!key.endsWith(".map") || !("source" in asset) || typeof asset.source !== "string") continue;
|
|
852
|
+
const sourcemap = JSON.parse(asset.source);
|
|
853
|
+
delete sourcemap.sourcesContent;
|
|
854
|
+
delete sourcemap.x_google_ignoreList;
|
|
855
|
+
if ((sourcemap.sources || []).some((s) => s.includes("node_modules"))) sourcemap.mappings = "";
|
|
856
|
+
asset.source = JSON.stringify(sourcemap);
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
//#endregion
|
|
863
|
+
//#region src/build/plugins/raw.ts
|
|
864
|
+
const HELPER_ID = "\0nitro-raw-helpers";
|
|
865
|
+
const RESOLVED_PREFIX = "\0nitro:raw:";
|
|
866
|
+
const PREFIX = "raw:";
|
|
867
|
+
function raw() {
|
|
868
|
+
return {
|
|
869
|
+
name: "nitro:raw",
|
|
870
|
+
resolveId: {
|
|
871
|
+
order: "pre",
|
|
872
|
+
filter: { id: [/* @__PURE__ */ new RegExp(`^${HELPER_ID}$`), /* @__PURE__ */ new RegExp(`^${PREFIX}`)] },
|
|
873
|
+
async handler(id, importer, resolveOpts) {
|
|
874
|
+
if (id === HELPER_ID) return id;
|
|
875
|
+
if (id.startsWith(PREFIX)) {
|
|
876
|
+
const resolvedId = (await this.resolve(id.slice(4), importer, resolveOpts))?.id;
|
|
877
|
+
if (!resolvedId) return null;
|
|
878
|
+
return { id: RESOLVED_PREFIX + resolvedId };
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
},
|
|
882
|
+
load: {
|
|
883
|
+
order: "pre",
|
|
884
|
+
filter: { id: [/* @__PURE__ */ new RegExp(`^${HELPER_ID}$`), /* @__PURE__ */ new RegExp(`^${RESOLVED_PREFIX}`)] },
|
|
885
|
+
handler(id) {
|
|
886
|
+
if (id === HELPER_ID) return getHelpers();
|
|
887
|
+
if (id.startsWith(RESOLVED_PREFIX)) return promises.readFile(id.slice(11), isBinary(id) ? "binary" : "utf8");
|
|
888
|
+
}
|
|
889
|
+
},
|
|
890
|
+
transform: {
|
|
891
|
+
order: "pre",
|
|
892
|
+
filter: { id: /* @__PURE__ */ new RegExp(`^${RESOLVED_PREFIX}`) },
|
|
893
|
+
handler(code, id) {
|
|
894
|
+
const path = id.slice(11);
|
|
895
|
+
if (isBinary(id)) return {
|
|
896
|
+
code: `import {base64ToUint8Array } from "${HELPER_ID}" \n export default base64ToUint8Array("${Buffer.from(code, "binary").toString("base64")}")`,
|
|
897
|
+
map: rawAssetMap(path)
|
|
898
|
+
};
|
|
899
|
+
return {
|
|
900
|
+
code: `export default ${JSON.stringify(code)}`,
|
|
901
|
+
map: rawAssetMap(path),
|
|
902
|
+
moduleType: "js"
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
function isBinary(id) {
|
|
909
|
+
const idMime = src_default.getType(id) || "";
|
|
910
|
+
if (idMime.startsWith("text/")) return false;
|
|
911
|
+
if (/application\/(json|sql|xml|yaml)/.test(idMime)) return false;
|
|
912
|
+
return true;
|
|
913
|
+
}
|
|
914
|
+
function getHelpers() {
|
|
915
|
+
return `
|
|
916
|
+
export function base64ToUint8Array(str) {
|
|
917
|
+
const data = atob(str);
|
|
918
|
+
const size = data.length;
|
|
919
|
+
const bytes = new Uint8Array(size);
|
|
920
|
+
for (let i = 0; i < size; i++) {
|
|
921
|
+
bytes[i] = data.charCodeAt(i);
|
|
922
|
+
}
|
|
923
|
+
return bytes;
|
|
924
|
+
}
|
|
925
|
+
`;
|
|
926
|
+
}
|
|
927
|
+
function rawAssetMap(id) {
|
|
928
|
+
return {
|
|
929
|
+
version: 3,
|
|
930
|
+
file: id,
|
|
931
|
+
sources: [id],
|
|
932
|
+
sourcesContent: [],
|
|
933
|
+
names: [],
|
|
934
|
+
mappings: ""
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
//#endregion
|
|
939
|
+
//#region src/utils/dep.ts
|
|
940
|
+
async function importDep(opts, _retry) {
|
|
941
|
+
const resolved = resolveModulePath(opts.id, {
|
|
942
|
+
from: [opts.dir, import.meta.url],
|
|
943
|
+
cache: _retry ? false : true,
|
|
944
|
+
try: true
|
|
945
|
+
});
|
|
946
|
+
if (resolved) return await import(resolved);
|
|
947
|
+
let shouldInstall;
|
|
948
|
+
if (_retry || a) shouldInstall = false;
|
|
949
|
+
else if (T) {
|
|
950
|
+
consola.info(`\`${opts.id}\` is required for ${opts.reason}. Installing automatically in CI environment...`);
|
|
951
|
+
shouldInstall = true;
|
|
952
|
+
} else shouldInstall = await consola.prompt(`\`${opts.id}\` is required for ${opts.reason}, but it is not installed. Would you like to install it?`, {
|
|
953
|
+
type: "confirm",
|
|
954
|
+
default: true,
|
|
955
|
+
cancel: "undefined"
|
|
956
|
+
});
|
|
957
|
+
if (!shouldInstall) throw new Error(`\`${opts.id}\` is not installed. Please add it to your dependencies for ${opts.reason}.`);
|
|
958
|
+
const start = Date.now();
|
|
959
|
+
consola.start(`Installing \`${opts.id}\` in \`${opts.dir}\`...`);
|
|
960
|
+
const { addDevDependency } = await import("../cli/_chunks/dist4.mjs");
|
|
961
|
+
await addDevDependency(opts.id, { cwd: opts.dir });
|
|
962
|
+
consola.success(`Installed \`${opts.id}\` in ${opts.dir} (${Date.now() - start}ms).`);
|
|
963
|
+
return importDep(opts, true);
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
//#endregion
|
|
967
|
+
//#region src/build/plugins/externals.ts
|
|
968
|
+
const PLUGIN_NAME = "nitro:externals";
|
|
969
|
+
function externals(opts) {
|
|
970
|
+
const include = opts?.include ? opts.include.map((p) => toPathRegExp(p)) : void 0;
|
|
971
|
+
const exclude = [/^(?:[\0#~.]|[a-z0-9]{2,}:)|\?/, ...(opts?.exclude || []).map((p) => toPathRegExp(p))];
|
|
972
|
+
const filter = (id) => {
|
|
973
|
+
if (include && !include.some((r) => r.test(id))) return false;
|
|
974
|
+
if (exclude.some((r) => r.test(id))) return false;
|
|
975
|
+
return true;
|
|
976
|
+
};
|
|
977
|
+
const tryResolve = (id, from) => resolveModulePath(id, {
|
|
978
|
+
try: true,
|
|
979
|
+
from: from && isAbsolute(from) ? from : opts.rootDir,
|
|
980
|
+
conditions: opts.conditions
|
|
981
|
+
});
|
|
982
|
+
const tracedPaths = /* @__PURE__ */ new Set();
|
|
983
|
+
if (include && include.length === 0) return { name: PLUGIN_NAME };
|
|
984
|
+
return {
|
|
985
|
+
name: PLUGIN_NAME,
|
|
986
|
+
resolveId: {
|
|
987
|
+
order: "pre",
|
|
988
|
+
filter: { id: {
|
|
989
|
+
exclude,
|
|
990
|
+
include
|
|
991
|
+
} },
|
|
992
|
+
async handler(id, importer, rOpts) {
|
|
993
|
+
if (builtinModules.includes(id)) return {
|
|
994
|
+
resolvedBy: PLUGIN_NAME,
|
|
995
|
+
external: true,
|
|
996
|
+
id: id.includes(":") ? id : `node:${id}`
|
|
997
|
+
};
|
|
998
|
+
if (rOpts.custom?.["node-resolve"]) return null;
|
|
999
|
+
let resolved = await this.resolve(id, importer, rOpts);
|
|
1000
|
+
const cjsResolved = resolved?.meta?.commonjs?.resolved;
|
|
1001
|
+
if (cjsResolved) {
|
|
1002
|
+
if (!filter(cjsResolved.id)) return resolved;
|
|
1003
|
+
resolved = cjsResolved;
|
|
1004
|
+
}
|
|
1005
|
+
if (!resolved?.id || !filter(resolved.id)) return resolved;
|
|
1006
|
+
let resolvedPath = resolved.id;
|
|
1007
|
+
if (!isAbsolute(resolvedPath)) resolvedPath = tryResolve(resolvedPath, importer) || resolvedPath;
|
|
1008
|
+
if (opts.trace) {
|
|
1009
|
+
let importId = toImport(id) || toImport(resolvedPath);
|
|
1010
|
+
if (!importId) return resolved;
|
|
1011
|
+
if (!tryResolve(importId, importer)) {
|
|
1012
|
+
const guessed = await guessSubpath(resolvedPath, opts.conditions);
|
|
1013
|
+
if (!guessed) return resolved;
|
|
1014
|
+
importId = guessed;
|
|
1015
|
+
}
|
|
1016
|
+
tracedPaths.add(resolvedPath);
|
|
1017
|
+
return {
|
|
1018
|
+
...resolved,
|
|
1019
|
+
resolvedBy: PLUGIN_NAME,
|
|
1020
|
+
external: true,
|
|
1021
|
+
id: importId
|
|
1022
|
+
};
|
|
1023
|
+
}
|
|
1024
|
+
return {
|
|
1025
|
+
...resolved,
|
|
1026
|
+
resolvedBy: PLUGIN_NAME,
|
|
1027
|
+
external: true,
|
|
1028
|
+
id: isAbsolute(resolvedPath) ? pathToFileURL(resolvedPath).href : resolvedPath
|
|
1029
|
+
};
|
|
1030
|
+
}
|
|
1031
|
+
},
|
|
1032
|
+
buildEnd: {
|
|
1033
|
+
order: "post",
|
|
1034
|
+
async handler() {
|
|
1035
|
+
if (!opts.trace || tracedPaths.size === 0) return;
|
|
1036
|
+
const { traceNodeModules } = await importDep({
|
|
1037
|
+
id: "nf3",
|
|
1038
|
+
dir: opts.rootDir,
|
|
1039
|
+
reason: "tracing external dependencies"
|
|
1040
|
+
});
|
|
1041
|
+
await traceNodeModules([...tracedPaths], {
|
|
1042
|
+
...opts.trace,
|
|
1043
|
+
conditions: opts.conditions,
|
|
1044
|
+
rootDir: opts.rootDir,
|
|
1045
|
+
writePackageJson: true
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
const NODE_MODULES_RE = /^(?<dir>.+[\\/]node_modules[\\/])(?<name>[^@\\/]+|@[^\\/]+[\\/][^\\/]+)(?:[\\/](?<subpath>.+))?$/;
|
|
1052
|
+
const IMPORT_RE = /^(?!\.)(?<name>[^@/\\]+|@[^/\\]+[/\\][^/\\]+)(?:[/\\](?<subpath>.+))?$/;
|
|
1053
|
+
function toImport(id) {
|
|
1054
|
+
if (isAbsolute(id)) {
|
|
1055
|
+
const { name, subpath } = NODE_MODULES_RE.exec(id)?.groups || {};
|
|
1056
|
+
if (name && subpath) return join(name, subpath);
|
|
1057
|
+
} else if (IMPORT_RE.test(id)) return id;
|
|
1058
|
+
}
|
|
1059
|
+
function guessSubpath(path, conditions) {
|
|
1060
|
+
const { dir, name, subpath } = NODE_MODULES_RE.exec(path)?.groups || {};
|
|
1061
|
+
if (!dir || !name || !subpath) return;
|
|
1062
|
+
const exports = getPkgJSON(join(dir, name) + "/")?.exports;
|
|
1063
|
+
if (!exports || typeof exports !== "object") return;
|
|
1064
|
+
for (const e of flattenExports(exports)) {
|
|
1065
|
+
if (!conditions.includes(e.condition || "default")) continue;
|
|
1066
|
+
if (e.fsPath === subpath) return join(name, e.subpath);
|
|
1067
|
+
if (e.fsPath.includes("*")) {
|
|
1068
|
+
const fsPathRe = /* @__PURE__ */ new RegExp("^" + escapeRegExp(e.fsPath).replace(String.raw`\*`, "(.+?)") + "$");
|
|
1069
|
+
if (fsPathRe.test(subpath)) {
|
|
1070
|
+
const matched = fsPathRe.exec(subpath)?.[1];
|
|
1071
|
+
if (matched) return join(name, e.subpath.replace("*", matched));
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
function getPkgJSON(dir) {
|
|
1077
|
+
const cache = getPkgJSON._cache ||= /* @__PURE__ */ new Map();
|
|
1078
|
+
if (cache.has(dir)) return cache.get(dir);
|
|
1079
|
+
try {
|
|
1080
|
+
const pkg = createRequire(dir)("./package.json");
|
|
1081
|
+
cache.set(dir, pkg);
|
|
1082
|
+
return pkg;
|
|
1083
|
+
} catch {}
|
|
1084
|
+
}
|
|
1085
|
+
function flattenExports(exports = {}, parentSubpath = "./") {
|
|
1086
|
+
return Object.entries(exports).flatMap(([key, value]) => {
|
|
1087
|
+
const [subpath, condition] = key.startsWith(".") ? [key.slice(1)] : [void 0, key];
|
|
1088
|
+
const _subPath = join(parentSubpath, subpath || "");
|
|
1089
|
+
if (typeof value === "string") return [{
|
|
1090
|
+
subpath: _subPath,
|
|
1091
|
+
fsPath: value.replace(/^\.\//, ""),
|
|
1092
|
+
condition
|
|
1093
|
+
}];
|
|
1094
|
+
return typeof value === "object" ? flattenExports(value, _subPath) : [];
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
//#endregion
|
|
1099
|
+
//#region src/build/plugins.ts
|
|
1100
|
+
function baseBuildPlugins(nitro, base) {
|
|
1101
|
+
const plugins$1 = [];
|
|
1102
|
+
const virtualPlugin = virtual(virtualTemplates(nitro, [...base.env.polyfill]));
|
|
1103
|
+
nitro.vfs = virtualPlugin.api.modules;
|
|
1104
|
+
plugins$1.push(virtualPlugin, virtualDeps());
|
|
1105
|
+
if (nitro.options.imports) plugins$1.push(unplugin.rollup(nitro.options.imports));
|
|
1106
|
+
if (nitro.options.wasm !== false) plugins$1.push(unwasm(nitro.options.wasm || {}));
|
|
1107
|
+
plugins$1.push(serverMain(nitro));
|
|
1108
|
+
plugins$1.push(raw());
|
|
1109
|
+
if (nitro.options.experimental.openAPI) plugins$1.push(routeMeta(nitro));
|
|
1110
|
+
plugins$1.push(replace({
|
|
1111
|
+
preventAssignment: true,
|
|
1112
|
+
values: base.replacements
|
|
1113
|
+
}));
|
|
1114
|
+
if (nitro.options.node && nitro.options.noExternals !== true) {
|
|
1115
|
+
const isDevOrPrerender = nitro.options.dev || nitro.options.preset === "nitro-prerender";
|
|
1116
|
+
plugins$1.push(externals({
|
|
1117
|
+
rootDir: nitro.options.rootDir,
|
|
1118
|
+
conditions: nitro.options.exportConditions || ["default"],
|
|
1119
|
+
exclude: [...base.noExternal],
|
|
1120
|
+
include: isDevOrPrerender ? void 0 : nitro.options.traceDeps,
|
|
1121
|
+
trace: isDevOrPrerender ? false : { outDir: nitro.options.output.serverDir }
|
|
1122
|
+
}));
|
|
1123
|
+
}
|
|
1124
|
+
if (nitro.options.sourcemap && !nitro.options.dev && nitro.options.experimental.sourcemapMinify !== false) plugins$1.push(sourcemapMinify());
|
|
1125
|
+
return plugins$1;
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
//#endregion
|
|
1129
|
+
export { baseBuildConfig as a, libChunkName as i, NODE_MODULES_RE$1 as n, getChunkName as r, baseBuildPlugins as t };
|