nitro-nightly 4.0.0-20251010-091516-7cafddba → 4.0.0-20251030-091344-d4418b98

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.
Files changed (195) hide show
  1. package/dist/{_chunks/build2.mjs → _build/build.mjs} +108 -58
  2. package/dist/_build/build2.mjs +556 -0
  3. package/dist/_build/info.mjs +1002 -0
  4. package/dist/_build/prepare.mjs +1511 -0
  5. package/dist/_build/snapshot.mjs +185 -0
  6. package/dist/{_chunks/plugin.mjs → _build/vite.mjs} +295 -296
  7. package/dist/_build/vite2.mjs +149 -0
  8. package/dist/_chunks/_deps/@jridgewell/gen-mapping.mjs +189 -0
  9. package/dist/_chunks/_deps/@jridgewell/remapping.mjs +137 -0
  10. package/dist/_chunks/_deps/@jridgewell/resolve-uri.mjs +231 -0
  11. package/dist/_chunks/_deps/@jridgewell/sourcemap-codec.mjs +173 -0
  12. package/dist/_chunks/_deps/@jridgewell/trace-mapping.mjs +170 -0
  13. package/dist/_chunks/_deps/@pi0/vite-plugin-fullstack.mjs +575 -0
  14. package/dist/_chunks/_deps/@rollup/plugin-alias.mjs +89 -0
  15. package/dist/_chunks/_deps/@rollup/plugin-commonjs.mjs +2376 -0
  16. package/dist/_chunks/{index2.mjs → _deps/@rollup/plugin-inject.mjs} +5 -90
  17. package/dist/_chunks/_deps/@rollup/plugin-json.mjs +37 -0
  18. package/dist/_chunks/_deps/@rollup/plugin-node-resolve.mjs +1386 -0
  19. package/dist/_chunks/_deps/@rollup/plugin-replace.mjs +133 -0
  20. package/dist/_chunks/_deps/@rollup/pluginutils.mjs +346 -0
  21. package/dist/_chunks/_deps/acorn.mjs +6225 -0
  22. package/dist/_chunks/_deps/c12.mjs +510 -0
  23. package/dist/_chunks/_deps/chokidar.mjs +1428 -0
  24. package/dist/_chunks/_deps/citty.mjs +460 -0
  25. package/dist/_chunks/_deps/commondir.mjs +77 -0
  26. package/dist/_chunks/_deps/compatx.mjs +76 -0
  27. package/dist/_chunks/_deps/confbox.mjs +300 -0
  28. package/dist/_chunks/_deps/debug.mjs +885 -0
  29. package/dist/_chunks/_deps/deepmerge.mjs +147 -0
  30. package/dist/_chunks/_deps/depd.mjs +550 -0
  31. package/dist/_chunks/_deps/dot-prop.mjs +282 -0
  32. package/dist/_chunks/_deps/dotenv.mjs +555 -0
  33. package/dist/_chunks/_deps/duplexer.mjs +1 -0
  34. package/dist/_chunks/_deps/ee-first.mjs +104 -0
  35. package/dist/_chunks/_deps/encodeurl.mjs +69 -0
  36. package/dist/_chunks/_deps/escape-html.mjs +87 -0
  37. package/dist/_chunks/_deps/escape-string-regexp.mjs +13 -0
  38. package/dist/_chunks/_deps/estree-walker.mjs +433 -0
  39. package/dist/_chunks/_deps/etag.mjs +147 -0
  40. package/dist/_chunks/_deps/exsolve.mjs +1416 -0
  41. package/dist/_chunks/_deps/fdir.mjs +569 -0
  42. package/dist/_chunks/_deps/fresh.mjs +145 -0
  43. package/dist/_chunks/_deps/function-bind.mjs +106 -0
  44. package/dist/_chunks/{index4.mjs → _deps/giget.mjs} +21 -776
  45. package/dist/_chunks/_deps/gzip-size.mjs +19 -0
  46. package/dist/_chunks/_deps/hasown.mjs +19 -0
  47. package/dist/_chunks/_deps/http-errors.mjs +307 -0
  48. package/dist/_chunks/_deps/httpxy.mjs +580 -0
  49. package/dist/_chunks/_deps/inherits.mjs +57 -0
  50. package/dist/_chunks/_deps/is-core-module.mjs +596 -0
  51. package/dist/_chunks/_deps/is-module.mjs +25 -0
  52. package/dist/_chunks/_deps/is-reference.mjs +31 -0
  53. package/dist/_chunks/_deps/js-tokens.mjs +411 -0
  54. package/dist/_chunks/_deps/klona.mjs +137 -0
  55. package/dist/_chunks/_deps/knitwork.mjs +172 -0
  56. package/dist/_chunks/_deps/local-pkg.mjs +163 -0
  57. package/dist/_chunks/_deps/magic-string.mjs +1296 -0
  58. package/dist/_chunks/_deps/mime-db.mjs +11685 -0
  59. package/dist/_chunks/_deps/mime-types.mjs +287 -0
  60. package/dist/_chunks/_deps/mime.mjs +1172 -0
  61. package/dist/_chunks/_deps/mlly.mjs +2413 -0
  62. package/dist/_chunks/_deps/ms.mjs +172 -0
  63. package/dist/_chunks/_deps/node-fetch-native.mjs +3 -0
  64. package/dist/_chunks/_deps/nypm.mjs +219 -0
  65. package/dist/_chunks/_deps/on-finished.mjs +246 -0
  66. package/dist/_chunks/_deps/parseurl.mjs +168 -0
  67. package/dist/_chunks/_deps/path-parse.mjs +85 -0
  68. package/dist/_chunks/{pathe.M-eThtNZ.mjs → _deps/pathe.mjs} +48 -1
  69. package/dist/_chunks/_deps/perfect-debounce.mjs +88 -0
  70. package/dist/_chunks/_deps/picomatch.mjs +2144 -0
  71. package/dist/_chunks/_deps/pkg-types.mjs +247 -0
  72. package/dist/_chunks/{snapshot.mjs → _deps/pretty-bytes.mjs} +1 -105
  73. package/dist/_chunks/_deps/quansync.mjs +99 -0
  74. package/dist/_chunks/_deps/range-parser.mjs +171 -0
  75. package/dist/_chunks/_deps/rc9.mjs +219 -0
  76. package/dist/_chunks/_deps/readdirp.mjs +245 -0
  77. package/dist/_chunks/_deps/resolve.mjs +1260 -0
  78. package/dist/_chunks/_deps/rou3.mjs +326 -0
  79. package/dist/_chunks/_deps/send.mjs +1022 -0
  80. package/dist/_chunks/_deps/serve-static.mjs +228 -0
  81. package/dist/_chunks/_deps/setprototypeof.mjs +26 -0
  82. package/dist/_chunks/_deps/statuses.mjs +457 -0
  83. package/dist/_chunks/_deps/std-env.mjs +3 -0
  84. package/dist/_chunks/_deps/strip-literal.mjs +67 -0
  85. package/dist/_chunks/_deps/supports-color.mjs +44 -0
  86. package/dist/_chunks/_deps/tinyexec.mjs +552 -0
  87. package/dist/_chunks/_deps/tinyglobby.mjs +293 -0
  88. package/dist/_chunks/_deps/toidentifier.mjs +41 -0
  89. package/dist/_chunks/_deps/ultrahtml.mjs +3 -0
  90. package/dist/_chunks/_deps/unimport.mjs +2267 -0
  91. package/dist/_chunks/_deps/unplugin-utils.mjs +65 -0
  92. package/dist/_chunks/_deps/unplugin.mjs +1294 -0
  93. package/dist/_chunks/_deps/untyped.mjs +375 -0
  94. package/dist/_chunks/{info.mjs → _deps/unwasm.mjs} +8 -4206
  95. package/dist/_chunks/_deps/webpack-virtual-modules.mjs +360 -0
  96. package/dist/_chunks/_presets/_all.mjs +59 -0
  97. package/dist/_chunks/_presets/_nitro.mjs +74 -0
  98. package/dist/_chunks/_presets/_resolve.mjs +64 -0
  99. package/dist/_chunks/_presets/_static.mjs +69 -0
  100. package/dist/_chunks/_presets/_types.mjs +3 -0
  101. package/dist/_chunks/_presets/_utils.mjs +31 -0
  102. package/dist/_chunks/_presets/alwaysdata.mjs +17 -0
  103. package/dist/_chunks/_presets/aws-amplify.mjs +111 -0
  104. package/dist/_chunks/_presets/aws-lambda.mjs +23 -0
  105. package/dist/_chunks/_presets/azure.mjs +162 -0
  106. package/dist/_chunks/_presets/bun.mjs +19 -0
  107. package/dist/_chunks/_presets/cleavr.mjs +15 -0
  108. package/dist/_chunks/_presets/cloudflare.mjs +608 -0
  109. package/dist/_chunks/_presets/deno.mjs +196 -0
  110. package/dist/_chunks/_presets/digitalocean.mjs +14 -0
  111. package/dist/_chunks/_presets/firebase.mjs +47 -0
  112. package/dist/_chunks/_presets/flightcontrol.mjs +14 -0
  113. package/dist/_chunks/_presets/genezio.mjs +13 -0
  114. package/dist/_chunks/_presets/heroku.mjs +14 -0
  115. package/dist/_chunks/_presets/iis.mjs +194 -0
  116. package/dist/_chunks/_presets/index.mjs +62 -0
  117. package/dist/_chunks/_presets/koyeb.mjs +14 -0
  118. package/dist/_chunks/_presets/netlify.mjs +241 -0
  119. package/dist/_chunks/_presets/node.mjs +54 -0
  120. package/dist/_chunks/_presets/platform.mjs +14 -0
  121. package/dist/_chunks/_presets/render.mjs +14 -0
  122. package/dist/_chunks/_presets/standard.mjs +23 -0
  123. package/dist/_chunks/_presets/stormkit.mjs +18 -0
  124. package/dist/_chunks/_presets/vercel.mjs +375 -0
  125. package/dist/_chunks/_presets/winterjs.mjs +22 -0
  126. package/dist/_chunks/_presets/zeabur.mjs +69 -0
  127. package/dist/_chunks/_presets/zerops.mjs +27 -0
  128. package/dist/_chunks/app.mjs +9 -19206
  129. package/dist/_chunks/{index3.mjs → builder.mjs} +560 -970
  130. package/dist/_chunks/server.mjs +6 -4
  131. package/dist/{cli → _cli}/build.mjs +3 -8
  132. package/dist/{cli → _cli}/dev.mjs +38 -12
  133. package/dist/{cli/index2.mjs → _cli/index.mjs} +1 -2
  134. package/dist/{cli → _cli}/list.mjs +3 -4
  135. package/dist/{cli → _cli}/prepare.mjs +3 -4
  136. package/dist/{cli → _cli}/run.mjs +3 -4
  137. package/dist/{index.d.mts → builder.d.mts} +11 -11
  138. package/dist/builder.mjs +117 -0
  139. package/dist/cli/index.mjs +7 -464
  140. package/dist/node_modules/@speed-highlight/core/dist/index.js +1 -1
  141. package/dist/node_modules/@speed-highlight/core/dist/terminal.js +1 -1
  142. package/dist/node_modules/@speed-highlight/core/package.json +7 -3
  143. package/dist/node_modules/cookie-es/dist/index.mjs +262 -0
  144. package/dist/node_modules/cookie-es/package.json +37 -0
  145. package/dist/node_modules/hookable/dist/index.mjs +243 -266
  146. package/dist/node_modules/hookable/package.json +29 -26
  147. package/dist/node_modules/rendu/dist/index.mjs +380 -0
  148. package/dist/node_modules/rendu/package.json +47 -0
  149. package/dist/presets/_nitro/runtime/nitro-dev.mjs +4 -7
  150. package/dist/presets/_nitro/runtime/nitro-prerenderer.mjs +5 -4
  151. package/dist/presets/azure/runtime/azure-swa.mjs +1 -1
  152. package/dist/presets/cloudflare/runtime/_module-handler.mjs +7 -6
  153. package/dist/presets/cloudflare/runtime/cloudflare-durable.mjs +4 -5
  154. package/dist/presets/cloudflare/runtime/plugin.dev.mjs +7 -10
  155. package/dist/presets/cloudflare/runtime/shims/workers.dev.d.mts +21 -0
  156. package/dist/presets/cloudflare/runtime/shims/workers.dev.mjs +27 -0
  157. package/dist/presets/node/runtime/node-server.mjs +1 -1
  158. package/dist/runtime/index.d.mts +1 -1
  159. package/dist/runtime/index.mjs +1 -1
  160. package/dist/runtime/internal/app.d.mts +3 -1
  161. package/dist/runtime/internal/app.mjs +90 -64
  162. package/dist/runtime/internal/error/prod.d.mts +3 -2
  163. package/dist/runtime/internal/error/prod.mjs +9 -13
  164. package/dist/runtime/internal/renderer.mjs +4 -4
  165. package/dist/runtime/internal/routes/dev-tasks.d.mts +30 -2
  166. package/dist/runtime/internal/shutdown.d.mts +1 -2
  167. package/dist/runtime/internal/shutdown.mjs +3 -2
  168. package/dist/runtime/internal/task.mjs +1 -2
  169. package/dist/types/index.d.mts +1603 -10
  170. package/dist/vite.d.mts +5 -0
  171. package/dist/vite.mjs +94 -30
  172. package/lib/index.mjs +1 -0
  173. package/lib/indexd.mts +1 -0
  174. package/lib/runtime/meta.mjs +35 -0
  175. package/package.json +40 -41
  176. package/dist/_chunks/build.mjs +0 -84
  177. package/dist/_chunks/build3.mjs +0 -6452
  178. package/dist/_chunks/detect-acorn.mjs +0 -503
  179. package/dist/_chunks/index.mjs +0 -22242
  180. package/dist/_chunks/json5.mjs +0 -68
  181. package/dist/_chunks/jsonc.mjs +0 -51
  182. package/dist/_chunks/toml.mjs +0 -259
  183. package/dist/_chunks/yaml.mjs +0 -86
  184. package/dist/index.mjs +0 -55
  185. package/dist/node_modules/klona/dist/index.mjs +0 -81
  186. package/dist/node_modules/klona/full/index.mjs +0 -53
  187. package/dist/node_modules/klona/package.json +0 -74
  188. package/dist/node_modules/std-env/dist/index.mjs +0 -1
  189. package/dist/node_modules/std-env/package.json +0 -46
  190. package/dist/presets.mjs +0 -2460
  191. package/dist/runtime/internal/debug.d.mts +0 -2
  192. package/dist/runtime/internal/debug.mjs +0 -5
  193. package/lib/runtime-meta.mjs +0 -38
  194. /package/dist/{cli → _cli}/common.mjs +0 -0
  195. /package/lib/{runtime-meta.d.mts → runtime/meta.d.mts} +0 -0
@@ -1,38 +1,46 @@
1
- import { s as sanitizeFilePath, f as formatCompatibilityDate, d as copyPublicAssets, a as scanHandlers, r as resolveModulePath, e as prettyPath, i as createNitro, j as prepare } from './index.mjs';
2
- import { fileURLToPath } from 'node:url';
3
- import { colors } from 'consola/utils';
1
+ import { e as copyPublicAssets, s as scanHandlers, h as prettyPath, p as prepare, c as createNitro } from './prepare.mjs';
2
+ import consola from 'consola';
3
+ import { existsSync, watch as watch$1 } from 'node:fs';
4
4
  import { defu } from 'defu';
5
5
  import 'ufo';
6
- import { existsSync, watch as watch$1 } from 'node:fs';
6
+ import { runtimeDir, runtimeDependencies } from 'nitro/runtime/meta';
7
+ import { r as resolveModulePath } from '../_chunks/_deps/exsolve.mjs';
7
8
  import { readFile, rm, mkdir, writeFile, readlink } from 'node:fs/promises';
8
9
  import 'node:zlib';
10
+ import '../_chunks/_deps/mime.mjs';
9
11
  import 'node:worker_threads';
10
- import consola from 'consola';
11
- import 'std-env';
12
12
  import 'h3';
13
13
  import 'undici';
14
14
  import 'nitro/meta';
15
15
  import { NodeRequest, sendNodeResponse } from 'srvx/node';
16
- import { d as debounce, w as watch, a as NodeDevWorker, c as createProxyServer, N as NitroDevApp } from './app.mjs';
17
- import 'klona/full';
18
- import { runtimeDir, runtimeDependencies } from 'nitro/runtime/meta';
16
+ import { a as NodeDevWorker, N as NitroDevApp } from '../_chunks/app.mjs';
17
+ import 'node:url';
18
+ import { colors } from 'consola/utils';
19
19
  import 'ofetch';
20
- import { a as alias, i as inject } from './index2.mjs';
21
- import { b as baseBuildConfig, d as baseBuildPlugins, r as replace, e as writeBuildInfo } from './info.mjs';
22
- import { r as resolve, d as dirname, n as normalize, b as basename, a as relative, j as join, i as isAbsolute } from './pathe.M-eThtNZ.mjs';
20
+ import { s as sanitizeFilePath } from '../_chunks/_deps/mlly.mjs';
21
+ import { a as alias } from '../_chunks/_deps/@rollup/plugin-alias.mjs';
22
+ import { i as inject } from '../_chunks/_deps/@rollup/plugin-inject.mjs';
23
+ import { b as baseBuildConfig, a as baseBuildPlugins, r as replace, w as writeBuildInfo } from './info.mjs';
24
+ import { r as resolve, d as dirname, n as normalize, b as basename, a as relative, j as join, i as isAbsolute } from '../_chunks/_deps/pathe.mjs';
25
+ import { f as formatCompatibilityDate } from '../_chunks/_deps/compatx.mjs';
23
26
  import 'hookable';
24
27
  import 'jiti';
25
- import 'klona';
26
28
  import 'unstorage';
27
29
  import 'ohash';
30
+ import { a, T } from '../_chunks/_deps/std-env.mjs';
31
+ import '../_chunks/_deps/serve-static.mjs';
28
32
  import { resolve as resolve$1, join as join$1 } from 'node:path';
29
33
  import 'youch-core';
30
34
  import 'youch';
31
35
  import 'source-map';
32
36
  import 'srvx';
33
37
  import { DevEnvironment } from 'vite';
38
+ import { watch } from '../_chunks/_deps/chokidar.mjs';
39
+ import { d as debounce } from '../_chunks/_deps/perfect-debounce.mjs';
34
40
  import { getRandomPort } from 'get-port-please';
35
41
  import { spawn } from 'node:child_process';
42
+ import { c as createProxyServer } from '../_chunks/_deps/httpxy.mjs';
43
+ import { a as assetsPlugin } from '../_chunks/_deps/@pi0/vite-plugin-fullstack.mjs';
36
44
 
37
45
  const getViteRollupConfig = (ctx) => {
38
46
  const nitro = ctx.nitro;
@@ -42,7 +50,6 @@ const getViteRollupConfig = (ctx) => {
42
50
  [base.buildServerDir, "app"],
43
51
  [runtimeDir, "nitro"],
44
52
  [base.presetsDir, "nitro"],
45
- ["\0raw:", "raw"],
46
53
  ["\0nitro-wasm:", "wasm"],
47
54
  ["\0", "virtual"]
48
55
  ];
@@ -58,7 +65,11 @@ const getViteRollupConfig = (ctx) => {
58
65
  ctx.pluginConfig.experimental?.virtualBundle && virtualBundlePlugin(ctx._serviceBundles),
59
66
  ...baseBuildPlugins(nitro, base),
60
67
  alias({ entries: base.aliases }),
61
- replace({ preventAssignment: true, values: base.replacements }),
68
+ replace({
69
+ delimiters: base.replaceDelimiters,
70
+ preventAssignment: true,
71
+ values: base.replacements
72
+ }),
62
73
  inject(base.env.inject)
63
74
  ].filter(Boolean),
64
75
  treeshake: {
@@ -193,7 +204,7 @@ async function buildEnvironments(ctx, builder) {
193
204
  }
194
205
  continue;
195
206
  }
196
- console.log();
207
+ if (!a && !T) console.log();
197
208
  nitro.logger.start(`Building [${fmtName}]`);
198
209
  await builder.build(env);
199
210
  }
@@ -218,7 +229,8 @@ async function buildEnvironments(ctx, builder) {
218
229
  nitroOptions.renderer.template = tmp;
219
230
  }
220
231
  }
221
- console.log();
232
+ await builder.writeAssetsManifest?.();
233
+ if (!a && !T) console.log();
222
234
  const buildInfo = [
223
235
  ["preset", nitro.options.preset],
224
236
  ["compatibility", formatCompatibilityDate(nitro.options.compatibilityDate)]
@@ -227,11 +239,6 @@ async function buildEnvironments(ctx, builder) {
227
239
  `Building [${BuilderNames.nitro}] ${colors.dim(`(${buildInfo.map(([k, v]) => `${k}: \`${v}\``).join(", ")})`)}`
228
240
  );
229
241
  await copyPublicAssets(nitro);
230
- await nitro.hooks.callHook(
231
- "rollup:before",
232
- nitro,
233
- builder.environments.nitro.config.build.rollupOptions
234
- );
235
242
  await builder.build(builder.environments.nitro);
236
243
  await nitro.close();
237
244
  await nitro.hooks.callHook("compiled", nitro);
@@ -240,7 +247,7 @@ async function buildEnvironments(ctx, builder) {
240
247
  const rewriteRelativePaths = (input) => {
241
248
  return input.replace(/([\s:])\.\/(\S*)/g, `$1${rOutput}/$2`);
242
249
  };
243
- console.log();
250
+ if (!a && !T) console.log();
244
251
  if (nitro.options.commands.preview) {
245
252
  nitro.logger.success(
246
253
  `You can preview this build using \`${rewriteRelativePaths(
@@ -446,7 +453,8 @@ async function configureViteDevServer(ctx, server) {
446
453
  if (fetchDest) {
447
454
  res.setHeader("vary", "sec-fetch-dest");
448
455
  }
449
- if (!fetchDest || /^(document|iframe|frame|empty)$/.test(fetchDest)) {
456
+ const ext = (req.url || "").match(/\.([a-z0-9]+)(?:[?#]|$)/i)?.[1] || "";
457
+ if (!ext && (!fetchDest || /^(document|iframe|frame|empty)$/.test(fetchDest))) {
450
458
  nitroDevMiddleware(req, res, next);
451
459
  } else {
452
460
  next();
@@ -489,11 +497,7 @@ function createNitroEnvironment(ctx) {
489
497
  noExternal: ctx.nitro.options.dev ? (
490
498
  // Workaround for dev: external dependencies are not resolvable with respect to nodeModulePaths
491
499
  new RegExp(runtimeDependencies.join("|"))
492
- ) : (
493
- // Workaround for build: externals tracing is unstable
494
- ctx.nitro.options.noExternals === false ? void 0 : true
495
- ),
496
- // prettier-ignore
500
+ ) : true,
497
501
  conditions: ctx.nitro.options.exportConditions,
498
502
  externalConditions: ctx.nitro.options.exportConditions
499
503
  },
@@ -517,7 +521,6 @@ function createServiceEnvironment(ctx, name, serviceConfig) {
517
521
  emptyOutDir: true
518
522
  },
519
523
  resolve: {
520
- noExternal: ctx.nitro.options.dev ? void 0 : true,
521
524
  conditions: ctx.nitro.options.exportConditions,
522
525
  externalConditions: ctx.nitro.options.exportConditions
523
526
  },
@@ -633,280 +636,218 @@ function nitroPreviewPlugin(ctx) {
633
636
  }
634
637
 
635
638
  const DEFAULT_EXTENSIONS = [".ts", ".js", ".mts", ".mjs", ".tsx", ".jsx"];
639
+ const debug = process.env.NITRO_DEBUG ? (...args) => console.log("[nitro]", ...args) : () => {
640
+ };
636
641
  function nitro(pluginConfig = {}) {
637
- const ctx = {
638
- pluginConfig,
639
- _entryPoints: {},
640
- _manifest: {},
641
- _serviceBundles: {}
642
- };
642
+ const ctx = createContext(pluginConfig);
643
643
  return [
644
- nitroPlugin(ctx),
645
- nitroServicePlugin(ctx),
644
+ nitroInit(ctx),
645
+ nitroEnv(ctx),
646
+ nitroMain(ctx),
647
+ nitroPrepare(ctx),
648
+ nitroService(ctx),
646
649
  nitroPreviewPlugin(),
647
- nitroRollupPlugins(ctx)
650
+ pluginConfig.experimental?.assetsImport !== false && assetsPlugin({
651
+ experimental: {
652
+ // See https://github.com/hi-ogawa/vite-plugins/pull/1289
653
+ clientBuildFallback: false
654
+ }
655
+ })
648
656
  ];
649
657
  }
650
- function nitroPlugin(ctx) {
651
- return [
652
- {
653
- name: "nitro:main",
654
- // Opt-in this plugin into the shared plugins pipeline
655
- sharedDuringBuild: true,
656
- // Only apply this plugin during build or dev
657
- apply: (config, configEnv) => !configEnv.isPreview,
658
- // Extend vite config before it's resolved
659
- async config(userConfig, configEnv) {
660
- ctx.nitro = ctx.pluginConfig._nitro || await createNitro({
661
- dev: configEnv.mode === "development",
662
- rootDir: userConfig.root,
663
- ...defu(ctx.pluginConfig.config, userConfig.nitro)
664
- });
665
- if (!ctx.pluginConfig.services?.ssr) {
666
- ctx.pluginConfig.services ??= {};
667
- if (userConfig.environments?.ssr === void 0) {
668
- const ssrEntry = resolveModulePath("./entry-server", {
669
- from: ["", "app", "src"].flatMap(
670
- (d) => ctx.nitro.options.scanDirs.map((s) => join(s, d) + "/")
671
- ),
672
- extensions: DEFAULT_EXTENSIONS,
673
- try: true
674
- });
675
- if (ssrEntry) {
676
- ctx.pluginConfig.services.ssr = { entry: ssrEntry };
677
- ctx.nitro.logger.info(
678
- `Using \`${prettyPath(ssrEntry)}\` as vite ssr entry.`
679
- );
680
- }
681
- } else {
682
- let ssrEntry = getEntry(
683
- userConfig.environments.ssr.build?.rollupOptions?.input
684
- );
685
- if (typeof ssrEntry === "string") {
686
- ssrEntry = resolveModulePath(ssrEntry, {
687
- from: ctx.nitro.options.scanDirs,
688
- extensions: DEFAULT_EXTENSIONS,
689
- suffixes: ["", "/index"],
690
- try: true
691
- }) || ssrEntry;
692
- ctx.pluginConfig.services.ssr = { entry: ssrEntry };
693
- } else {
694
- this.error(`Invalid input type for SSR entry point.`);
695
- }
658
+ function nitroInit(ctx) {
659
+ return {
660
+ name: "nitro:init",
661
+ sharedDuringBuild: true,
662
+ apply: (_config, configEnv) => !configEnv.isPreview,
663
+ async config(config, configEnv) {
664
+ if (!ctx._initialized) {
665
+ debug("[init] Initializing nitro");
666
+ ctx._initialized = true;
667
+ await setupNitroContext(ctx, configEnv, config);
668
+ }
669
+ },
670
+ applyToEnvironment(env) {
671
+ if (env.name === "nitro" && ctx.nitro?.options.dev) {
672
+ debug("[init] Adding rollup plugins for dev");
673
+ return [...ctx.rollupConfig?.config.plugins || []];
674
+ }
675
+ }
676
+ };
677
+ }
678
+ function nitroEnv(ctx) {
679
+ return {
680
+ name: "nitro:env",
681
+ sharedDuringBuild: true,
682
+ apply: (_config, configEnv) => !configEnv.isPreview,
683
+ async config(userConfig, _configEnv) {
684
+ debug("[env] Extending config (environments)");
685
+ const environments = {
686
+ ...createServiceEnvironments(ctx),
687
+ nitro: createNitroEnvironment(ctx)
688
+ };
689
+ environments.client = {
690
+ consumer: userConfig.environments?.client?.consumer ?? "client",
691
+ build: {
692
+ rollupOptions: {
693
+ input: userConfig.environments?.client?.build?.rollupOptions?.input ?? useNitro(ctx).options.renderer?.template
696
694
  }
697
695
  }
698
- if (!ctx.nitro.options.renderer?.entry && !ctx.nitro.options.renderer?.template && ctx.pluginConfig.services.ssr?.entry) {
699
- ctx.nitro.options.renderer ??= {};
700
- ctx.nitro.options.renderer.entry = resolve(
701
- runtimeDir,
702
- "internal/vite/ssr-renderer"
703
- );
704
- }
705
- const publicDistDir = ctx._publicDistDir = userConfig.build?.outDir || resolve(ctx.nitro.options.buildDir, "vite/public");
706
- ctx.nitro.options.publicAssets.push({
707
- dir: publicDistDir,
708
- maxAge: 0,
709
- baseURL: "/",
710
- fallthrough: true
711
- });
712
- if (!ctx.nitro.options.dev) {
713
- ctx.nitro.options.unenv.push({
714
- meta: { name: "nitro-vite" },
715
- polyfill: ["#nitro-vite-setup"]
716
- });
717
- }
718
- await ctx.nitro.hooks.callHook("build:before", ctx.nitro);
719
- ctx.rollupConfig = await getViteRollupConfig(ctx);
720
- if (ctx.nitro.options.dev && !ctx.devWorker) {
721
- ctx.devWorker = createDevWorker(ctx);
696
+ };
697
+ debug("[env] Environments:", Object.keys(environments).join(", "));
698
+ return {
699
+ environments
700
+ };
701
+ },
702
+ configEnvironment(name, config) {
703
+ if (config.consumer === "client") {
704
+ debug(
705
+ "[env] Configuring client environment",
706
+ name === "client" ? "" : ` (${name})`
707
+ );
708
+ config.build.emptyOutDir = false;
709
+ config.build.outDir = useNitro(ctx).options.output.publicDir;
710
+ } else {
711
+ if (ctx.pluginConfig.experimental?.virtualBundle && name in (ctx.pluginConfig.services || {})) {
712
+ debug("[env] Configuring service environment for virtual:", name);
713
+ config.build ??= {};
714
+ config.build.write = config.build.write ?? false;
722
715
  }
723
- if (ctx.nitro.options.dev && !ctx.devApp) {
724
- ctx.devApp = new NitroDevApp(ctx.nitro);
716
+ }
717
+ }
718
+ };
719
+ }
720
+ function nitroMain(ctx) {
721
+ return {
722
+ name: "nitro:main",
723
+ sharedDuringBuild: true,
724
+ apply: (_config, configEnv) => !configEnv.isPreview,
725
+ async config(userConfig, _configEnv) {
726
+ debug("[main] Extending config (appType, resolve, server)");
727
+ if (!ctx.rollupConfig) {
728
+ throw new Error("Nitro rollup config is not initialized yet.");
729
+ }
730
+ return {
731
+ appType: userConfig.appType || "custom",
732
+ resolve: {
733
+ // TODO: environment specific aliases not working
734
+ // https://github.com/vitejs/vite/pull/17583 (seems not effective)
735
+ alias: ctx.rollupConfig.base.aliases
736
+ },
737
+ builder: {
738
+ sharedConfigBuild: true
739
+ },
740
+ server: {
741
+ port: Number.parseInt(process.env.PORT || "") || userConfig.server?.port || useNitro(ctx).options.devServer?.port || 3e3,
742
+ // #3673, disable Vite's `cors` by default as Nitro handles all requests
743
+ cors: false
725
744
  }
726
- return {
727
- // Don't include HTML middlewares
728
- appType: userConfig.appType || "custom",
729
- // Add Nitro as a Vite environment
730
- environments: {
731
- client: {
732
- consumer: userConfig.environments?.client?.consumer ?? "client",
733
- build: {
734
- rollupOptions: {
735
- input: userConfig.environments?.client?.build?.rollupOptions?.input ?? ctx.nitro.options.renderer?.template
736
- }
737
- }
738
- },
739
- ...createServiceEnvironments(ctx),
740
- nitro: createNitroEnvironment(ctx)
741
- },
742
- resolve: {
743
- // TODO: environment specific aliases not working
744
- // https://github.com/vitejs/vite/pull/17583 (seems not effective)
745
- alias: ctx.rollupConfig.base.aliases
746
- },
747
- build: {
748
- // TODO: Support server environment emitted assets
749
- assetsInlineLimit: 4096 * 4
750
- },
751
- builder: {
752
- /// Share the config instance among environments to align with the behavior of dev server
753
- sharedConfigBuild: true
754
- },
755
- server: {
756
- port: Number.parseInt(process.env.PORT || "") || userConfig.server?.port || ctx.nitro.options.devServer?.port || 3e3
757
- }
758
- };
759
- },
760
- configResolved(config) {
761
- if (config.command === "build") {
762
- for (const env of Object.values(config.environments)) {
763
- if (env.consumer === "client") {
764
- const { assetsDir } = env.build;
765
- const rule = ctx.nitro.options.routeRules[`/${assetsDir}/**`] ??= {};
766
- if (!rule.headers?.["cache-control"]) {
767
- rule.headers = {
768
- ...rule.headers,
769
- "cache-control": `public, max-age=31536000, immutable`
770
- };
771
- }
745
+ };
746
+ },
747
+ configResolved(config) {
748
+ if (config.command === "build") {
749
+ debug("[main] Inferring caching routes");
750
+ for (const env of Object.values(config.environments)) {
751
+ if (env.consumer === "client") {
752
+ const rule = ctx.nitro.options.routeRules[`/${env.build.assetsDir}/**`] ??= {};
753
+ if (!rule.headers?.["cache-control"]) {
754
+ rule.headers = {
755
+ ...rule.headers,
756
+ "cache-control": `public, max-age=31536000, immutable`
757
+ };
772
758
  }
773
759
  }
774
760
  }
775
- ctx.nitro.routing.sync();
776
- },
777
- buildApp: {
778
- order: "post",
779
- handler(builder) {
780
- return buildEnvironments(ctx, builder);
781
- }
782
- },
783
- generateBundle: {
784
- handler(_options, bundle) {
785
- const { root } = this.environment.config;
786
- const services = ctx.pluginConfig.services || {};
787
- const serviceNames = Object.keys(services);
788
- const isRegisteredService = serviceNames.includes(
789
- this.environment.name
790
- );
791
- let entryFile;
792
- for (const [_name, file] of Object.entries(bundle)) {
793
- if (file.type === "chunk") {
794
- if (isRegisteredService && file.isEntry) {
795
- if (entryFile !== void 0) {
796
- this.error(
797
- `Multiple entry points found for service "${this.environment.name}". Only one entry point is allowed.`
798
- );
799
- }
761
+ }
762
+ debug("[main] Syncing nitro routes");
763
+ ctx.nitro.routing.sync();
764
+ },
765
+ buildApp: {
766
+ order: "post",
767
+ handler(builder) {
768
+ debug("[main] Building environments");
769
+ return buildEnvironments(ctx, builder);
770
+ }
771
+ },
772
+ generateBundle: {
773
+ handler(_options, bundle) {
774
+ const environment = this.environment;
775
+ debug(
776
+ "[main] Generating manifest and entry points for environment:",
777
+ environment.name
778
+ );
779
+ const { root } = environment.config;
780
+ const services = ctx.pluginConfig.services || {};
781
+ const serviceNames = Object.keys(services);
782
+ const isRegisteredService = serviceNames.includes(environment.name);
783
+ let entryFile;
784
+ for (const [_name, file] of Object.entries(bundle)) {
785
+ if (file.type === "chunk") {
786
+ if (isRegisteredService && file.isEntry) {
787
+ if (entryFile === void 0) {
800
788
  entryFile = file.fileName;
801
- }
802
- const filteredModuleIds = file.moduleIds.filter(
803
- (id) => id.startsWith(root)
804
- );
805
- for (const id of filteredModuleIds) {
806
- const originalFile = relative(root, id);
807
- ctx._manifest[originalFile] = { file: file.fileName };
789
+ } else {
790
+ this.warn(
791
+ `Multiple entry points found for service "${environment.name}"`
792
+ );
808
793
  }
809
794
  }
810
- }
811
- if (isRegisteredService) {
812
- if (entryFile === void 0) {
813
- this.error(
814
- `No entry point found for service "${this.environment.name}".`
815
- );
795
+ const filteredModuleIds = file.moduleIds.filter(
796
+ (id) => id.startsWith(root)
797
+ );
798
+ for (const id of filteredModuleIds) {
799
+ const originalFile = relative(root, id);
800
+ ctx._manifest[originalFile] = { file: file.fileName };
816
801
  }
817
- ctx._entryPoints[this.environment.name] = entryFile;
818
- ctx._serviceBundles[this.environment.name] = bundle;
819
802
  }
820
803
  }
821
- },
822
- // Modify environment configs before it's resolved.
823
- configEnvironment(name, config) {
824
- if (config.consumer === "client") {
825
- config.build.emptyOutDir = false;
826
- config.build.outDir = ctx.nitro.options.output.publicDir;
827
- }
828
- const services = ctx.pluginConfig.services || {};
829
- const serviceNames = Object.keys(services);
830
- if (serviceNames.includes(name) && ctx.pluginConfig.experimental?.virtualBundle) {
831
- config.build ??= {};
832
- config.build.write = config.build.write ?? false;
804
+ if (isRegisteredService) {
805
+ if (entryFile === void 0) {
806
+ this.error(
807
+ `No entry point found for service "${this.environment.name}".`
808
+ );
809
+ }
810
+ ctx._entryPoints[this.environment.name] = entryFile;
811
+ ctx._serviceBundles[this.environment.name] = bundle;
833
812
  }
834
- },
835
- // Extend Vite dev server with Nitro middleware
836
- configureServer: (server) => configureViteDevServer(ctx, server)
813
+ }
837
814
  },
838
- {
839
- name: "nitro:prepare",
840
- buildApp: {
841
- // clean the output directory before any environment is built
842
- order: "pre",
843
- async handler() {
844
- const nitro2 = ctx.nitro;
845
- await prepare(nitro2);
846
- }
815
+ configureServer: (server) => {
816
+ debug("[main] Configuring dev server");
817
+ return configureViteDevServer(ctx, server);
818
+ }
819
+ };
820
+ }
821
+ function nitroPrepare(ctx) {
822
+ return {
823
+ name: "nitro:prepare",
824
+ sharedDuringBuild: true,
825
+ applyToEnvironment: (env) => env.name === "nitro",
826
+ buildApp: {
827
+ // Clean the output directory before any environment is built
828
+ order: "pre",
829
+ async handler() {
830
+ debug("[prepare] Preparing output directory");
831
+ const nitro2 = ctx.nitro;
832
+ await prepare(nitro2);
847
833
  }
848
834
  }
849
- ];
835
+ };
850
836
  }
851
- function nitroServicePlugin(ctx) {
837
+ function nitroService(ctx) {
852
838
  return {
853
839
  name: "nitro:service",
854
840
  enforce: "pre",
855
- // Only apply this plugin to the nitro environment
841
+ sharedDuringBuild: true,
856
842
  applyToEnvironment: (env) => env.name === "nitro",
857
843
  resolveId: {
858
- async handler(id, importer, options) {
844
+ async handler(id) {
859
845
  if (id === "#nitro-vite-setup") {
860
846
  return { id, moduleSideEffects: true };
861
847
  }
862
848
  if (id === "#nitro-vite-services") {
863
849
  return id;
864
850
  }
865
- if (runtimeDependencies.some(
866
- (dep) => id === dep || id.startsWith(`${dep}/`)
867
- )) {
868
- const resolved = await this.resolve(id, importer, {
869
- ...options,
870
- skipSelf: true
871
- });
872
- return resolved || resolveModulePath(id, {
873
- from: ctx.nitro.options.nodeModulesDirs,
874
- conditions: ctx.nitro.options.exportConditions,
875
- try: true
876
- });
877
- }
878
- if (importer?.startsWith("\0virtual:#nitro-internal-virtual")) {
879
- const internalRes = await this.resolve(id, import.meta.url, {
880
- ...options,
881
- custom: { ...options.custom, skipNoExternals: true }
882
- });
883
- if (internalRes) {
884
- return internalRes;
885
- }
886
- const resolvedFromRoot = await this.resolve(
887
- id,
888
- ctx.nitro.options.rootDir,
889
- { ...options, custom: { ...options.custom, skipNoExternals: true } }
890
- );
891
- if (resolvedFromRoot) {
892
- return resolvedFromRoot;
893
- }
894
- const ids = [id];
895
- if (!/^[./@#]/.test(id)) {
896
- ids.push(`./${id}`);
897
- }
898
- for (const _id of ids) {
899
- const resolved = resolveModulePath(_id, {
900
- from: process.cwd(),
901
- extensions: DEFAULT_EXTENSIONS,
902
- suffixes: ["", "/index"],
903
- try: true
904
- });
905
- if (resolved) {
906
- return resolved;
907
- }
908
- }
909
- }
910
851
  }
911
852
  },
912
853
  load: {
@@ -918,35 +859,93 @@ function nitroServicePlugin(ctx) {
918
859
  }
919
860
  };
920
861
  }
921
- function nitroRollupPlugins(ctx) {
922
- const createHookCaller = (hook, order) => {
923
- const handler = async function(...args) {
924
- for (const plugin of ctx.rollupConfig.config.plugins) {
925
- if (typeof plugin[hook] !== "function") continue;
926
- const res = await plugin[hook].call(this, ...args);
927
- if (res) {
928
- if (hook === "resolveId" && res.id?.startsWith?.("file://")) {
929
- res.id = fileURLToPath(res.id);
930
- }
931
- return res;
932
- }
933
- }
934
- };
935
- Object.defineProperty(handler, "name", { value: hook });
936
- return order ? { order, handler } : handler;
937
- };
862
+ function createContext(pluginConfig) {
938
863
  return {
939
- name: "nitro:rollup-hooks",
940
- applyToEnvironment: (env) => env.name === "nitro",
941
- buildStart: createHookCaller("buildStart", "pre"),
942
- resolveId: createHookCaller("resolveId", "pre"),
943
- load: createHookCaller("load", "pre"),
944
- transform: createHookCaller("transform", "post"),
945
- renderChunk: createHookCaller("renderChunk", "post"),
946
- generateBundle: createHookCaller("generateBundle", "post"),
947
- buildEnd: createHookCaller("buildEnd", "post")
864
+ pluginConfig,
865
+ _entryPoints: {},
866
+ _manifest: {},
867
+ _serviceBundles: {}
948
868
  };
949
869
  }
870
+ function useNitro(ctx) {
871
+ if (!ctx.nitro) {
872
+ throw new Error("Nitro instance is not initialized yet.");
873
+ }
874
+ return ctx.nitro;
875
+ }
876
+ async function setupNitroContext(ctx, configEnv, userConfig) {
877
+ ctx.nitro = ctx.pluginConfig._nitro || await createNitro({
878
+ dev: configEnv.mode === "development",
879
+ rootDir: userConfig.root,
880
+ ...defu(ctx.pluginConfig.config, userConfig.nitro)
881
+ });
882
+ if (!ctx.pluginConfig.services?.ssr) {
883
+ ctx.pluginConfig.services ??= {};
884
+ if (userConfig.environments?.ssr === void 0) {
885
+ const ssrEntry = resolveModulePath("./entry-server", {
886
+ from: ["", "app", "src"].flatMap(
887
+ (d) => ctx.nitro.options.scanDirs.map((s) => join(s, d) + "/")
888
+ ),
889
+ extensions: DEFAULT_EXTENSIONS,
890
+ try: true
891
+ });
892
+ if (ssrEntry) {
893
+ ctx.pluginConfig.services.ssr = { entry: ssrEntry };
894
+ ctx.nitro.logger.info(
895
+ `Using \`${prettyPath(ssrEntry)}\` as vite ssr entry.`
896
+ );
897
+ }
898
+ } else {
899
+ let ssrEntry = getEntry(
900
+ userConfig.environments.ssr.build?.rollupOptions?.input
901
+ );
902
+ if (typeof ssrEntry === "string") {
903
+ ssrEntry = resolveModulePath(ssrEntry, {
904
+ from: ctx.nitro.options.scanDirs,
905
+ extensions: DEFAULT_EXTENSIONS,
906
+ suffixes: ["", "/index"],
907
+ try: true
908
+ }) || ssrEntry;
909
+ ctx.pluginConfig.services.ssr = { entry: ssrEntry };
910
+ } else {
911
+ throw new TypeError(`Invalid input type for SSR entry point.`);
912
+ }
913
+ }
914
+ }
915
+ if (!ctx.nitro.options.renderer?.entry && !ctx.nitro.options.renderer?.template && ctx.pluginConfig.services.ssr?.entry) {
916
+ ctx.nitro.options.renderer ??= {};
917
+ ctx.nitro.options.renderer.entry = resolve(
918
+ runtimeDir,
919
+ "internal/vite/ssr-renderer"
920
+ );
921
+ }
922
+ const publicDistDir = ctx._publicDistDir = userConfig.build?.outDir || resolve(ctx.nitro.options.buildDir, "vite/public");
923
+ ctx.nitro.options.publicAssets.push({
924
+ dir: publicDistDir,
925
+ maxAge: 0,
926
+ baseURL: "/",
927
+ fallthrough: true
928
+ });
929
+ if (!ctx.nitro.options.dev) {
930
+ ctx.nitro.options.unenv.push({
931
+ meta: { name: "nitro-vite" },
932
+ polyfill: ["#nitro-vite-setup"]
933
+ });
934
+ }
935
+ await ctx.nitro.hooks.callHook("build:before", ctx.nitro);
936
+ ctx.rollupConfig = await getViteRollupConfig(ctx);
937
+ await ctx.nitro.hooks.callHook(
938
+ "rollup:before",
939
+ ctx.nitro,
940
+ ctx.rollupConfig.config
941
+ );
942
+ if (ctx.nitro.options.dev && !ctx.devWorker) {
943
+ ctx.devWorker = createDevWorker(ctx);
944
+ }
945
+ if (ctx.nitro.options.dev && !ctx.devApp) {
946
+ ctx.devApp = new NitroDevApp(ctx.nitro);
947
+ }
948
+ }
950
949
  function getEntry(input) {
951
950
  if (typeof input === "string") {
952
951
  return input;