@analogjs/platform 3.0.0-beta.6 → 3.0.0-beta.61

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 (175) hide show
  1. package/migrations/migrate-setup-testbed-browser-mode/migrate-setup-testbed-browser-mode.js +1 -2
  2. package/migrations/migrate-to-separated-plugins/migrate-to-separated-plugins.js +3 -3
  3. package/migrations/migrate-to-separated-plugins/migrate-to-separated-plugins.js.map +1 -1
  4. package/migrations/migrate-vitest-builder-aliases/migrate-vitest-builder-aliases.js +3 -3
  5. package/migrations/migrate-vitest-builder-aliases/migrate-vitest-builder-aliases.js.map +1 -1
  6. package/migrations/utils/source-edits.js +1 -2
  7. package/package.json +14 -11
  8. package/src/index.js +2 -3
  9. package/src/lib/analog-integration-plugin.js +1 -2
  10. package/src/lib/clear-client-page-endpoint.js +2 -3
  11. package/src/lib/content/marked/index.js +1 -2
  12. package/src/lib/content/marked/marked-content-highlighter.js +1 -2
  13. package/src/lib/content/marked/marked-setup.service.js +1 -2
  14. package/src/lib/content/prism/angular.js +1 -2
  15. package/src/lib/content/prism/index.js +1 -2
  16. package/src/lib/content/prism/prism-highlighter.js +1 -2
  17. package/src/lib/content/shiki/index.js +1 -2
  18. package/src/lib/content/shiki/shiki-highlighter.js +2 -3
  19. package/src/lib/content/utils/escape-html.js +1 -2
  20. package/src/lib/content-plugin.js +13 -5
  21. package/src/lib/content-plugin.js.map +1 -1
  22. package/src/lib/deps-plugin.d.ts +1 -1
  23. package/src/lib/deps-plugin.js +13 -10
  24. package/src/lib/deps-plugin.js.map +1 -1
  25. package/src/lib/discover-library-routes.d.ts +3 -3
  26. package/src/lib/discover-library-routes.js +5 -6
  27. package/src/lib/discover-library-routes.js.map +1 -1
  28. package/src/lib/get-page-endpoints.d.ts +25 -0
  29. package/src/lib/get-page-endpoints.js +28 -0
  30. package/src/lib/get-page-endpoints.js.map +1 -0
  31. package/src/lib/inject-server-fn-ids.js +1 -2
  32. package/src/lib/nitro/analog-nitro-plugin.d.ts +6 -6
  33. package/src/lib/nitro/analog-nitro-plugin.js +119 -51
  34. package/src/lib/nitro/analog-nitro-plugin.js.map +1 -1
  35. package/src/lib/nitro/angular-linker-plugin.d.ts +4 -2
  36. package/src/lib/nitro/angular-linker-plugin.js +25 -40
  37. package/src/lib/nitro/angular-linker-plugin.js.map +1 -1
  38. package/src/lib/nitro/build-sitemap.js +3 -4
  39. package/src/lib/nitro/build-sitemap.js.map +1 -1
  40. package/src/lib/nitro/debug.js +1 -2
  41. package/src/lib/nitro/get-content-files.js +1 -2
  42. package/src/lib/nitro/get-page-handlers.d.ts +4 -19
  43. package/src/lib/nitro/get-page-handlers.js +10 -28
  44. package/src/lib/nitro/get-page-handlers.js.map +1 -1
  45. package/src/lib/nitro/get-server-fn-handlers.js +1 -2
  46. package/src/lib/nitro/i18n-prerender.d.ts +2 -1
  47. package/src/lib/nitro/i18n-prerender.js +11 -9
  48. package/src/lib/nitro/i18n-prerender.js.map +1 -1
  49. package/src/lib/nitro/page-endpoints-plugin.js +1 -2
  50. package/src/lib/nitro/post-rendering-hook.js +1 -2
  51. package/src/lib/nitro/renderers.js +1 -2
  52. package/src/lib/nitro/server-fn-endpoints.d.ts +3 -1
  53. package/src/lib/nitro/server-fn-endpoints.js +15 -7
  54. package/src/lib/nitro/server-fn-endpoints.js.map +1 -1
  55. package/src/lib/nx-plugin/_virtual/_rolldown/runtime.js +1 -2
  56. package/src/lib/nx-plugin/executors.json +0 -7
  57. package/src/lib/nx-plugin/node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/string/camelCase.js +1 -2
  58. package/src/lib/nx-plugin/node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/string/camelCase.js.map +1 -1
  59. package/src/lib/nx-plugin/node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/string/capitalize.js +1 -2
  60. package/src/lib/nx-plugin/node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/string/capitalize.js.map +1 -1
  61. package/src/lib/nx-plugin/node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/string/words.js +1 -2
  62. package/src/lib/nx-plugin/node_modules/.pnpm/es-toolkit@1.45.1/node_modules/es-toolkit/dist/string/words.js.map +1 -1
  63. package/src/lib/nx-plugin/src/builders/vite/vite-build.js +1 -2
  64. package/src/lib/nx-plugin/src/builders/vite/vite-build.js.map +1 -1
  65. package/src/lib/nx-plugin/src/builders/vite-dev-server/dev-server.js +1 -2
  66. package/src/lib/nx-plugin/src/builders/vite-dev-server/dev-server.js.map +1 -1
  67. package/src/lib/nx-plugin/src/generators/app/compat.d.ts +2 -1
  68. package/src/lib/nx-plugin/src/generators/app/compat.js +5 -3
  69. package/src/lib/nx-plugin/src/generators/app/compat.js.map +1 -1
  70. package/src/lib/nx-plugin/src/generators/app/generator.js +8 -4
  71. package/src/lib/nx-plugin/src/generators/app/generator.js.map +1 -1
  72. package/src/lib/nx-plugin/src/generators/app/lib/add-agent-context.js +1 -2
  73. package/src/lib/nx-plugin/src/generators/app/lib/add-agent-context.js.map +1 -1
  74. package/src/lib/nx-plugin/src/generators/app/lib/add-analog-project-config.js +1 -2
  75. package/src/lib/nx-plugin/src/generators/app/lib/add-analog-project-config.js.map +1 -1
  76. package/src/lib/nx-plugin/src/generators/app/lib/add-angular-app.js +13 -3
  77. package/src/lib/nx-plugin/src/generators/app/lib/add-angular-app.js.map +1 -1
  78. package/src/lib/nx-plugin/src/generators/app/lib/add-files.js +1 -2
  79. package/src/lib/nx-plugin/src/generators/app/lib/add-files.js.map +1 -1
  80. package/src/lib/nx-plugin/src/generators/app/lib/add-home-page.js +1 -2
  81. package/src/lib/nx-plugin/src/generators/app/lib/add-home-page.js.map +1 -1
  82. package/src/lib/nx-plugin/src/generators/app/lib/add-tailwind-config.js +1 -2
  83. package/src/lib/nx-plugin/src/generators/app/lib/add-tailwind-config.js.map +1 -1
  84. package/src/lib/nx-plugin/src/generators/app/lib/add-tailwind-helpers.js +3 -4
  85. package/src/lib/nx-plugin/src/generators/app/lib/add-tailwind-helpers.js.map +1 -1
  86. package/src/lib/nx-plugin/src/generators/app/lib/cleanup-files.js +1 -2
  87. package/src/lib/nx-plugin/src/generators/app/lib/update-index-html.js +1 -2
  88. package/src/lib/nx-plugin/src/generators/app/lib/update-index-html.js.map +1 -1
  89. package/src/lib/nx-plugin/src/generators/app/versions/minimum-supported-versions.d.ts +3 -1
  90. package/src/lib/nx-plugin/src/generators/app/versions/minimum-supported-versions.js +6 -3
  91. package/src/lib/nx-plugin/src/generators/app/versions/minimum-supported-versions.js.map +1 -1
  92. package/src/lib/nx-plugin/src/generators/app/versions/tailwind-dependencies.js +1 -2
  93. package/src/lib/nx-plugin/src/generators/app/versions/tailwind-dependencies.js.map +1 -1
  94. package/src/lib/nx-plugin/src/generators/app/versions/versions.js +1 -2
  95. package/src/lib/nx-plugin/src/generators/init/compat.d.ts +2 -1
  96. package/src/lib/nx-plugin/src/generators/init/compat.js +5 -3
  97. package/src/lib/nx-plugin/src/generators/init/compat.js.map +1 -1
  98. package/src/lib/nx-plugin/src/generators/init/files/src/server/routes/api/v1/hello.ts__template__ +2 -2
  99. package/src/lib/nx-plugin/src/generators/init/files/vite.config.ts__template__ +5 -1
  100. package/src/lib/nx-plugin/src/generators/init/generator.js +1 -2
  101. package/src/lib/nx-plugin/src/generators/init/generator.js.map +1 -1
  102. package/src/lib/nx-plugin/src/generators/init/lib/add-analog-dependencies.js +3 -4
  103. package/src/lib/nx-plugin/src/generators/init/lib/add-analog-dependencies.js.map +1 -1
  104. package/src/lib/nx-plugin/src/generators/init/lib/update-app-tsconfig.js +1 -2
  105. package/src/lib/nx-plugin/src/generators/init/lib/update-app-tsconfig.js.map +1 -1
  106. package/src/lib/nx-plugin/src/generators/init/lib/update-build-target.js +1 -2
  107. package/src/lib/nx-plugin/src/generators/init/lib/update-build-target.js.map +1 -1
  108. package/src/lib/nx-plugin/src/generators/init/lib/update-git-ignore.js +1 -2
  109. package/src/lib/nx-plugin/src/generators/init/lib/update-index-html.js +1 -2
  110. package/src/lib/nx-plugin/src/generators/init/lib/update-index-html.js.map +1 -1
  111. package/src/lib/nx-plugin/src/generators/init/lib/update-main.js +1 -2
  112. package/src/lib/nx-plugin/src/generators/init/lib/update-main.js.map +1 -1
  113. package/src/lib/nx-plugin/src/generators/init/lib/update-package-json.js +1 -2
  114. package/src/lib/nx-plugin/src/generators/init/lib/update-package-json.js.map +1 -1
  115. package/src/lib/nx-plugin/src/generators/init/lib/update-serve-target.js +1 -2
  116. package/src/lib/nx-plugin/src/generators/init/lib/update-serve-target.js.map +1 -1
  117. package/src/lib/nx-plugin/src/generators/init/lib/update-test-target.js +1 -2
  118. package/src/lib/nx-plugin/src/generators/init/lib/update-test-target.js.map +1 -1
  119. package/src/lib/nx-plugin/src/generators/init/lib/update-test-tsconfig.js +1 -2
  120. package/src/lib/nx-plugin/src/generators/init/lib/update-test-tsconfig.js.map +1 -1
  121. package/src/lib/nx-plugin/src/generators/page/compat.d.ts +2 -1
  122. package/src/lib/nx-plugin/src/generators/page/compat.js +5 -3
  123. package/src/lib/nx-plugin/src/generators/page/compat.js.map +1 -1
  124. package/src/lib/nx-plugin/src/generators/page/generator.js +1 -2
  125. package/src/lib/nx-plugin/src/generators/page/generator.js.map +1 -1
  126. package/src/lib/nx-plugin/src/generators/preset/__snapshots__/generator.spec.ts.snap +4 -0
  127. package/src/lib/nx-plugin/src/generators/preset/generator.js +6 -4
  128. package/src/lib/nx-plugin/src/generators/preset/generator.js.map +1 -1
  129. package/src/lib/nx-plugin/src/utils/version-utils.js +1 -2
  130. package/src/lib/nx-plugin/src/utils/version-utils.js.map +1 -1
  131. package/src/lib/nx-plugin/src/utils/versions/dependencies.d.ts +1 -1
  132. package/src/lib/nx-plugin/src/utils/versions/dependencies.js +2 -2
  133. package/src/lib/nx-plugin/src/utils/versions/dependencies.js.map +1 -1
  134. package/src/lib/nx-plugin/src/utils/versions/dev-dependencies.d.ts +2 -2
  135. package/src/lib/nx-plugin/src/utils/versions/dev-dependencies.js +9 -5
  136. package/src/lib/nx-plugin/src/utils/versions/dev-dependencies.js.map +1 -1
  137. package/src/lib/nx-plugin/src/utils/versions/versions.d.ts +9 -6
  138. package/src/lib/nx-plugin/src/utils/versions/versions.js +14 -9
  139. package/src/lib/nx-plugin/src/utils/versions/versions.js.map +1 -1
  140. package/src/lib/options.d.ts +16 -0
  141. package/src/lib/platform-plugin.js +3 -2
  142. package/src/lib/platform-plugin.js.map +1 -1
  143. package/src/lib/route-file-discovery.js +9 -6
  144. package/src/lib/route-file-discovery.js.map +1 -1
  145. package/src/lib/route-idiom-diagnostics.js +24 -10
  146. package/src/lib/route-idiom-diagnostics.js.map +1 -1
  147. package/src/lib/route-manifest.d.ts +1 -1
  148. package/src/lib/route-manifest.js +7 -8
  149. package/src/lib/route-manifest.js.map +1 -1
  150. package/src/lib/router-plugin.js +8 -5
  151. package/src/lib/router-plugin.js.map +1 -1
  152. package/src/lib/server-fn-client-transform.js +1 -2
  153. package/src/lib/server-fn-id.js +1 -2
  154. package/src/lib/server-plugin.d.ts +9 -0
  155. package/src/lib/server-plugin.js +169 -0
  156. package/src/lib/server-plugin.js.map +1 -0
  157. package/src/lib/ssr/defer-streaming-plugin.js +0 -1
  158. package/src/lib/ssr/inject-html-plugin.js +1 -2
  159. package/src/lib/ssr/ssr-build-plugin.js +1 -2
  160. package/src/lib/typed-routes-plugin.js +7 -7
  161. package/src/lib/typed-routes-plugin.js.map +1 -1
  162. package/src/lib/utils/debug-harness.js +1 -2
  163. package/src/lib/utils/debug-log-file.js +1 -2
  164. package/src/lib/utils/debug.js +1 -2
  165. package/src/lib/utils/rolldown.js +1 -2
  166. package/src/server-mode-plugin.js +2 -2
  167. package/src/server-mode-plugin.js.map +1 -1
  168. package/src/lib/nx-plugin/src/executors/vite-dev-server/compat.d.ts +0 -3
  169. package/src/lib/nx-plugin/src/executors/vite-dev-server/compat.js +0 -7
  170. package/src/lib/nx-plugin/src/executors/vite-dev-server/compat.js.map +0 -1
  171. package/src/lib/nx-plugin/src/executors/vite-dev-server/schema.d.ts +0 -1
  172. package/src/lib/nx-plugin/src/executors/vite-dev-server/schema.json +0 -101
  173. package/src/lib/nx-plugin/src/executors/vite-dev-server/vite-dev-server.impl.d.ts +0 -2
  174. package/src/lib/nx-plugin/src/executors/vite-dev-server/vite-dev-server.impl.js +0 -6
  175. package/src/lib/nx-plugin/src/executors/vite-dev-server/vite-dev-server.impl.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"analog-nitro-plugin.js","names":[],"sources":["../../../../src/lib/nitro/analog-nitro-plugin.ts"],"sourcesContent":["import { existsSync, readFileSync, readdirSync, unlinkSync } from 'node:fs';\nimport { mkdir, writeFile } from 'node:fs/promises';\nimport { createRequire } from 'node:module';\nimport { basename, dirname, join, relative, resolve } from 'node:path';\nimport type { Nitro, NitroEventHandler, PrerenderRoute } from 'nitro/types';\nimport type { DevEnvironment, Plugin, UserConfig } from 'vite';\n\nimport type { Options } from '../options.js';\nimport { SERVER_MODE_ID } from '../../server-mode-plugin.js';\nimport { injectServerFnIds } from '../inject-server-fn-ids.js';\nimport { serverFnFileId } from '../server-fn-id.js';\nimport type {\n PrerenderContentDir,\n PrerenderContentFile,\n PrerenderRouteConfig,\n PrerenderSitemapConfig,\n} from './types.js';\nimport { getPageHandlers } from './get-page-handlers.js';\nimport { pageEndpointsPlugin } from './page-endpoints-plugin.js';\nimport { getMatchingContentFilesWithFrontMatter } from './get-content-files.js';\nimport { buildSitemap } from './build-sitemap.js';\nimport { addPostRenderingHooks } from './post-rendering-hook.js';\nimport {\n expandRoutesWithLocales,\n createI18nPostRenderingHook,\n} from './i18n-prerender.js';\nimport { getServerFnHandlers } from './get-server-fn-handlers.js';\nimport {\n buildServerFnDispatchModule,\n getServerFnDispatchHandler,\n SERVER_FN_DISPATCH_VIRTUAL,\n} from './server-fn-endpoints.js';\n\nconst SSR_ENTRY_VIRTUAL_ID = '\\0virtual:@analogjs/nitro/ssr-entry';\n\ninterface NitroPluginContext {\n workspaceRoot: string;\n rootDir: string;\n sourceRoot: string;\n}\n\ntype RouteSitemap =\n | PrerenderSitemapConfig\n | (() => PrerenderSitemapConfig)\n | undefined;\n\n/**\n * Analog's NitroModule. Plug it into the Vite plugin chain alongside\n * `nitro()` from `nitro/vite`; nitro/vite picks up the `.nitro` property\n * and runs `setup(nitro)` once the Nitro instance is ready.\n */\nexport function analogNitroPlugin(options: Options = {}): Plugin {\n const workspaceRoot =\n options.workspaceRoot ?? process.env['NX_WORKSPACE_ROOT'] ?? process.cwd();\n const sourceRoot = 'src';\n const ssr = options.ssr ?? true;\n const apiPrefix = options.apiPrefix ?? 'api';\n\n let context: NitroPluginContext = {\n workspaceRoot,\n rootDir: '.',\n sourceRoot,\n };\n let ssrEntryMarkerPath = '';\n let userPublicDir: string | undefined;\n let isBuild = false;\n // Only active alongside `nitro()` from `nitro/vite`; apps on Nitro v2 use\n // `@analogjs/vite-plugin-nitro` instead.\n let enabled = false;\n let nitroH3Resolver: Plugin;\n const ssrDepsReady = new WeakMap<DevEnvironment, Promise<void>>();\n // The document the client environment emitted for this build, captured in\n // `writeBundle` below. A build must render around this rather than the\n // source document: the source points at the unbundled entry, which only\n // resolves while Vite is serving.\n let builtIndexHtml: string | undefined;\n\n function waitForSsrDeps(environment: DevEnvironment): Promise<void> {\n let ready = ssrDepsReady.get(environment);\n if (!ready) {\n // Vite's subsequent init() calls return before the first run finishes.\n ready = Promise.resolve(environment.depsOptimizer?.init());\n ssrDepsReady.set(environment, ready);\n }\n return ready;\n }\n\n function refreshContext(viteRoot: string | undefined) {\n const root = viteRoot ?? process.cwd();\n context = {\n workspaceRoot,\n rootDir: relative(workspaceRoot, root) || '.',\n sourceRoot,\n };\n ssrEntryMarkerPath = resolve(\n context.workspaceRoot,\n context.rootDir,\n '.analog/__ssr-entry.mjs',\n );\n }\n\n function indexHtmlPath(): string {\n return resolve(\n context.workspaceRoot,\n context.rootDir,\n options.index ?? 'index.html',\n );\n }\n\n function readIndexHtml(): string {\n // Dev reads the source document so Vite can transform it and serve the\n // entry it points at.\n if (!isBuild) {\n const source = indexHtmlPath();\n return existsSync(source)\n ? readFileSync(source, 'utf-8')\n : '<!doctype html><html><body><div id=\"app\"></div></body></html>';\n }\n if (builtIndexHtml === undefined) {\n throw new Error(\n `[analog] The client build produced no ${basename(indexHtmlPath())}, ` +\n `so there is no document to server-render around. Rendering around ` +\n `the source document instead would ship markup pointing at the ` +\n `unbundled entry, which does not exist in a build.`,\n );\n }\n return builtIndexHtml;\n }\n\n function resolveEntryServer(): string {\n return (\n options.entryServer ??\n resolve(\n context.workspaceRoot,\n context.rootDir,\n `${sourceRoot}/main.server.ts`,\n )\n );\n }\n\n const plugin: Plugin & {\n nitro: { setup: (nitro: Nitro) => void | Promise<void> };\n } = {\n name: '@analogjs/nitro',\n enforce: 'pre',\n\n config(userConfig, configEnv) {\n enabled = hasNitroVitePlugin(userConfig);\n if (!enabled) {\n return;\n }\n refreshContext(userConfig.root);\n isBuild = configEnv?.command === 'build';\n nitroH3Resolver = nitroH3Plugin(\n resolve(context.workspaceRoot, context.rootDir),\n );\n\n // Capture the user's Vite `publicDir` so the nitro `setup()` hook can\n // register it as a Nitro `publicAssets` source. nitro/vite forces the\n // client environment's `build.copyPublicDir` to `false`\n // (vite.mjs:248), expecting Nitro to manage public assets — but\n // doesn't auto-add the user's `publicDir`. Without this, anything\n // under `src/public/` (e.g. `/assets/shipping.json`) 404s during SSR\n // and ofetch consumers parse the catch-all SSR HTML as JSON.\n if (userConfig.publicDir !== false) {\n userPublicDir = resolve(\n context.workspaceRoot,\n context.rootDir,\n (userConfig.publicDir as string | undefined) ?? 'public',\n );\n }\n\n // Bridge the legacy `BUILD_PRESET` env var that `@analogjs/vite-plugin-nitro`\n // accepted into Nitro v3's `NITRO_PRESET`, and auto-pick the matching\n // preset when the build runs inside a host's CI (each host sets its\n // own well-known env var). Mirrors the legacy plugin's behavior so\n // users upgrading don't need to change their CI configuration.\n if (!process.env['NITRO_PRESET']) {\n if (process.env['BUILD_PRESET']) {\n process.env['NITRO_PRESET'] = process.env['BUILD_PRESET'];\n } else if (process.env['VERCEL']) {\n process.env['NITRO_PRESET'] = 'vercel';\n } else if (process.env['NETLIFY']) {\n process.env['NITRO_PRESET'] = 'netlify';\n } else if (process.env['CF_PAGES']) {\n process.env['NITRO_PRESET'] = 'cloudflare-pages';\n }\n }\n\n const overrides: UserConfig = {\n // Vite 8 defaults `server.fs.allow` to `[searchForWorkspaceRoot(root)]`,\n // which should already cover the workspace root. In practice, nitro/vite's\n // env-runner loads its own `dev-entry.mjs` from a pnpm content-hash path\n // (`node_modules/.pnpm/nitro@.../...`) through Vite's ModuleRunner and\n // hits an `ERR_LOAD_URL`/\"Does the file exist?\" error unless an explicit\n // allow entry covers the same root. Whitelist the workspace root here so\n // users don't have to write the workaround in every vite.config.ts.\n server: {\n fs: {\n allow: [context.workspaceRoot],\n },\n },\n // nitro/vite defaults the client environment's input to\n // `renderer.template` (vite.mjs:240). We install our own renderer\n // handler and delete that template — Nitro's template-serving\n // renderer just returns the raw document — which leaves the client\n // environment with no input, and an environment without one is\n // skipped (vite.mjs:78). The build then emits no browser bundles at\n // all and the client never boots. Point it at the source document\n // ourselves.\n //\n // Declared ahead of `ssr` so it builds first: the SSR bundle inlines\n // the document it renders around, so the client output has to exist\n // by the time that bundle is built.\n environments: {\n client: {\n build: { rollupOptions: { input: indexHtmlPath() } },\n },\n } as UserConfig['environments'],\n };\n\n if (ssr) {\n // Two-pronged registration: `experimental.vite.services.ssr.entry`\n // is the documented hook, but nitro/vite's setupNitroContext also\n // accepts an `environments.ssr.build.rollupOptions.input` entry\n // (see node_modules/nitro/dist/vite.mjs:710-734). When `analog()`\n // and `nitro()` are invoked separately, the `services` slot on\n // `nitro()`'s pluginConfig is empty, so the rollupOptions.input\n // path is how we get our wrapper entry recognized.\n overrides.experimental = {\n vite: {\n services: {\n ssr: { entry: ssrEntryMarkerPath },\n },\n },\n };\n (overrides.environments as Record<string, unknown>)['ssr'] = {\n build: {\n outDir: resolve(\n context.workspaceRoot,\n 'dist',\n context.rootDir,\n 'ssr',\n ),\n rollupOptions: {\n input: { index: ssrEntryMarkerPath },\n },\n },\n };\n }\n\n return overrides;\n },\n\n configureServer(server) {\n const environment = server.environments['ssr'];\n if (enabled && ssr && environment) {\n // Capture initialization before listen(); entry resolution propagates failures.\n void waitForSsrDeps(environment).catch(() => undefined);\n }\n },\n\n resolveId(id, importer, resolveOptions) {\n if (enabled && id === 'h3') {\n return (nitroH3Resolver.resolveId as ResolveIdHook).call(\n this,\n id,\n importer,\n resolveOptions,\n );\n }\n if (id === ssrEntryMarkerPath || id === SSR_ENTRY_VIRTUAL_ID) {\n if (ssr && !isBuild && this.environment.name === 'ssr') {\n return waitForSsrDeps(this.environment as DevEnvironment).then(\n () => SSR_ENTRY_VIRTUAL_ID,\n );\n }\n return SSR_ENTRY_VIRTUAL_ID;\n }\n return null;\n },\n\n load(id) {\n if (id !== SSR_ENTRY_VIRTUAL_ID) return null;\n return generateSsrEntryWrapper(resolveEntryServer(), readIndexHtml());\n },\n\n // Take the rendered-around document from the client environment's own\n // output rather than guessing where it landed. A guessed path can name a\n // leftover from an earlier build, which serves pages an application bundle\n // that no longer matches the markup around it.\n //\n // `writeBundle` rather than `generateBundle`: this plugin is\n // `enforce: 'pre'`, so its `generateBundle` runs before `vite:build-html`\n // emits the document. `writeBundle` runs once every `generateBundle` has.\n writeBundle(_outputOptions, bundle) {\n if (\n !enabled ||\n (this as { environment?: { name?: string } }).environment?.name !==\n 'client'\n ) {\n return;\n }\n const emitted = bundle[basename(indexHtmlPath())];\n if (emitted?.type === 'asset') {\n const { source } = emitted;\n builtIndexHtml =\n typeof source === 'string'\n ? source\n : new TextDecoder().decode(source);\n }\n },\n\n nitro: {\n async setup(nitro) {\n // refreshContext may not have run yet if nitro/vite resolved the\n // plugin before `config()`; fall back to nitro's own root.\n if (!context || context.rootDir === '.') {\n refreshContext(nitro.options.rootDir);\n }\n\n // Nitro's default dev runner answers from a worker, and the response\n // does not survive the trip back: the server adapter receives a value\n // it cannot read headers from, so every request Nitro handles hangs\n // without a status. Run the environment in process until that path is\n // fixed upstream. An explicit `devServer.runner` or `NITRO_DEV_RUNNER`\n // still wins.\n if (\n nitro.options.dev &&\n !process.env['NITRO_DEV_RUNNER'] &&\n !nitro.options.devServer?.runner\n ) {\n nitro.options.devServer = {\n ...nitro.options.devServer,\n runner: 'self',\n };\n }\n\n // Preserve the legacy `@analogjs/vite-plugin-nitro` final output\n // layout so downstream tooling (deploy scripts, docs, the\n // `dist/analog/server` start command) keeps working. nitro/vite's\n // default `<rootDir>/.output` would otherwise drop artifacts in an\n // unexpected location for users upgrading from v2.\n //\n // Build only. During dev, leaving the output paths at Nitro's\n // defaults keeps the dev server's `readAsset` happy — those\n // virtuals expect to read from the in-memory module graph, not\n // from a `dist/` directory that doesn't exist yet.\n //\n // `buildDir` (Nitro's intermediate scratch dir) stays at its default\n // inside the project root. Nitro's prerender phase re-bundles SSR\n // chunks out of `<buildDir>/vite/services/ssr/`, and Rolldown's\n // resolver walks up from those files looking for `node_modules/`.\n // Keeping `buildDir` adjacent to the project root means workspace\n // packages installed at `<rootDir>/node_modules/` (the usual install\n // shape for both standalone and Nx setups) remain reachable.\n if (!nitro.options.dev) {\n // Deployment presets (Vercel, Netlify, Cloudflare, Firebase, ...)\n // own their own output layout — Vercel writes the Build Output API\n // tree under `.vercel/output/`, Netlify expects functions under\n // `.netlify/functions-internal/` with static assets under `dist/`,\n // and so on. Clobbering `output.{dir,publicDir,serverDir}` for\n // those presets leaves functions and static files in different\n // trees and breaks the deploy. Only override when running the\n // default node-server preset (or no preset at all) so docs and\n // the legacy `dist/analog/server` start command keep working for\n // standalone Node deployments.\n const preset = (nitro.options.preset ?? '').toLowerCase();\n const isManagedPreset =\n preset !== '' &&\n preset !== 'node-server' &&\n preset !== 'node' &&\n preset !== 'nitro-dev';\n\n // Vercel's Build Output API expects `.vercel/output/` at the cwd\n // `vercel build` runs from (the repo root). Nitro's preset anchors\n // to `{{rootDir}}`, so in Nx monorepos artifacts land at\n // `apps/<name>/.vercel/output/` and the deploy can't find them.\n // Hoist to workspace root and apply Analog's runtime defaults.\n if (preset.includes('vercel')) {\n const vercel = (nitro.options as { vercel?: Record<string, any> })\n .vercel;\n (nitro.options as { vercel?: Record<string, any> }).vercel = {\n ...vercel,\n entryFormat: vercel?.entryFormat ?? 'node',\n functions: {\n runtime: vercel?.functions?.runtime ?? 'nodejs24.x',\n ...vercel?.functions,\n },\n };\n const vercelDir = resolve(context.workspaceRoot, '.vercel/output');\n nitro.options.output = {\n ...nitro.options.output,\n dir: vercelDir,\n serverDir: resolve(vercelDir, 'functions/__server.func'),\n publicDir: resolve(vercelDir, 'static'),\n };\n }\n\n // Netlify auto-discovers functions under\n // `<workspaceRoot>/.netlify/functions-internal/`. Nitro's netlify\n // preset anchors that to `{{rootDir}}`, which in Nx monorepos\n // becomes `apps/<name>/.netlify/...` and is invisible to the\n // Netlify deploy. Hoist the functions to the workspace root so\n // `nx build <app>` produces a deploy-ready tree at the repo root.\n // Keep `publicDir` under `dist/<rootDir>/analog/public/` (the\n // legacy Analog Netlify publish layout) — the user wires their\n // `netlify.toml` publish path to it.\n if (preset === 'netlify' || preset === 'netlify-edge') {\n const netlifyDir = resolve(\n context.workspaceRoot,\n '.netlify/functions-internal',\n );\n nitro.options.output = {\n ...nitro.options.output,\n dir: netlifyDir,\n serverDir: resolve(netlifyDir, 'server'),\n publicDir: resolve(\n context.workspaceRoot,\n 'dist',\n context.rootDir,\n 'analog/public',\n ),\n };\n }\n\n // Cloudflare Pages/Workers presets anchor their output at\n // `{{rootDir}}/dist` or `{{rootDir}}/.output`, which puts the\n // deploy tree at `apps/<name>/...` for Nx monorepos. Hoist to\n // `<workspaceRoot>/dist/<rootDir>/` so `wrangler pages deploy\n // dist/<rootDir>` from the workspace root finds `_worker.js/`\n // alongside the static assets.\n if (preset.includes('cloudflare')) {\n const cfDir = resolve(\n context.workspaceRoot,\n 'dist',\n context.rootDir,\n );\n nitro.options.output = {\n ...nitro.options.output,\n dir: cfDir,\n publicDir: cfDir,\n serverDir: resolve(cfDir, '_worker.js'),\n };\n }\n\n if (!isManagedPreset) {\n const distRoot = resolve(\n context.workspaceRoot,\n 'dist',\n context.rootDir,\n );\n nitro.options.output = {\n ...nitro.options.output,\n dir: resolve(distRoot, 'analog'),\n publicDir: resolve(distRoot, 'analog/public'),\n serverDir: resolve(distRoot, 'analog/server'),\n };\n }\n\n // Nitro v3's prerender writer compares `filePath.startsWith(publicDir)`\n // with `filePath` built via `node:path.join` (platform-native\n // separators on Windows) and `publicDir` set via `resolveNitroPath`\n // (always forward slashes via pathe). On Windows the two sides\n // disagree and every route is marked `(skipped)` — no HTML is\n // written. Hook `prerender:route` and write the file ourselves\n // when Nitro has skipped it but the route generated content.\n nitro.hooks.hook('prerender:route', async (route) => {\n if (!route.skip || route.error) return;\n const buffer = route.data;\n if (!buffer || !route.fileName) return;\n const filePath = resolve(\n nitro.options.output.publicDir,\n route.fileName.replace(/^[\\\\/]+/, ''),\n );\n try {\n await mkdir(dirname(filePath), { recursive: true });\n await writeFile(filePath, Buffer.from(buffer));\n route.skip = false;\n } catch {\n // leave Nitro's skip in place if the manual write also fails\n }\n });\n }\n\n // Register the user's Vite `publicDir` (e.g. `src/public/`) as a\n // Nitro public asset source. nitro/vite turns off Vite's own copy\n // of publicDir so Nitro can take over, but doesn't auto-bridge the\n // user's setting — without this entry, files like\n // `src/public/assets/shipping.json` aren't served and `HttpClient`\n // SSR fetches fall through to the catch-all SSR renderer, leaking\n // HTML where consumers expected JSON.\n if (userPublicDir && existsSync(userPublicDir)) {\n const already = nitro.options.publicAssets.some(\n (asset) => asset.dir === userPublicDir,\n );\n if (!already) {\n nitro.options.publicAssets.push({\n dir: userPublicDir,\n baseURL: '/',\n maxAge: 0,\n fallthrough: true,\n });\n }\n }\n\n // Bridge the outer Nitro's `output.publicDir` into the prerender's\n // own Nitro instance. Nitro spawns a nested `nitroRenderer` for the\n // prerender pass (`createNitro({ preset: 'nitro-prerender' })`) and\n // builds the public-assets manifest by glob-scanning that\n // instance's `output.publicDir`. The nested config resets\n // `output.publicDir` to undefined and resolves it against the\n // prerender preset defaults (`<rootDir>/.output/public`), so the\n // scan hits an empty directory and the manifest is empty — every\n // `HttpClient.get('/assets/...')` during SSR then falls through to\n // the catch-all SSR renderer and the consumer parses HTML as JSON.\n // Force the nested publicDir to match the outer publicDir (which\n // `copyPublicAssets` has already populated by this point).\n nitro.hooks.hook(\n 'prerender:config',\n (prerendererConfig: { output?: Record<string, unknown> }) => {\n prerendererConfig.output = {\n ...prerendererConfig.output,\n publicDir: nitro.options.output.publicDir,\n };\n },\n );\n\n const pageHandlers: NitroEventHandler[] = getPageHandlers({\n workspaceRoot: context.workspaceRoot,\n sourceRoot: context.sourceRoot,\n rootDir: context.rootDir,\n additionalPagesDirs: options.additionalPagesDirs,\n });\n nitro.options.handlers.push(...pageHandlers);\n registerServerFns(nitro, context);\n\n const serverDir = resolve(\n context.workspaceRoot,\n context.rootDir,\n `${context.sourceRoot}/server`,\n );\n const apiDirs = (options.additionalAPIDirs ?? []).map((dir) =>\n resolve(context.workspaceRoot, `.${dir}`),\n );\n for (const dir of [serverDir, ...apiDirs]) {\n if (existsSync(dir) && !nitro.options.scanDirs.includes(dir)) {\n nitro.options.scanDirs.push(dir);\n }\n }\n\n nitro.hooks.hook('rollup:before', (_n, rollupConfig: any) => {\n if (Array.isArray(rollupConfig.plugins)) {\n rollupConfig.plugins.push(\n nitroH3Resolver,\n pageEndpointsPlugin(),\n serverFnIdPlugin(resolve(context.workspaceRoot, context.rootDir)),\n );\n }\n applyAnalogNitroExternals(rollupConfig);\n sanitizeNitroBundlerConfig(rollupConfig);\n });\n\n if (ssr) {\n // Override Nitro's auto-detected template-serving renderer with one\n // that routes HTML requests to our SSR service. Nitro's\n // `resolveRendererOptions` finds `index.html` at the project root\n // and installs `internal/routes/renderer-template[.dev]`, which\n // just serves the raw template. nitro/vite's own SSR-routing\n // renderer only auto-installs when both `renderer.handler` and\n // `renderer.template` are empty (vite.mjs:574), which never holds\n // for a typical app — so we install our own renderer virtual\n // explicitly here.\n //\n // `#analog/ssr` is a Nitro virtual (not a Vite virtual) so it\n // resolves under both Vite-built bundles (main) and Rolldown-built\n // bundles (Nitro's prerender, which forces builder: 'rolldown' —\n // see nitro/dist/_chunks/nitro.mjs:769). That sidesteps nitro/vite's\n // prodSetup polyfill, which is Vite-only and leaves\n // `__nitro_vite_envs__` unset in the prerender bundle.\n nitro.options.virtual['#analog/ssr'] = () =>\n generateSsrServiceVirtual(nitro);\n // Lazy: `setup()` runs before any environment builds, so reading\n // the template here would read it before the client has emitted\n // one. Nitro resolves this virtual when it bundles — the prerender\n // pass and the final server build, both after the client build.\n nitro.options.virtual['#analog/ssr-renderer'] = () =>\n generateSsrRendererVirtual(readIndexHtml());\n nitro.options.renderer ??= {};\n nitro.options.renderer.handler = '#analog/ssr-renderer';\n delete nitro.options.renderer.template;\n\n // Nitro resolves `renderer.handler` to a file on disk while it\n // creates the prerender instance, which rejects the virtual id.\n // Hide the renderer from that resolution and restore the virtual\n // handler before the prerenderer syncs its routes.\n nitro.hooks.hook('prerender:config', (prerendererConfig) => {\n prerendererConfig.renderer = false;\n // The prerender instance is created from the raw user config, so\n // pass along the rules carrying the `ssr: false` headers.\n prerendererConfig.routeRules = nitro.options.routeRules;\n });\n nitro.hooks.hook('prerender:init', (prerenderer) => {\n prerenderer.options.renderer = { handler: '#analog/ssr-renderer' };\n });\n\n // The client build emits an `index.html` shell, and copying the\n // public assets puts it in the output directory. Nitro answers from\n // the public assets ahead of the renderer, so `/` would be served\n // that shell instead of being rendered. Nitro calls this hook once\n // the assets are in place, before prerendering and the server build.\n nitro.hooks.hook('prerender:routes', () => {\n const publicDir = resolve(nitro.options.output.publicDir);\n for (const extension of ['', '.br', '.gz']) {\n const indexFile = resolve(publicDir, `index.html${extension}`);\n if (existsSync(indexFile)) {\n unlinkSync(indexFile);\n }\n }\n });\n }\n // When ssr === false, Nitro's auto-detected template-serving\n // renderer is exactly what we want (serve the raw index.html for\n // every HTML request) — leave it in place.\n\n injectAnalogRouteRuleHeaders(nitro);\n\n await wirePrerender(nitro, options, context, apiPrefix);\n\n if (options.i18n) {\n addPostRenderingHooks(nitro, [\n createI18nPostRenderingHook({\n defaultLocale: options.i18n.defaultLocale,\n locales: options.i18n.locales,\n }),\n ]);\n }\n },\n },\n };\n\n return plugin;\n}\n\nfunction hasNitroVitePlugin(userConfig: UserConfig): boolean {\n const plugins = ((userConfig.plugins ?? []) as unknown[]).flat(Infinity) as (\n | { name?: string }\n | null\n | undefined\n )[];\n return plugins.some((plugin) => plugin?.name === 'nitro:main');\n}\n\ntype ResolveIdHook = Extract<NonNullable<Plugin['resolveId']>, Function>;\n\n/**\n * Resolves `h3` from Nitro's own package, so server code always uses the h3\n * version the server runs on.\n */\nfunction nitroH3Plugin(root: string): Plugin {\n let nitroPackageJson: string | undefined;\n return {\n name: 'analogjs-nitro-h3',\n resolveId(id, _importer, resolveOptions) {\n if (id !== 'h3') {\n return null;\n }\n nitroPackageJson ??= createRequire(join(root, 'noop.js')).resolve(\n 'nitro/package.json',\n );\n return this.resolve(id, nitroPackageJson, {\n ...resolveOptions,\n skipSelf: true,\n });\n },\n };\n}\n\n/**\n * Registers the `/_analog/fn/:id` dispatch handler when the app defines server\n * functions in `*.server.ts` files.\n */\nfunction registerServerFns(nitro: Nitro, context: NitroPluginContext): void {\n const modules = getServerFnHandlers(context);\n if (!modules.length) {\n return;\n }\n\n const root = resolve(context.workspaceRoot, context.rootDir);\n const appConfigModule = [\n `${root}/${context.sourceRoot}/app/app.config.server.ts`,\n `${root}/${context.sourceRoot}/app.config.server.ts`,\n ].find((candidate) => existsSync(candidate));\n\n nitro.options.handlers.push(getServerFnDispatchHandler());\n nitro.options.virtual[SERVER_FN_DISPATCH_VIRTUAL] =\n buildServerFnDispatchModule({ modules, appConfigModule });\n}\n\n/**\n * Stamps the derived id into each `serverFn` in the Nitro server bundle, so it\n * registers under the route the client calls.\n */\nfunction serverFnIdPlugin(projectRoot: string): {\n name: string;\n transform(code: string, id: string): { code: string; map: null } | undefined;\n} {\n return {\n name: 'analogjs-server-fn-id',\n transform(code, id) {\n if (!id.endsWith('.server.ts')) {\n return;\n }\n const injected = injectServerFnIds(code, serverFnFileId(id, projectRoot));\n return injected ? { code: injected.code, map: null } : undefined;\n },\n };\n}\n\n/**\n * Builds the h3 handler installed as Nitro's `renderer.handler`. Short-circuits\n * `ssr: false` routes to the raw client template; otherwise dispatches the\n * request to the SSR service env (`fetchViteEnv(\"ssr\", req)` works in dev via\n * the env-runner and in prod via the `__nitro_vite_envs__` global set up by\n * nitro/vite's `prodSetup`).\n */\nfunction generateSsrRendererVirtual(template: string): string {\n return `\nimport { defineHandler } from 'nitro/h3';\nimport { getRouteRules } from 'nitro/app';\nimport ssr from '#analog/ssr';\n\nconst TEMPLATE = ${JSON.stringify(template)};\n\nexport default defineHandler(async (event) => {\n event.res.headers.set('content-type', 'text/html; charset=utf-8');\n // 'x-analog-no-ssr' is stamped on the route rule headers by\n // injectAnalogRouteRuleHeaders for routeRules with \\`ssr: false\\`. Nitro\n // writes those headers after the handler runs, so read the matched rules.\n const { routeRules } = getRouteRules(event.req.method, event.url.pathname);\n if (routeRules?.headers?.['x-analog-no-ssr'] === 'true') {\n return TEMPLATE;\n }\n const service = ssr.default ?? ssr;\n // The SSR service renders without a Node response, so forward the\n // \\`streaming: false\\` rule on the request for the renderer to read.\n if (routeRules?.headers?.['x-analog-no-streaming'] === 'true') {\n const headers = new Headers(event.req.headers);\n headers.set('x-analog-no-streaming', 'true');\n return service.fetch(\n new Request(event.req.url, { method: event.req.method, headers }),\n );\n }\n return service.fetch(event.req);\n});\n`;\n}\n\n/**\n * Resolves \\`#analog/ssr\\` to the SSR fetch handler. The shape returned by\n * this function is bundler-agnostic: works in Vite-built main bundles and\n * Rolldown-built prerender bundles alike.\n *\n * - Dev: dispatch through nitro/vite's env runner (\\`fetchViteEnv\\`); the SSR\n * service module isn't on disk yet, so we delegate to the runner.\n * - Build / prerender: re-export the built SSR entry directly from the\n * filesystem. By the time Nitro's bundlers ask for \\`#analog/ssr\\`, Vite has\n * already produced \\`<buildDir>/vite/services/ssr/<entry>.mjs\\` (or \\`.js\\`\n * when the app's package.json sets \\`\"type\": \"module\"\\`).\n */\nfunction generateSsrServiceVirtual(nitro: Nitro): string {\n if (nitro.options.dev) {\n return `\nimport { fetchViteEnv } from 'nitro/vite/runtime';\nexport default {\n // The dev runner answers from a worker, and a rejection there comes back as\n // something Nitro cannot turn into a response, so the request hangs with no\n // status and no message. Answer with the failure instead.\n async fetch(req) {\n try {\n return await fetchViteEnv('ssr', req);\n } catch (err) {\n console.error('[analog ssr]', err);\n const message = err instanceof Error ? (err.stack ?? err.message) : String(err);\n return new Response(message, {\n status: 500,\n headers: { 'content-type': 'text/plain; charset=utf-8' },\n });\n }\n },\n};\n`;\n }\n\n const ssrDir = resolve(nitro.options.buildDir, 'vite/services/ssr');\n if (!existsSync(ssrDir)) {\n return `export default { async fetch() { throw new Error('Analog SSR service directory missing: ${ssrDir}'); } };`;\n }\n const entries = readdirSync(ssrDir).filter((f) => /\\.m?js$/.test(f));\n if (entries.length === 0) {\n return `export default { async fetch() { throw new Error('No Analog SSR entry file built in: ${ssrDir}'); } };`;\n }\n // Prefer 'main.server' if present; otherwise take the only entry.\n const entry =\n entries.find((f) => /^main\\.server\\.m?js$/.test(f)) ?? entries[0];\n const entryPath = resolve(ssrDir, entry);\n return `export { default } from ${JSON.stringify(entryPath)};`;\n}\n\n/**\n * Packages Analog forces external in the Nitro server bundle. Each entry is\n * here for a specific reason — see comments.\n */\nconst ANALOG_NITRO_EXTERNALS = [\n // rxjs ships per-entry CJS/ESM facades that confuse the Nitro/Rolldown\n // resolver during bundling.\n 'rxjs',\n // node-fetch-native's polyfill subpath rewrites global fetch and isn't\n // safe to inline into the Nitro bundle.\n 'node-fetch-native/dist/polyfill',\n // sharp ships platform-specific native binaries under @img/sharp-*. pnpm\n // creates symlinks for ALL optional platform deps but only installs the\n // matching one, leaving broken symlinks that crash Nitro's externals\n // plugin with ENOENT during realpath(). Externalizing sharp avoids\n // bundling it; the user's app resolves it from node_modules at runtime.\n 'sharp',\n];\n\nfunction applyAnalogNitroExternals(rollupConfig: { external?: unknown }): void {\n // Rolldown's `external` only accepts `Array<string | RegExp>`; promote\n // whatever shape Nitro gave us (regex, single string, undefined) to an\n // array and append Analog's entries as regex patterns that also match\n // sub-paths (e.g. `sharp` matches `sharp/lib/foo`).\n const prev = rollupConfig.external;\n const existing: Array<string | RegExp> =\n prev === undefined\n ? []\n : Array.isArray(prev)\n ? (prev as Array<string | RegExp>)\n : prev instanceof RegExp\n ? [prev]\n : typeof prev === 'string'\n ? [prev]\n : [];\n\n const escapeRegExp = (s: string) => s.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\n for (const entry of ANALOG_NITRO_EXTERNALS) {\n const pattern = new RegExp(`^${escapeRegExp(entry)}(?:/|$)`);\n if (!existing.some((p) => String(p) === String(pattern))) {\n existing.push(pattern);\n }\n }\n\n rollupConfig.external = existing;\n}\n\n/**\n * Workarounds for Nitro v3 + Rolldown bundler interaction quirks. Each\n * is narrowly scoped and can be removed once the upstream bug is fixed:\n *\n * 1. `output.codeSplitting` — Nitro 3.0.x sets this; Rolldown rejects it\n * as an unknown key.\n * 2. `output.manualChunks` — Nitro's default manual chunking crashes\n * Nitro's prerender rebundle.\n * 3. `output.chunkFileNames` — Nitro's chunk-name function produces\n * route-derived `[token]` patterns which Rollup/Rolldown interprets as\n * placeholders; we rewrite non-standard tokens to `_token_`.\n */\nfunction sanitizeNitroBundlerConfig(rollupConfig: { output?: unknown }): void {\n const output = rollupConfig.output;\n if (!output || Array.isArray(output) || typeof output !== 'object') return;\n const out = output as Record<string, unknown>;\n\n if ('codeSplitting' in out) delete out['codeSplitting'];\n if ('manualChunks' in out) delete out['manualChunks'];\n\n const VALID_ROLLUP_PLACEHOLDER = /^\\[(?:name|hash|format|ext)\\]$/;\n const chunkFileNames = out['chunkFileNames'];\n if (typeof chunkFileNames === 'function') {\n const originalFn = chunkFileNames as (...args: unknown[]) => unknown;\n out['chunkFileNames'] = (...args: unknown[]) => {\n const result = originalFn(...args);\n if (typeof result !== 'string') return result;\n return result.replace(/\\[[^\\]]+\\]/g, (match: string) =>\n VALID_ROLLUP_PLACEHOLDER.test(match)\n ? match\n : `_${match.slice(1, -1)}_`,\n );\n };\n }\n}\n\n/**\n * Walks Nitro's resolved routeRules and stamps `x-analog-no-ssr: true` onto\n * any rule with `ssr: false`, and `x-analog-no-streaming: true` onto any rule\n * with `streaming: false`. Kept as response-header hints for downstream\n * consumers (CDN, edge logic); the actual SSR short-circuit happens inside\n * the SSR renderer virtual above, and the router falls back to a buffered\n * render when it sees the streaming hint.\n */\nexport function injectAnalogRouteRuleHeaders(nitro: Nitro): void {\n const routeRules = nitro.options.routeRules as\n | Record<\n string,\n {\n ssr?: boolean;\n streaming?: boolean;\n headers?: Record<string, string>;\n }\n >\n | undefined;\n if (!routeRules) return;\n\n for (const rule of Object.values(routeRules)) {\n if (rule?.ssr === false) {\n rule.headers = { ...rule.headers, 'x-analog-no-ssr': 'true' };\n }\n if (rule?.streaming === false) {\n rule.headers = { ...rule.headers, 'x-analog-no-streaming': 'true' };\n }\n }\n}\n\n/**\n * Builds the SSR service entry source. The wrapper imports the user's\n * `main.server.ts` Angular renderer and adapts it to the `{ fetch(req) }`\n * shape that nitro/vite's service mechanism expects.\n */\nfunction generateSsrEntryWrapper(\n entryServer: string,\n template: string,\n): string {\n return `\n// Import \\`serverFetch\\` from the root \\`nitro\\` entry rather than \\`nitro/app\\`.\n// The /app subpath creates a fresh \\`useNitroApp()\\` instance scoped to the\n// importing bundle, which in our setup is the standalone SSR vite bundle —\n// it has no route handlers, so every fetch 404s. The root entry instead\n// reads \\`globalThis.__nitro__.{default,prerender}\\`, which the surrounding\n// Nitro server (prerender pass or production runtime) has already\n// populated with the real app + handlers.\n// Set \\`ngServerMode\\` before the Angular renderer and its app config evaluate,\n// otherwise \\`provideClientHydration()\\` registers browser-only features such\n// as event replay on the server.\nimport ${JSON.stringify(SERVER_MODE_ID)};\nimport { serverFetch as nitroServerFetch } from 'nitro';\nimport { createFetch } from 'ofetch';\nimport renderer from ${JSON.stringify(entryServer)};\n\nconst TEMPLATE = ${JSON.stringify(template)};\n\nconst normalizeRequestPath = (url) =>\n url.replace(/\\\\/index\\\\.html(?=$|[?#])/, '/');\n\n// In-process fetch wired into Nitro's request pipeline. Angular's HttpClient\n// (via withFetch()) and Analog's injectLoad() call this during SSR/prerender\n// so they hit the running app's page-endpoint and API routes without going\n// through the network — the prerender pipeline doesn't have a listening\n// socket. Without this, every SSR data fetch ECONNREFUSEs and Angular's\n// router fails to resolve any data-bound route, producing an empty\n// <router-outlet/> in the prerendered HTML.\nconst ssrFetch = (resource, init) => {\n let url = typeof resource === 'string'\n ? resource\n : resource instanceof URL\n ? resource.href\n : resource.url;\n // Relative URLs from injectAPIPrefix() etc. need a host for Nitro's\n // Request constructor to accept them.\n if (typeof url === 'string' && url.startsWith('/')) {\n url = 'http://localhost' + url;\n }\n return nitroServerFetch(url, init);\n};\n\n// Wrap in ofetch so consumers that expect \\`$fetch.raw()\\` (the router's\n// request-context interceptor short-circuits SSR HttpClient calls through\n// \\`globalThis.$fetch.raw\\`) can call it. Set on globalThis so router code\n// running inside the Angular renderer can find it.\nconst ssrOFetch = createFetch({ fetch: ssrFetch });\nif (typeof globalThis.$fetch === 'undefined') {\n globalThis.$fetch = ssrOFetch;\n}\n\nexport default {\n async fetch(req) {\n const url = new URL(req.url);\n const requestPath = normalizeRequestPath(url.pathname);\n // Preserve the query string so Angular's router + injectQuery() and\n // server data loaders that read from req.url see the full path+query.\n const requestUrl = requestPath + url.search;\n\n if (req.headers.get('x-analog-no-ssr') === 'true') {\n return new Response(TEMPLATE, {\n status: 200,\n headers: { 'content-type': 'text/html; charset=utf-8' },\n });\n }\n\n const reqShim = {\n headers: Object.fromEntries(req.headers.entries()),\n url: requestUrl,\n originalUrl: requestUrl,\n connection: {},\n };\n\n try {\n const html = await renderer(requestUrl, TEMPLATE, {\n req: reqShim,\n // Pass the ofetch-wrapped fetch — INTERNAL_FETCH is consumed by the\n // router's request-context interceptor via \\`serverFetch.raw(...)\\`,\n // which is ofetch's response-shape API. Plain fetch lacks \\`.raw\\`\n // and throws TypeError during prerender/SSR.\n fetch: ssrOFetch,\n });\n return new Response(html, {\n status: 200,\n headers: { 'content-type': 'text/html; charset=utf-8' },\n });\n } catch (err) {\n console.error('[analog ssr]', err);\n return new Response(TEMPLATE, {\n status: 500,\n headers: { 'content-type': 'text/html; charset=utf-8' },\n });\n }\n },\n};\n`;\n}\n\nasync function wirePrerender(\n nitro: Nitro,\n options: Options,\n context: NitroPluginContext,\n apiPrefix: string,\n): Promise<void> {\n const prerender = options.prerender;\n if (!prerender && !options.i18n) return;\n\n const {\n routes: collected,\n sitemaps: routeSitemaps,\n routeSourceFiles,\n } = await collectRoutes(prerender?.routes, context, apiPrefix);\n\n const expanded = options.i18n\n ? expandRoutesWithLocales(collected, {\n defaultLocale: options.i18n.defaultLocale,\n locales: options.i18n.locales,\n })\n : collected;\n\n const nitroPrerender = (nitro.options.prerender ??= {}) as Record<\n string,\n any\n >;\n nitroPrerender.routes ??= [];\n nitroPrerender.routes.push(...expanded);\n if (prerender?.discover ?? false) {\n nitroPrerender.crawlLinks = true;\n }\n\n if (prerender?.postRenderingHooks?.length) {\n addPostRenderingHooks(nitro, prerender.postRenderingHooks);\n }\n\n if (Object.keys(routeSourceFiles).length > 0) {\n // Mirror the legacy `@analogjs/vite-plugin-nitro` behavior: after\n // prerender completes, write the route's source content alongside the\n // prerendered HTML at `<publicDir>/<route>.md`. Drives the\n // `outputSourceFile` option on both `PrerenderRouteConfig` (string path\n // to source markdown) and `PrerenderContentDir` (per-file callback).\n nitro.hooks.hook('prerender:done', async () => {\n const publicDir = resolve(nitro.options.output.publicDir);\n const { mkdirSync, writeFileSync } = await import('node:fs');\n const { dirname, join } = await import('node:path');\n for (const [route, content] of Object.entries(routeSourceFiles)) {\n const outputPath = join(publicDir, `${route}.md`);\n mkdirSync(dirname(outputPath), { recursive: true });\n writeFileSync(outputPath, content, 'utf8');\n }\n });\n }\n\n const sitemapConfig = prerender?.sitemap;\n if (sitemapConfig) {\n nitro.hooks.hook('prerender:done', async (result) => {\n const prerenderedRoutes = (result?.prerenderedRoutes ?? []).map(\n (r: PrerenderRoute) => r.route,\n );\n const publicDir = resolve(nitro.options.output.publicDir);\n await buildSitemap(\n {} as any,\n sitemapConfig,\n prerenderedRoutes,\n publicDir,\n routeSitemaps,\n { apiPrefix },\n );\n });\n }\n}\n\nasync function collectRoutes(\n routesInput: Options['prerender'] extends infer P\n ? P extends { routes?: infer R }\n ? R\n : never\n : never,\n context: NitroPluginContext,\n apiPrefix: string,\n): Promise<{\n routes: string[];\n sitemaps: Record<string, RouteSitemap>;\n routeSourceFiles: Record<string, string>;\n}> {\n const out: string[] = [];\n const sitemaps: Record<string, RouteSitemap> = {};\n const routeSourceFiles: Record<string, string> = {};\n\n if (!routesInput) return { routes: out, sitemaps, routeSourceFiles };\n\n const inputs = Array.isArray(routesInput)\n ? routesInput\n : typeof routesInput === 'function'\n ? await routesInput()\n : [];\n\n for (const entry of inputs) {\n if (!entry) continue;\n\n if (typeof entry === 'string') {\n out.push(entry);\n continue;\n }\n\n if ('contentDir' in entry) {\n const dir = entry as PrerenderContentDir;\n const files = getMatchingContentFilesWithFrontMatter(\n context.workspaceRoot,\n context.rootDir,\n dir.contentDir,\n !!dir.recursive,\n );\n for (const file of files) {\n const route = dir.transform(file);\n if (route === false) continue;\n out.push(route);\n if (dir.sitemap) {\n sitemaps[route] =\n typeof dir.sitemap === 'function'\n ? (\n dir.sitemap as (\n f: PrerenderContentFile,\n ) => PrerenderSitemapConfig\n )(file)\n : dir.sitemap;\n }\n if (dir.outputSourceFile) {\n const sourceContent = dir.outputSourceFile(file);\n if (typeof sourceContent === 'string') {\n routeSourceFiles[route] = sourceContent;\n }\n }\n }\n continue;\n }\n\n if ('route' in entry) {\n const cfg = entry as PrerenderRouteConfig;\n out.push(cfg.route);\n if (cfg.sitemap) {\n sitemaps[cfg.route] = cfg.sitemap;\n }\n if (cfg.outputSourceFile) {\n const sourcePath = resolve(\n context.workspaceRoot,\n context.rootDir,\n cfg.outputSourceFile,\n );\n if (existsSync(sourcePath)) {\n routeSourceFiles[cfg.route] = readFileSync(sourcePath, 'utf8');\n }\n }\n if (cfg.staticData) {\n // Mirror the legacy plugin: when staticData is requested, also\n // prerender the page's data-fetching endpoint so the JSON payload\n // is available statically.\n const prefix = apiPrefix.startsWith('/') ? apiPrefix : `/${apiPrefix}`;\n const route = cfg.route.startsWith('/') ? cfg.route : `/${cfg.route}`;\n out.push(`${prefix}/_analog/pages${route}`);\n }\n }\n }\n\n return { routes: out, sitemaps, routeSourceFiles };\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAiCA,IAAM,uBAAuB;;;;;;AAkB7B,SAAgB,kBAAkB,UAAmB,CAAC,GAAW;CAC/D,MAAM,gBACJ,QAAQ,iBAAiB,QAAQ,IAAI,wBAAwB,QAAQ,IAAI;CAC3E,MAAM,aAAa;CACnB,MAAM,MAAM,QAAQ,OAAO;CAC3B,MAAM,YAAY,QAAQ,aAAa;CAEvC,IAAI,UAA8B;EAChC;EACA,SAAS;EACT;CACF;CACA,IAAI,qBAAqB;CACzB,IAAI;CACJ,IAAI,UAAU;CAGd,IAAI,UAAU;CACd,IAAI;CACJ,MAAM,+BAAe,IAAI,QAAuC;CAKhE,IAAI;CAEJ,SAAS,eAAe,aAA4C;EAClE,IAAI,QAAQ,aAAa,IAAI,WAAW;EACxC,IAAI,CAAC,OAAO;GAEV,QAAQ,QAAQ,QAAQ,YAAY,eAAe,KAAK,CAAC;GACzD,aAAa,IAAI,aAAa,KAAK;EACr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;CAEA,OAAO;EAAE,QAAQ;EAAK;EAAU;CAAiB;AACnD"}
1
+ {"version":3,"file":"analog-nitro-plugin.js","names":[],"sources":["../../../../src/lib/nitro/analog-nitro-plugin.ts"],"sourcesContent":["import { existsSync, readFileSync, readdirSync, unlinkSync } from 'node:fs';\nimport { mkdir, writeFile } from 'node:fs/promises';\nimport { createRequire } from 'node:module';\nimport { basename, dirname, join, relative, resolve } from 'node:path';\nimport type { Nitro, NitroEventHandler, PrerenderRoute } from 'nitro/types';\nimport type { DevEnvironment, Plugin, UserConfig } from 'vite';\n\nimport type { Options } from '../options.js';\nimport { SERVER_MODE_ID } from '../../server-mode-plugin.js';\nimport { injectServerFnIds } from '../inject-server-fn-ids.js';\nimport { serverFnFileId } from '../server-fn-id.js';\nimport type {\n PrerenderContentDir,\n PrerenderContentFile,\n PrerenderRouteConfig,\n PrerenderSitemapConfig,\n} from './types.js';\nimport { getPageHandlers } from './get-page-handlers.js';\nimport { pageEndpointsPlugin } from './page-endpoints-plugin.js';\nimport { getMatchingContentFilesWithFrontMatter } from './get-content-files.js';\nimport { buildSitemap } from './build-sitemap.js';\nimport { addPostRenderingHooks } from './post-rendering-hook.js';\nimport {\n expandRoutesWithLocales,\n createI18nPostRenderingHook,\n} from './i18n-prerender.js';\nimport { getServerFnHandlers } from './get-server-fn-handlers.js';\nimport {\n buildServerFnDispatchModule,\n getServerFnDispatchHandler,\n SERVER_FN_DISPATCH_VIRTUAL,\n} from './server-fn-endpoints.js';\n\nconst SSR_ENTRY_VIRTUAL_ID = '\\0virtual:@analogjs/nitro/ssr-entry';\n\ninterface NitroPluginContext {\n workspaceRoot: string;\n rootDir: string;\n sourceRoot: string;\n}\n\ntype RouteSitemap =\n | PrerenderSitemapConfig\n | (() => PrerenderSitemapConfig)\n | undefined;\n\n/**\n * Analog's NitroModule. Plug it into the Vite plugin chain alongside\n * `nitro()` from `nitro/vite`; nitro/vite picks up the `.nitro` property\n * and runs `setup(nitro)` once the Nitro instance is ready.\n */\nexport function analogNitroPlugin(options: Options = {}): Plugin {\n const workspaceRoot =\n options.workspaceRoot ?? process.env['NX_WORKSPACE_ROOT'] ?? process.cwd();\n const sourceRoot = 'src';\n const ssr = options.ssr ?? true;\n const apiPrefix = options.apiPrefix ?? 'api';\n\n let context: NitroPluginContext = {\n workspaceRoot,\n rootDir: '.',\n sourceRoot,\n };\n let ssrEntryMarkerPath = '';\n let userPublicDir: string | undefined;\n let isBuild = false;\n // Only active alongside `nitro()` from `nitro/vite`; apps on Nitro v2 use\n // `@analogjs/vite-plugin-nitro` instead.\n let enabled = false;\n let nitroH3Resolver: Plugin;\n const ssrDepsReady = new WeakMap<DevEnvironment, Promise<void>>();\n // The document the client environment emitted for this build, captured in\n // `writeBundle` below. A build must render around this rather than the\n // source document: the source points at the unbundled entry, which only\n // resolves while Vite is serving.\n let builtIndexHtml: string | undefined;\n\n function waitForSsrDeps(environment: DevEnvironment): Promise<void> {\n let ready = ssrDepsReady.get(environment);\n if (!ready) {\n // Vite's subsequent init() calls return before the first run finishes.\n ready = Promise.resolve(environment.depsOptimizer?.init());\n ssrDepsReady.set(environment, ready);\n }\n return ready;\n }\n\n function refreshContext(viteRoot: string | undefined) {\n const root = viteRoot ?? process.cwd();\n context = {\n workspaceRoot,\n rootDir: relative(workspaceRoot, root) || '.',\n sourceRoot,\n };\n ssrEntryMarkerPath = resolve(\n context.workspaceRoot,\n context.rootDir,\n '.analog/__ssr-entry.mjs',\n );\n }\n\n function indexHtmlPath(): string {\n return resolve(\n context.workspaceRoot,\n context.rootDir,\n options.index ?? 'index.html',\n );\n }\n\n function readIndexHtml(): string {\n // Dev reads the source document so Vite can transform it and serve the\n // entry it points at.\n if (!isBuild) {\n const source = indexHtmlPath();\n return existsSync(source)\n ? readFileSync(source, 'utf-8')\n : '<!doctype html><html><body><div id=\"app\"></div></body></html>';\n }\n if (builtIndexHtml === undefined) {\n throw new Error(\n `[analog] The client build produced no ${basename(indexHtmlPath())}, ` +\n `so there is no document to server-render around. Rendering around ` +\n `the source document instead would ship markup pointing at the ` +\n `unbundled entry, which does not exist in a build.`,\n );\n }\n return builtIndexHtml;\n }\n\n function resolveEntryServer(): string {\n return (\n options.entryServer ??\n resolve(\n context.workspaceRoot,\n context.rootDir,\n `${sourceRoot}/main.server.ts`,\n )\n );\n }\n\n const plugin: Plugin & {\n nitro: { setup: (nitro: Nitro) => void | Promise<void> };\n } = {\n name: '@analogjs/nitro',\n enforce: 'pre',\n\n config(userConfig, configEnv) {\n enabled = hasNitroVitePlugin(userConfig);\n if (!enabled) {\n return;\n }\n refreshContext(userConfig.root);\n isBuild = configEnv?.command === 'build';\n nitroH3Resolver = nitroH3Plugin(\n resolve(context.workspaceRoot, context.rootDir),\n );\n\n // Capture the user's Vite `publicDir` so the nitro `setup()` hook can\n // register it as a Nitro `publicAssets` source. nitro/vite forces the\n // client environment's `build.copyPublicDir` to `false`\n // (vite.mjs:248), expecting Nitro to manage public assets — but\n // doesn't auto-add the user's `publicDir`. Without this, anything\n // under `src/public/` (e.g. `/assets/shipping.json`) 404s during SSR\n // and ofetch consumers parse the catch-all SSR HTML as JSON.\n if (userConfig.publicDir !== false) {\n userPublicDir = resolve(\n context.workspaceRoot,\n context.rootDir,\n (userConfig.publicDir as string | undefined) ?? 'public',\n );\n }\n\n // Bridge the legacy `BUILD_PRESET` env var that `@analogjs/vite-plugin-nitro`\n // accepted into Nitro v3's `NITRO_PRESET`, and auto-pick the matching\n // preset when the build runs inside a host's CI (each host sets its\n // own well-known env var). Mirrors the legacy plugin's behavior so\n // users upgrading don't need to change their CI configuration.\n if (!process.env['NITRO_PRESET']) {\n if (process.env['BUILD_PRESET']) {\n process.env['NITRO_PRESET'] = process.env['BUILD_PRESET'];\n } else if (process.env['VERCEL']) {\n process.env['NITRO_PRESET'] = 'vercel';\n } else if (process.env['NETLIFY']) {\n process.env['NITRO_PRESET'] = 'netlify';\n } else if (process.env['CF_PAGES']) {\n process.env['NITRO_PRESET'] = 'cloudflare-pages';\n }\n }\n\n const overrides: UserConfig = {\n // Vite 8 defaults `server.fs.allow` to `[searchForWorkspaceRoot(root)]`,\n // which should already cover the workspace root. In practice, nitro/vite's\n // env-runner loads its own `dev-entry.mjs` from a pnpm content-hash path\n // (`node_modules/.pnpm/nitro@.../...`) through Vite's ModuleRunner and\n // hits an `ERR_LOAD_URL`/\"Does the file exist?\" error unless an explicit\n // allow entry covers the same root. Whitelist the workspace root here so\n // users don't have to write the workaround in every vite.config.ts.\n server: {\n fs: {\n allow: [context.workspaceRoot],\n },\n },\n // nitro/vite defaults the client environment's input to\n // `renderer.template` (vite.mjs:240). We install our own renderer\n // handler and delete that template — Nitro's template-serving\n // renderer just returns the raw document — which leaves the client\n // environment with no input, and an environment without one is\n // skipped (vite.mjs:78). The build then emits no browser bundles at\n // all and the client never boots. Point it at the source document\n // ourselves.\n //\n // Declared ahead of `ssr` so it builds first: the SSR bundle inlines\n // the document it renders around, so the client output has to exist\n // by the time that bundle is built.\n environments: {\n client: {\n build: { rollupOptions: { input: indexHtmlPath() } },\n },\n } as UserConfig['environments'],\n };\n\n if (ssr) {\n // nitro/vite registers the SSR service from\n // `environments.ssr.build.rollupOptions.input`.\n (overrides.environments as Record<string, unknown>)['ssr'] = {\n build: {\n outDir: resolve(\n context.workspaceRoot,\n 'dist',\n context.rootDir,\n 'ssr',\n ),\n rollupOptions: {\n input: { index: ssrEntryMarkerPath },\n },\n },\n };\n }\n\n return overrides;\n },\n\n configureServer(server) {\n const environment = server.environments['ssr'];\n if (enabled && ssr && environment) {\n // Capture initialization before listen(); entry resolution propagates failures.\n void waitForSsrDeps(environment).catch(() => undefined);\n }\n },\n\n resolveId(id, importer, resolveOptions) {\n if (enabled && id === 'h3') {\n return (nitroH3Resolver.resolveId as ResolveIdHook).call(\n this,\n id,\n importer,\n resolveOptions,\n );\n }\n if (id === ssrEntryMarkerPath || id === SSR_ENTRY_VIRTUAL_ID) {\n if (ssr && !isBuild && this.environment.name === 'ssr') {\n return waitForSsrDeps(this.environment as DevEnvironment).then(\n () => SSR_ENTRY_VIRTUAL_ID,\n );\n }\n return SSR_ENTRY_VIRTUAL_ID;\n }\n return null;\n },\n\n load(id) {\n if (id !== SSR_ENTRY_VIRTUAL_ID) return null;\n return generateSsrEntryWrapper(resolveEntryServer(), readIndexHtml());\n },\n\n // Take the rendered-around document from the client environment's own\n // output rather than guessing where it landed. A guessed path can name a\n // leftover from an earlier build, which serves pages an application bundle\n // that no longer matches the markup around it.\n //\n // `writeBundle` rather than `generateBundle`: this plugin is\n // `enforce: 'pre'`, so its `generateBundle` runs before `vite:build-html`\n // emits the document. `writeBundle` runs once every `generateBundle` has.\n writeBundle(_outputOptions, bundle) {\n if (\n !enabled ||\n (this as { environment?: { name?: string } }).environment?.name !==\n 'client'\n ) {\n return;\n }\n const emitted = bundle[basename(indexHtmlPath())];\n if (emitted?.type === 'asset') {\n const { source } = emitted;\n builtIndexHtml =\n typeof source === 'string'\n ? source\n : new TextDecoder().decode(source);\n }\n },\n\n nitro: {\n async setup(nitro) {\n // refreshContext may not have run yet if nitro/vite resolved the\n // plugin before `config()`; fall back to nitro's own root.\n if (!context || context.rootDir === '.') {\n refreshContext(nitro.options.rootDir);\n }\n\n // Nitro's default dev runner answers from a worker, and the response\n // does not survive the trip back: the server adapter receives a value\n // it cannot read headers from, so every request Nitro handles hangs\n // without a status. Run the environment in process until that path is\n // fixed upstream. An explicit `devServer.runner` or `NITRO_DEV_RUNNER`\n // still wins.\n if (\n nitro.options.dev &&\n !process.env['NITRO_DEV_RUNNER'] &&\n !nitro.options.devServer?.runner\n ) {\n nitro.options.devServer = {\n ...nitro.options.devServer,\n runner: 'self',\n };\n }\n\n // Preserve the legacy `@analogjs/vite-plugin-nitro` final output\n // layout so downstream tooling (deploy scripts, docs, the\n // `dist/analog/server` start command) keeps working. nitro/vite's\n // default `<rootDir>/.output` would otherwise drop artifacts in an\n // unexpected location for users upgrading from v2.\n //\n // Build only. During dev, leaving the output paths at Nitro's\n // defaults keeps the dev server's `readAsset` happy — those\n // virtuals expect to read from the in-memory module graph, not\n // from a `dist/` directory that doesn't exist yet.\n //\n // `buildDir` (Nitro's intermediate scratch dir) stays at its default\n // inside the project root. Nitro's prerender phase re-bundles SSR\n // chunks out of `<buildDir>/vite/services/ssr/`, and Rolldown's\n // resolver walks up from those files looking for `node_modules/`.\n // Keeping `buildDir` adjacent to the project root means workspace\n // packages installed at `<rootDir>/node_modules/` (the usual install\n // shape for both standalone and Nx setups) remain reachable.\n if (!nitro.options.dev) {\n // Deployment presets (Vercel, Netlify, Cloudflare, Firebase, ...)\n // own their own output layout — Vercel writes the Build Output API\n // tree under `.vercel/output/`, Netlify expects functions under\n // `.netlify/functions-internal/` with static assets under `dist/`,\n // and so on. Clobbering `output.{dir,publicDir,serverDir}` for\n // those presets leaves functions and static files in different\n // trees and breaks the deploy. Only override when running the\n // default node-server preset (or no preset at all) so docs and\n // the legacy `dist/analog/server` start command keep working for\n // standalone Node deployments.\n const preset = (nitro.options.preset ?? '').toLowerCase();\n const isManagedPreset =\n preset !== '' &&\n preset !== 'node-server' &&\n preset !== 'node' &&\n preset !== 'nitro-dev';\n\n // Vercel's Build Output API expects `.vercel/output/` at the cwd\n // `vercel build` runs from (the repo root). Nitro's preset anchors\n // to `{{rootDir}}`, so in Nx monorepos artifacts land at\n // `apps/<name>/.vercel/output/` and the deploy can't find them.\n // Hoist to workspace root and apply Analog's runtime defaults.\n if (preset.includes('vercel')) {\n const vercel = (nitro.options as { vercel?: Record<string, any> })\n .vercel;\n (nitro.options as { vercel?: Record<string, any> }).vercel = {\n ...vercel,\n entryFormat: vercel?.entryFormat ?? 'node',\n functions: {\n runtime: vercel?.functions?.runtime ?? 'nodejs24.x',\n ...vercel?.functions,\n },\n };\n const vercelDir = resolve(context.workspaceRoot, '.vercel/output');\n nitro.options.output = {\n ...nitro.options.output,\n dir: vercelDir,\n serverDir: resolve(vercelDir, 'functions/__server.func'),\n publicDir: resolve(vercelDir, 'static'),\n };\n }\n\n // Netlify auto-discovers functions under\n // `<workspaceRoot>/.netlify/functions-internal/`. Nitro's netlify\n // preset anchors that to `{{rootDir}}`, which in Nx monorepos\n // becomes `apps/<name>/.netlify/...` and is invisible to the\n // Netlify deploy. Hoist the functions to the workspace root so\n // `nx build <app>` produces a deploy-ready tree at the repo root.\n // Keep `publicDir` under `dist/<rootDir>/analog/public/` (the\n // legacy Analog Netlify publish layout) — the user wires their\n // `netlify.toml` publish path to it.\n if (preset === 'netlify' || preset === 'netlify-edge') {\n const netlifyDir = resolve(\n context.workspaceRoot,\n '.netlify/functions-internal',\n );\n nitro.options.output = {\n ...nitro.options.output,\n dir: netlifyDir,\n serverDir: resolve(netlifyDir, 'server'),\n publicDir: resolve(\n context.workspaceRoot,\n 'dist',\n context.rootDir,\n 'analog/public',\n ),\n };\n }\n\n // Cloudflare Pages/Workers presets anchor their output at\n // `{{rootDir}}/dist` or `{{rootDir}}/.output`, which puts the\n // deploy tree at `apps/<name>/...` for Nx monorepos. Hoist to\n // `<workspaceRoot>/dist/<rootDir>/` so `wrangler pages deploy\n // dist/<rootDir>` from the workspace root finds `_worker.js/`\n // alongside the static assets.\n if (preset.includes('cloudflare')) {\n const cfDir = resolve(\n context.workspaceRoot,\n 'dist',\n context.rootDir,\n );\n nitro.options.output = {\n ...nitro.options.output,\n dir: cfDir,\n publicDir: cfDir,\n serverDir: resolve(cfDir, '_worker.js'),\n };\n }\n\n if (!isManagedPreset) {\n const distRoot = resolve(\n context.workspaceRoot,\n 'dist',\n context.rootDir,\n );\n nitro.options.output = {\n ...nitro.options.output,\n dir: resolve(distRoot, 'analog'),\n publicDir: resolve(distRoot, 'analog/public'),\n serverDir: resolve(distRoot, 'analog/server'),\n };\n }\n\n // Nitro v3's prerender writer compares `filePath.startsWith(publicDir)`\n // with `filePath` built via `node:path.join` (platform-native\n // separators on Windows) and `publicDir` set via `resolveNitroPath`\n // (always forward slashes via pathe). On Windows the two sides\n // disagree and every route is marked `(skipped)` — no HTML is\n // written. Hook `prerender:route` and write the file ourselves\n // when Nitro has skipped it but the route generated content.\n nitro.hooks.hook('prerender:route', async (route) => {\n if (!route.skip || route.error) return;\n const buffer = route.data;\n if (!buffer || !route.fileName) return;\n const filePath = resolve(\n nitro.options.output.publicDir,\n route.fileName.replace(/^[\\\\/]+/, ''),\n );\n try {\n await mkdir(dirname(filePath), { recursive: true });\n await writeFile(filePath, Buffer.from(buffer));\n route.skip = false;\n } catch {\n // leave Nitro's skip in place if the manual write also fails\n }\n });\n }\n\n // Register the user's Vite `publicDir` (e.g. `src/public/`) as a\n // Nitro public asset source. nitro/vite turns off Vite's own copy\n // of publicDir so Nitro can take over, but doesn't auto-bridge the\n // user's setting — without this entry, files like\n // `src/public/assets/shipping.json` aren't served and `HttpClient`\n // SSR fetches fall through to the catch-all SSR renderer, leaking\n // HTML where consumers expected JSON.\n if (userPublicDir && existsSync(userPublicDir)) {\n const already = nitro.options.publicAssets.some(\n (asset) => asset.dir === userPublicDir,\n );\n if (!already) {\n nitro.options.publicAssets.push({\n dir: userPublicDir,\n baseURL: '/',\n maxAge: 0,\n fallthrough: true,\n });\n }\n }\n\n // Bridge the outer Nitro's `output.publicDir` into the prerender's\n // own Nitro instance. Nitro spawns a nested `nitroRenderer` for the\n // prerender pass (`createNitro({ preset: 'nitro-prerender' })`) and\n // builds the public-assets manifest by glob-scanning that\n // instance's `output.publicDir`. The nested config resets\n // `output.publicDir` to undefined and resolves it against the\n // prerender preset defaults (`<rootDir>/.output/public`), so the\n // scan hits an empty directory and the manifest is empty — every\n // `HttpClient.get('/assets/...')` during SSR then falls through to\n // the catch-all SSR renderer and the consumer parses HTML as JSON.\n // Force the nested publicDir to match the outer publicDir (which\n // `copyPublicAssets` has already populated by this point).\n nitro.hooks.hook(\n 'prerender:config',\n (prerendererConfig: { output?: Record<string, unknown> }) => {\n prerendererConfig.output = {\n ...prerendererConfig.output,\n publicDir: nitro.options.output.publicDir,\n };\n },\n );\n\n const pageHandlers: NitroEventHandler[] = getPageHandlers({\n workspaceRoot: context.workspaceRoot,\n sourceRoot: context.sourceRoot,\n rootDir: context.rootDir,\n additionalPagesDirs: options.additionalPagesDirs,\n });\n nitro.options.handlers.push(...pageHandlers);\n registerServerFns(nitro, context);\n\n const serverDir = resolve(\n context.workspaceRoot,\n context.rootDir,\n `${context.sourceRoot}/server`,\n );\n const apiDirs = (options.additionalAPIDirs ?? []).map((dir) =>\n resolve(context.workspaceRoot, `.${dir}`),\n );\n for (const dir of [serverDir, ...apiDirs]) {\n if (existsSync(dir) && !nitro.options.scanDirs.includes(dir)) {\n nitro.options.scanDirs.push(dir);\n }\n }\n\n nitro.hooks.hook('build:config', (_n, buildConfig) => {\n const rollupConfig: any = buildConfig.rollup ?? buildConfig.rolldown;\n if (Array.isArray(rollupConfig.plugins)) {\n rollupConfig.plugins.push(\n nitroH3Resolver,\n pageEndpointsPlugin(),\n serverFnIdPlugin(resolve(context.workspaceRoot, context.rootDir)),\n );\n }\n // Same rule as Nitro's own externals: worker presets ship a single\n // self-contained bundle with no module resolver.\n if (nitro.options.node && nitro.options.noExternals !== true) {\n applyAnalogNitroExternals(rollupConfig);\n }\n sanitizeNitroBundlerConfig(rollupConfig);\n });\n\n if (ssr) {\n // Override Nitro's auto-detected template-serving renderer with one\n // that routes HTML requests to our SSR service. Nitro's\n // `resolveRendererOptions` finds `index.html` at the project root\n // and installs `internal/routes/renderer-template[.dev]`, which\n // just serves the raw template. nitro/vite's own SSR-routing\n // renderer only auto-installs when both `renderer.handler` and\n // `renderer.template` are empty (vite.mjs:574), which never holds\n // for a typical app — so we install our own renderer virtual\n // explicitly here.\n //\n // `#analog/ssr` is a Nitro virtual (not a Vite virtual) so it\n // resolves under both Vite-built bundles (main) and Rolldown-built\n // bundles (Nitro's prerender, which forces builder: 'rolldown' —\n // see nitro/dist/_chunks/nitro.mjs:769). That sidesteps nitro/vite's\n // prodSetup polyfill, which is Vite-only and leaves\n // `__nitro_vite_envs__` unset in the prerender bundle.\n nitro.options.virtual['#analog/ssr'] = () =>\n generateSsrServiceVirtual(nitro);\n // Lazy: `setup()` runs before any environment builds, so reading\n // the template here would read it before the client has emitted\n // one. Nitro resolves this virtual when it bundles — the prerender\n // pass and the final server build, both after the client build.\n nitro.options.virtual['#analog/ssr-renderer'] = () =>\n generateSsrRendererVirtual(readIndexHtml());\n nitro.options.renderer ??= {};\n nitro.options.renderer.handler = '#analog/ssr-renderer';\n delete nitro.options.renderer.template;\n\n // Nitro resolves `renderer.handler` to a file on disk while it\n // creates the prerender instance, which rejects the virtual id.\n // Hide the renderer from that resolution and restore the virtual\n // handler before the prerenderer syncs its routes.\n nitro.hooks.hook('prerender:config', (prerendererConfig) => {\n prerendererConfig.renderer = false;\n // The prerender instance is created from the raw user config, so\n // pass along the rules carrying the `ssr: false` headers.\n prerendererConfig.routeRules = nitro.options.routeRules;\n });\n nitro.hooks.hook('prerender:init', (prerenderer) => {\n prerenderer.options.renderer = { handler: '#analog/ssr-renderer' };\n });\n\n // The client build emits an `index.html` shell, and copying the\n // public assets puts it in the output directory. Nitro answers from\n // the public assets ahead of the renderer, so `/` would be served\n // that shell instead of being rendered. Nitro calls this hook once\n // the assets are in place, before prerendering and the server build.\n nitro.hooks.hook('prerender:routes', () => {\n const publicDir = resolve(nitro.options.output.publicDir);\n for (const extension of ['', '.br', '.gz']) {\n const indexFile = resolve(publicDir, `index.html${extension}`);\n if (existsSync(indexFile)) {\n unlinkSync(indexFile);\n }\n }\n });\n }\n // When ssr === false, Nitro's auto-detected template-serving\n // renderer is exactly what we want (serve the raw index.html for\n // every HTML request) — leave it in place.\n\n injectAnalogRouteRuleHeaders(nitro);\n\n await wirePrerender(nitro, options, context, apiPrefix);\n\n if (options.i18n) {\n addPostRenderingHooks(nitro, [\n createI18nPostRenderingHook({\n defaultLocale: options.i18n.defaultLocale,\n locales: options.i18n.locales,\n }),\n ]);\n }\n },\n },\n };\n\n return plugin;\n}\n\nfunction hasNitroVitePlugin(userConfig: UserConfig): boolean {\n const plugins = ((userConfig.plugins ?? []) as unknown[]).flat(Infinity) as (\n | { name?: string }\n | null\n | undefined\n )[];\n return plugins.some((plugin) => plugin?.name === 'nitro:main');\n}\n\ntype ResolveIdHook = Extract<NonNullable<Plugin['resolveId']>, Function>;\n\n/**\n * Resolves `h3` from Nitro's own package, so server code always uses the h3\n * version the server runs on.\n */\nfunction nitroH3Plugin(root: string): Plugin {\n let nitroPackageJson: string | undefined;\n return {\n name: 'analogjs-nitro-h3',\n resolveId(id, _importer, resolveOptions) {\n if (id !== 'h3') {\n return null;\n }\n nitroPackageJson ??= createRequire(join(root, 'noop.js')).resolve(\n 'nitro/package.json',\n );\n return this.resolve(id, nitroPackageJson, {\n ...resolveOptions,\n skipSelf: true,\n });\n },\n };\n}\n\n/**\n * Registers the `/_analog/fn/:id` dispatch handler when the app defines server\n * functions in `*.server.ts` files.\n */\nfunction registerServerFns(nitro: Nitro, context: NitroPluginContext): void {\n const modules = getServerFnHandlers(context);\n if (!modules.length) {\n return;\n }\n\n const root = resolve(context.workspaceRoot, context.rootDir);\n const appConfigModule = [\n `${root}/${context.sourceRoot}/app/app.config.server.ts`,\n `${root}/${context.sourceRoot}/app.config.server.ts`,\n ].find((candidate) => existsSync(candidate));\n\n nitro.options.handlers.push(getServerFnDispatchHandler());\n nitro.options.virtual[SERVER_FN_DISPATCH_VIRTUAL] =\n buildServerFnDispatchModule({ modules, appConfigModule, root });\n}\n\n/**\n * Stamps the derived id into each `serverFn` in the Nitro server bundle, so it\n * registers under the route the client calls.\n */\nfunction serverFnIdPlugin(projectRoot: string): {\n name: string;\n transform(code: string, id: string): { code: string; map: null } | undefined;\n} {\n return {\n name: 'analogjs-server-fn-id',\n transform(code, id) {\n if (!id.endsWith('.server.ts')) {\n return;\n }\n const injected = injectServerFnIds(code, serverFnFileId(id, projectRoot));\n return injected ? { code: injected.code, map: null } : undefined;\n },\n };\n}\n\n/**\n * Builds the h3 handler installed as Nitro's `renderer.handler`. Short-circuits\n * `ssr: false` routes to the raw client template; otherwise dispatches the\n * request to the SSR service env (`fetchService(\"ssr\", req)` works in dev via\n * the env-runner and in prod via the `__nitro_vite_envs__` global set up by\n * nitro/vite's `prodSetup`).\n */\nfunction generateSsrRendererVirtual(template: string): string {\n return `\nimport { defineHandler } from 'nitro/h3';\nimport { getRouteRules } from 'nitro/app';\nimport ssr from '#analog/ssr';\n\nconst TEMPLATE = ${JSON.stringify(template)};\n\nexport default defineHandler(async (event) => {\n event.res.headers.set('content-type', 'text/html; charset=utf-8');\n // 'x-analog-no-ssr' is stamped on the route rule headers by\n // injectAnalogRouteRuleHeaders. Nitro writes those headers after the\n // handler runs, so read the matched rules.\n const { routeRules } = getRouteRules(event.req.method, event.url.pathname);\n if (routeRules?.headers?.['x-analog-no-ssr'] === 'true') {\n return TEMPLATE;\n }\n const service = ssr.default ?? ssr;\n // The SSR service renders without a Node response, so forward the\n // \\`streaming: false\\` rule on the request for the renderer to read. Only\n // the route rule decides it, never the client.\n const headers = new Headers(event.req.headers);\n headers.delete('x-analog-no-ssr');\n headers.delete('x-analog-no-streaming');\n if (routeRules?.headers?.['x-analog-no-streaming'] === 'true') {\n headers.set('x-analog-no-streaming', 'true');\n }\n const request = new Request(event.req.url, {\n method: event.req.method,\n headers,\n signal: event.req.signal,\n });\n request.waitUntil = (task) => event.waitUntil(task);\n return service.fetch(request);\n});\n`;\n}\n\n/**\n * Resolves \\`#analog/ssr\\` to the SSR fetch handler. The shape returned by\n * this function is bundler-agnostic: works in Vite-built main bundles and\n * Rolldown-built prerender bundles alike.\n *\n * - Dev: dispatch through nitro/vite's env runner (\\`fetchService\\`); the SSR\n * service module isn't on disk yet, so we delegate to the runner.\n * - Build / prerender: re-export the built SSR entry directly from the\n * filesystem. By the time Nitro's bundlers ask for \\`#analog/ssr\\`, Vite has\n * already produced \\`<buildDir>/vite/services/ssr/<entry>.mjs\\` (or \\`.js\\`\n * when the app's package.json sets \\`\"type\": \"module\"\\`).\n */\nfunction generateSsrServiceVirtual(nitro: Nitro): string {\n if (nitro.options.dev) {\n return `\nimport { fetchService } from 'nitro/services';\nexport default {\n // The dev runner answers from a worker, and a rejection there comes back as\n // something Nitro cannot turn into a response, so the request hangs with no\n // status and no message. Answer with the failure instead.\n async fetch(req) {\n try {\n return await fetchService('ssr', req);\n } catch (err) {\n console.error('[analog ssr]', err);\n const message = err instanceof Error ? (err.stack ?? err.message) : String(err);\n return new Response(message, {\n status: 500,\n headers: { 'content-type': 'text/plain; charset=utf-8' },\n });\n }\n },\n};\n`;\n }\n\n const ssrDir = resolve(nitro.options.buildDir, 'vite/services/ssr');\n if (!existsSync(ssrDir)) {\n return `export default { async fetch() { throw new Error('Analog SSR service directory missing: ${ssrDir}'); } };`;\n }\n const entries = readdirSync(ssrDir).filter((f) => /\\.m?js$/.test(f));\n if (entries.length === 0) {\n return `export default { async fetch() { throw new Error('No Analog SSR entry file built in: ${ssrDir}'); } };`;\n }\n // Prefer 'main.server' if present; otherwise take the only entry.\n const entry =\n entries.find((f) => /^main\\.server\\.m?js$/.test(f)) ?? entries[0];\n const entryPath = resolve(ssrDir, entry);\n return `export { default } from ${JSON.stringify(entryPath)};`;\n}\n\n/**\n * Packages Analog forces external in the Nitro server bundle. Each entry is\n * here for a specific reason — see comments.\n */\nconst ANALOG_NITRO_EXTERNALS = [\n // rxjs ships per-entry CJS/ESM facades that confuse the Nitro/Rolldown\n // resolver during bundling.\n 'rxjs',\n // node-fetch-native's polyfill subpath rewrites global fetch and isn't\n // safe to inline into the Nitro bundle.\n 'node-fetch-native/dist/polyfill',\n // sharp ships platform-specific native binaries under @img/sharp-*. pnpm\n // creates symlinks for ALL optional platform deps but only installs the\n // matching one, leaving broken symlinks that crash Nitro's externals\n // plugin with ENOENT during realpath(). Externalizing sharp avoids\n // bundling it; the user's app resolves it from node_modules at runtime.\n 'sharp',\n];\n\nfunction applyAnalogNitroExternals(rollupConfig: { external?: unknown }): void {\n // Rolldown's `external` only accepts `Array<string | RegExp>`; promote\n // whatever shape Nitro gave us (regex, single string, undefined) to an\n // array and append Analog's entries as regex patterns that also match\n // sub-paths (e.g. `sharp` matches `sharp/lib/foo`).\n const prev = rollupConfig.external;\n const existing: Array<string | RegExp> =\n prev === undefined\n ? []\n : Array.isArray(prev)\n ? (prev as Array<string | RegExp>)\n : prev instanceof RegExp\n ? [prev]\n : typeof prev === 'string'\n ? [prev]\n : [];\n\n const escapeRegExp = (s: string) => s.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n\n for (const entry of ANALOG_NITRO_EXTERNALS) {\n const pattern = new RegExp(`^${escapeRegExp(entry)}(?:/|$)`);\n if (!existing.some((p) => String(p) === String(pattern))) {\n existing.push(pattern);\n }\n }\n\n rollupConfig.external = existing;\n}\n\n/**\n * Workarounds for Nitro v3 + Rolldown bundler interaction quirks. Each\n * is narrowly scoped and can be removed once the upstream bug is fixed:\n *\n * 1. `output.codeSplitting` — Nitro 3.0.x sets this; Rolldown rejects it\n * as an unknown key.\n * 2. `output.manualChunks` — Nitro's default manual chunking crashes\n * Nitro's prerender rebundle.\n * 3. `output.chunkFileNames` — Nitro's chunk-name function produces\n * route-derived `[token]` patterns which Rollup/Rolldown interprets as\n * placeholders; we rewrite non-standard tokens to `_token_`.\n */\nfunction sanitizeNitroBundlerConfig(rollupConfig: { output?: unknown }): void {\n const output = rollupConfig.output;\n if (!output || Array.isArray(output) || typeof output !== 'object') return;\n const out = output as Record<string, unknown>;\n\n if ('codeSplitting' in out) delete out['codeSplitting'];\n if ('manualChunks' in out) delete out['manualChunks'];\n\n const VALID_ROLLUP_PLACEHOLDER = /^\\[(?:name|hash|format|ext)\\]$/;\n const chunkFileNames = out['chunkFileNames'];\n if (typeof chunkFileNames === 'function') {\n const originalFn = chunkFileNames as (...args: unknown[]) => unknown;\n out['chunkFileNames'] = (...args: unknown[]) => {\n const result = originalFn(...args);\n if (typeof result !== 'string') return result;\n return result.replace(/\\[[^\\]]+\\]/g, (match: string) =>\n VALID_ROLLUP_PLACEHOLDER.test(match)\n ? match\n : `_${match.slice(1, -1)}_`,\n );\n };\n }\n}\n\n/**\n * Walks Nitro's resolved routeRules and stamps `x-analog-no-ssr` onto any rule\n * setting `ssr`, and `x-analog-no-streaming` onto any rule setting\n * `streaming`. Nitro drops `false` values when it merges rules, so the headers\n * carry them, and a narrower `true` rule overrides with `'false'`. The SSR\n * renderer virtual above reads them from the matched rules, and they reach\n * the response as hints for downstream consumers (CDN, edge logic).\n */\nexport function injectAnalogRouteRuleHeaders(nitro: Nitro): void {\n const routeRules = nitro.options.routeRules as\n | Record<\n string,\n {\n ssr?: boolean;\n streaming?: boolean;\n headers?: Record<string, string>;\n }\n >\n | undefined;\n if (!routeRules) return;\n\n for (const rule of Object.values(routeRules)) {\n if (typeof rule?.ssr === 'boolean') {\n rule.headers = { ...rule.headers, 'x-analog-no-ssr': String(!rule.ssr) };\n }\n if (typeof rule?.streaming === 'boolean') {\n rule.headers = {\n ...rule.headers,\n 'x-analog-no-streaming': String(!rule.streaming),\n };\n }\n }\n}\n\n/**\n * Builds the SSR service entry source. The wrapper imports the user's\n * `main.server.ts` Angular renderer and adapts it to the `{ fetch(req) }`\n * shape that nitro/vite's service mechanism expects.\n */\nfunction generateSsrEntryWrapper(\n entryServer: string,\n template: string,\n): string {\n return `\n// Import \\`serverFetch\\` from the root \\`nitro\\` entry rather than \\`nitro/app\\`.\n// The /app subpath creates a fresh \\`useNitroApp()\\` instance scoped to the\n// importing bundle, which in our setup is the standalone SSR vite bundle —\n// it has no route handlers, so every fetch 404s. The root entry instead\n// reads \\`globalThis.__nitro__.{default,prerender}\\`, which the surrounding\n// Nitro server (prerender pass or production runtime) has already\n// populated with the real app + handlers.\n// Set \\`ngServerMode\\` before the Angular renderer and its app config evaluate,\n// otherwise \\`provideClientHydration()\\` registers browser-only features such\n// as event replay on the server.\nimport ${JSON.stringify(SERVER_MODE_ID)};\nimport { serverFetch as nitroServerFetch } from 'nitro';\nimport { getProxyRequestHeaders } from 'nitro/h3';\nimport { createFetch } from 'ofetch';\nimport renderer from ${JSON.stringify(entryServer)};\n\nconst TEMPLATE = ${JSON.stringify(template)};\n\nconst normalizeRequestPath = (url) =>\n url.replace(/\\\\/index\\\\.html(?=$|[?#])/, '/');\n\n// In-process fetch wired into Nitro's request pipeline. Angular's HttpClient\n// (via withFetch()) and Analog's injectLoad() call this during SSR/prerender\n// so they hit the running app's page-endpoint and API routes without going\n// through the network — the prerender pipeline doesn't have a listening\n// socket. Without this, every SSR data fetch ECONNREFUSEs and Angular's\n// router fails to resolve any data-bound route, producing an empty\n// <router-outlet/> in the prerendered HTML.\nconst ssrFetch = (resource, init) => {\n let url = typeof resource === 'string'\n ? resource\n : resource instanceof URL\n ? resource.href\n : resource.url;\n // Relative URLs from injectAPIPrefix() etc. need a host for Nitro's\n // Request constructor to accept them.\n if (typeof url === 'string' && url.startsWith('/')) {\n url = 'http://localhost' + url;\n }\n return nitroServerFetch(url, init);\n};\n\n// Wrap in ofetch so consumers that expect \\`$fetch.raw()\\` (the router's\n// request-context interceptor short-circuits SSR HttpClient calls through\n// \\`globalThis.$fetch.raw\\`) can call it. Set on globalThis so router code\n// running inside the Angular renderer can find it.\nconst ssrOFetch = createFetch({ fetch: ssrFetch });\nif (typeof globalThis.$fetch === 'undefined') {\n globalThis.$fetch = ssrOFetch;\n}\n\n// Like h3's fetchWithEvent: in-process requests carry the incoming request's\n// headers (minus hop-by-hop) and are cancelled with it. Conditional and range\n// headers describe the document, not the data it loads.\nconst DOCUMENT_ONLY_HEADERS = [\n 'if-match',\n 'if-none-match',\n 'if-modified-since',\n 'if-unmodified-since',\n 'if-range',\n 'range',\n];\nconst requestOFetch = (req) =>\n createFetch({\n fetch: (resource, init) => {\n const headers = new Headers(\n getProxyRequestHeaders(\n { req },\n { host: true, filterHeaders: DOCUMENT_ONLY_HEADERS },\n ),\n );\n new Headers(init?.headers).forEach((value, name) => headers.set(name, value));\n const signal = init?.signal\n ? AbortSignal.any([req.signal, init.signal])\n : req.signal;\n return ssrFetch(resource, { ...init, headers, signal });\n },\n });\n\nexport default {\n async fetch(req) {\n const url = new URL(req.url);\n const requestPath = normalizeRequestPath(url.pathname);\n // Preserve the query string so Angular's router + injectQuery() and\n // server data loaders that read from req.url see the full path+query.\n const requestUrl = requestPath + url.search;\n\n const reqShim = {\n headers: Object.fromEntries(req.headers.entries()),\n url: requestUrl,\n originalUrl: requestUrl,\n connection: {},\n };\n // Carries the status and headers the app sets through RESPONSE.\n const resHeaders = new Headers();\n const resShim = {\n statusCode: 200,\n getHeader(name) {\n if (name.toLowerCase() !== 'set-cookie') {\n return resHeaders.get(name) ?? undefined;\n }\n const cookies = resHeaders.getSetCookie();\n return cookies.length ? cookies : undefined;\n },\n hasHeader: (name) => resHeaders.has(name),\n setHeader(name, value) {\n resHeaders.delete(name);\n for (const item of [value].flat()) {\n resHeaders.append(name, String(item));\n }\n return resShim;\n },\n removeHeader: (name) => resHeaders.delete(name),\n };\n\n try {\n const html = await renderer(requestUrl, TEMPLATE, {\n req: reqShim,\n res: resShim,\n // Pass the ofetch-wrapped fetch — INTERNAL_FETCH is consumed by the\n // router's request-context interceptor via \\`serverFetch.raw(...)\\`,\n // which is ofetch's response-shape API. Plain fetch lacks \\`.raw\\`\n // and throws TypeError during prerender/SSR.\n fetch: requestOFetch(req),\n signal: req.signal,\n waitUntil: req.waitUntil?.bind(req),\n renderErrorsAsHtml: true,\n });\n const headers = new Headers(resHeaders);\n if (!headers.has('content-type')) {\n headers.set('content-type', 'text/html; charset=utf-8');\n }\n const status = resShim.statusCode;\n if ([204, 205, 304].includes(status)) {\n if (typeof html !== 'string') {\n await html.cancel();\n }\n return new Response(null, { status, headers });\n }\n // So intermediaries don't cache or buffer partial HTML.\n if (typeof html !== 'string' && !html[Symbol.for('analog.bufferedStream')]) {\n headers.set('content-encoding', 'identity');\n headers.set('cache-control', 'no-store, no-transform');\n }\n return new Response(html, { status, headers });\n } catch (err) {\n console.error('[analog ssr]', err);\n const status = err?.statusCode ?? err?.status;\n return new Response(TEMPLATE, {\n status: status >= 400 && status <= 599 ? status : 500,\n headers: {\n 'content-type': 'text/html; charset=utf-8',\n 'cache-control': 'no-store',\n },\n });\n }\n },\n};\n`;\n}\n\nasync function wirePrerender(\n nitro: Nitro,\n options: Options,\n context: NitroPluginContext,\n apiPrefix: string,\n): Promise<void> {\n const prerender = options.prerender;\n if (!prerender && !options.i18n) return;\n\n const {\n routes: collected,\n sitemaps: routeSitemaps,\n routeSourceFiles,\n } = await collectRoutes(prerender?.routes, context, apiPrefix);\n\n const expanded = options.i18n\n ? expandRoutesWithLocales(\n collected,\n {\n defaultLocale: options.i18n.defaultLocale,\n locales: options.i18n.locales,\n },\n apiPrefix,\n )\n : collected;\n\n const nitroPrerender = (nitro.options.prerender ??= {}) as Record<\n string,\n any\n >;\n nitroPrerender.routes ??= [];\n nitroPrerender.routes.push(...expanded);\n if (prerender?.discover ?? false) {\n nitroPrerender.crawlLinks = true;\n }\n\n if (prerender?.postRenderingHooks?.length) {\n addPostRenderingHooks(nitro, prerender.postRenderingHooks);\n }\n\n if (Object.keys(routeSourceFiles).length > 0) {\n // Mirror the legacy `@analogjs/vite-plugin-nitro` behavior: after\n // prerender completes, write the route's source content alongside the\n // prerendered HTML at `<publicDir>/<route>.md`. Drives the\n // `outputSourceFile` option on both `PrerenderRouteConfig` (string path\n // to source markdown) and `PrerenderContentDir` (per-file callback).\n nitro.hooks.hook('prerender:done', async () => {\n const publicDir = resolve(nitro.options.output.publicDir);\n const { mkdirSync, writeFileSync } = await import('node:fs');\n const { dirname, join } = await import('node:path');\n for (const [route, content] of Object.entries(routeSourceFiles)) {\n const outputPath = join(publicDir, `${route}.md`);\n mkdirSync(dirname(outputPath), { recursive: true });\n writeFileSync(outputPath, content, 'utf8');\n }\n });\n }\n\n const sitemapConfig = prerender?.sitemap;\n if (sitemapConfig) {\n nitro.hooks.hook('prerender:done', async (result) => {\n const prerenderedRoutes = (result?.prerenderedRoutes ?? []).map(\n (r: PrerenderRoute) => r.route,\n );\n const publicDir = resolve(nitro.options.output.publicDir);\n await buildSitemap(\n {} as any,\n sitemapConfig,\n prerenderedRoutes,\n publicDir,\n routeSitemaps,\n { apiPrefix },\n );\n });\n }\n}\n\nasync function collectRoutes(\n routesInput: Options['prerender'] extends infer P\n ? P extends { routes?: infer R }\n ? R\n : never\n : never,\n context: NitroPluginContext,\n apiPrefix: string,\n): Promise<{\n routes: string[];\n sitemaps: Record<string, RouteSitemap>;\n routeSourceFiles: Record<string, string>;\n}> {\n const out: string[] = [];\n const sitemaps: Record<string, RouteSitemap> = {};\n const routeSourceFiles: Record<string, string> = {};\n\n if (!routesInput) return { routes: out, sitemaps, routeSourceFiles };\n\n const inputs = Array.isArray(routesInput)\n ? routesInput\n : typeof routesInput === 'function'\n ? await routesInput()\n : [];\n\n for (const entry of inputs) {\n if (!entry) continue;\n\n if (typeof entry === 'string') {\n out.push(entry);\n continue;\n }\n\n if ('contentDir' in entry) {\n const dir = entry as PrerenderContentDir;\n const files = getMatchingContentFilesWithFrontMatter(\n context.workspaceRoot,\n context.rootDir,\n dir.contentDir,\n !!dir.recursive,\n );\n for (const file of files) {\n const route = dir.transform(file);\n if (route === false) continue;\n out.push(route);\n if (dir.sitemap) {\n sitemaps[route] =\n typeof dir.sitemap === 'function'\n ? (\n dir.sitemap as (\n f: PrerenderContentFile,\n ) => PrerenderSitemapConfig\n )(file)\n : dir.sitemap;\n }\n if (dir.outputSourceFile) {\n const sourceContent = dir.outputSourceFile(file);\n if (typeof sourceContent === 'string') {\n routeSourceFiles[route] = sourceContent;\n }\n }\n }\n continue;\n }\n\n if ('route' in entry) {\n const cfg = entry as PrerenderRouteConfig;\n out.push(cfg.route);\n if (cfg.sitemap) {\n sitemaps[cfg.route] = cfg.sitemap;\n }\n if (cfg.outputSourceFile) {\n const sourcePath = resolve(\n context.workspaceRoot,\n context.rootDir,\n cfg.outputSourceFile,\n );\n if (existsSync(sourcePath)) {\n routeSourceFiles[cfg.route] = readFileSync(sourcePath, 'utf8');\n }\n }\n if (cfg.staticData) {\n // Mirror the legacy plugin: when staticData is requested, also\n // prerender the page's data-fetching endpoint so the JSON payload\n // is available statically.\n const prefix = apiPrefix.startsWith('/') ? apiPrefix : `/${apiPrefix}`;\n const route = cfg.route.startsWith('/') ? cfg.route : `/${cfg.route}`;\n out.push(`${prefix}/_analog/pages${route}`);\n }\n }\n }\n\n return { routes: out, sitemaps, routeSourceFiles };\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAiCA,IAAM,uBAAuB;;;;;;AAkB7B,SAAgB,kBAAkB,UAAmB,CAAC,GAAW;CAC/D,MAAM,gBACJ,QAAQ,iBAAiB,QAAQ,IAAI,wBAAwB,QAAQ,IAAI;CAC3E,MAAM,aAAa;CACnB,MAAM,MAAM,QAAQ,OAAO;CAC3B,MAAM,YAAY,QAAQ,aAAa;CAEvC,IAAI,UAA8B;EAChC;EACA,SAAS;EACT;CACF;CACA,IAAI,qBAAqB;CACzB,IAAI;CACJ,IAAI,UAAU;CAGd,IAAI,UAAU;CACd,IAAI;CACJ,MAAM,+BAAe,IAAI,QAAuC;CAKhE,IAAI;CAEJ,SAAS,eAAe,aAA4C;EAClE,IAAI,QAAQ,aAAa,IAAI,WAAW;EACxC,IAAI,CAAC,OAAO;GAEV,QAAQ,QAAQ,QAAQ,YAAY,eAAe,KAAK,CAAC;GACzD,aAAa,IAAI,aAAa,KAAK;EACrC;EACA,OAAO;CACT;CAEA,SAAS,eAAe,UAA8B;EACpD,MAAM,OAAO,YAAY,QAAQ,IAAI;EACrC,UAAU;GACR;GACA,SAAS,SAAS,eAAe,IAAI,KAAK;GAC1C;EACF;EACA,qBAAqB,QACnB,QAAQ,eACR,QAAQ,SACR,yBACF;CACF;CAEA,SAAS,gBAAwB;EAC/B,OAAO,QACL,QAAQ,eACR,QAAQ,SACR,QAAQ,SAAS,YACnB;CACF;CAEA,SAAS,gBAAwB;EAG/B,IAAI,CAAC,SAAS;GACZ,MAAM,SAAS,cAAc;GAC7B,OAAO,WAAW,MAAM,IACpB,aAAa,QAAQ,OAAO,IAC5B;EACN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BAAgB,OAAO;GACzB,MAAM,MAAM;GACZ,MAAM,QAAQ,uCACZ,QAAQ,eACR,QAAQ,SACR,IAAI,YACJ,CAAC,CAAC,IAAI,SACR;GACA,KAAK,MAAM,QAAQ,OAAO;IACxB,MAAM,QAAQ,IAAI,UAAU,IAAI;IAChC,IAAI,UAAU,OAAO;IACrB,IAAI,KAAK,KAAK;IACd,IAAI,IAAI,SACN,SAAS,SACP,OAAO,IAAI,YAAY,aAEjB,IAAI,QAGJ,IAAI,IACN,IAAI;IAEZ,IAAI,IAAI,kBAAkB;KACxB,MAAM,gBAAgB,IAAI,iBAAiB,IAAI;KAC/C,IAAI,OAAO,kBAAkB,UAC3B,iBAAiB,SAAS;IAE9B;GACF;GACA;EACF;EAEA,IAAI,WAAW,OAAO;GACpB,MAAM,MAAM;GACZ,IAAI,KAAK,IAAI,KAAK;GAClB,IAAI,IAAI,SACN,SAAS,IAAI,SAAS,IAAI;GAE5B,IAAI,IAAI,kBAAkB;IACxB,MAAM,aAAa,QACjB,QAAQ,eACR,QAAQ,SACR,IAAI,gBACN;IACA,IAAI,WAAW,UAAU,GACvB,iBAAiB,IAAI,SAAS,aAAa,YAAY,MAAM;GAEjE;GACA,IAAI,IAAI,YAAY;IAIlB,MAAM,SAAS,UAAU,WAAW,GAAG,IAAI,YAAY,IAAI;IAC3D,MAAM,QAAQ,IAAI,MAAM,WAAW,GAAG,IAAI,IAAI,QAAQ,IAAI,IAAI;IAC9D,IAAI,KAAK,GAAG,OAAO,gBAAgB,OAAO;GAC5C;EACF;CACF;CAEA,OAAO;EAAE,QAAQ;EAAK;EAAU;CAAiB;AACnD"}
@@ -9,7 +9,9 @@ import type { Plugin } from "rolldown";
9
9
  * server bundle would need JIT (eval) at runtime — forbidden on
10
10
  * `workerd` / edge runtimes and unnecessary anywhere else.
11
11
  *
12
- * Loaded lazily so apps that never trigger the SSR optimizer don't
13
- * incur the babel + compiler-cli/linker cost.
12
+ * Links through `@angular/build`'s JavaScript transformer, which uses
13
+ * Angular's native (OXC) linker on 22.2+ and the Babel linker before that.
14
+ * The constructor and `transformData` calls stay within the subset shared
15
+ * by every supported `@angular/build` version.
14
16
  */
15
17
  export declare function angularLinkerPlugin(): Plugin;
@@ -1,18 +1,6 @@
1
1
  import { createRequire } from "node:module";
2
- import { pathToFileURL } from "node:url";
3
- //#region packages/platform/src/lib/nitro/angular-linker-plugin.ts
4
- var require = createRequire(import.meta.url);
5
- /**
6
- * Loads the `@babel/core` release that `@angular/compiler-cli` depends on.
7
- * The linker Babel plugin asserts the Babel major it was built against
8
- * (Angular 22.0 needs Babel 7, Angular 22.1+ needs Babel 8), so resolving
9
- * through compiler-cli keeps both in sync, including in strict pnpm layouts.
10
- */
11
- async function loadBabel() {
12
- const babelEntry = createRequire(require.resolve("@angular/compiler-cli/package.json")).resolve("@babel/core");
13
- const babel = await import(pathToFileURL(babelEntry).href);
14
- return babel.transformAsync ? babel : babel.default;
15
- }
2
+ // packages/platform/src/lib/nitro/angular-linker-plugin.ts
3
+ var require = createRequire(createRequire(import.meta.url).resolve("@analogjs/vite-plugin-angular/package.json"));
16
4
  /**
17
5
  * Rolldown plugin that runs the Angular Linker against partially-compiled
18
6
  * Angular npm packages.
@@ -23,43 +11,40 @@ async function loadBabel() {
23
11
  * server bundle would need JIT (eval) at runtime — forbidden on
24
12
  * `workerd` / edge runtimes and unnecessary anywhere else.
25
13
  *
26
- * Loaded lazily so apps that never trigger the SSR optimizer don't
27
- * incur the babel + compiler-cli/linker cost.
14
+ * Links through `@angular/build`'s JavaScript transformer, which uses
15
+ * Angular's native (OXC) linker on 22.2+ and the Babel linker before that.
16
+ * The constructor and `transformData` calls stay within the subset shared
17
+ * by every supported `@angular/build` version.
28
18
  */
29
19
  function angularLinkerPlugin() {
30
- let linkerBabelPlugin;
31
- let needsLinkingFn;
32
- let transformAsyncFn;
33
- async function ensureLoaded() {
34
- if (linkerBabelPlugin && needsLinkingFn && transformAsyncFn) return;
35
- needsLinkingFn = (await import("@angular/compiler-cli/linker")).needsLinking;
36
- const linkerBabel = await import("@angular/compiler-cli/linker/babel");
37
- linkerBabelPlugin = linkerBabel.default ?? linkerBabel;
38
- transformAsyncFn = (await loadBabel()).transformAsync;
39
- }
20
+ let transformer;
40
21
  return {
41
22
  name: "analogjs-platform-angular-linker",
42
23
  async transform(code, id) {
43
24
  if (!id.endsWith(".mjs") && !id.endsWith(".js")) return;
44
25
  if (!code.includes("ɵɵngDeclare")) return;
45
- await ensureLoaded();
46
- if (!needsLinkingFn(id, code)) return;
47
- const result = await transformAsyncFn(code, {
48
- filename: id,
49
- plugins: [linkerBabelPlugin],
50
- sourceMaps: true,
51
- compact: false,
52
- configFile: false,
53
- babelrc: false
26
+ transformer ??= new (require("@angular/build/private")).JavaScriptTransformer({
27
+ sourcemap: true,
28
+ thirdPartySourcemaps: true,
29
+ maxConcurrency: 1
54
30
  });
55
- if (result?.code) return {
56
- code: result.code,
57
- map: result.map ?? null
58
- };
31
+ const result = Buffer.from(await transformer.transformData(id, code)).toString();
32
+ return result === code ? void 0 : splitInlineSourceMap(result);
33
+ },
34
+ async buildEnd() {
35
+ await transformer?.close();
36
+ transformer = void 0;
59
37
  }
60
38
  };
61
39
  }
62
- //#endregion
40
+ function splitInlineSourceMap(code) {
41
+ const match = code.match(/\n?\/\/# sourceMappingURL=data:application\/json(?:;charset=[^;,]+)?;base64,([A-Za-z0-9+/=]+)\s*$/);
42
+ if (match?.index === void 0) return { code };
43
+ return {
44
+ code: code.slice(0, match.index),
45
+ map: Buffer.from(match[1], "base64").toString()
46
+ };
47
+ }
63
48
  export { angularLinkerPlugin };
64
49
 
65
50
  //# sourceMappingURL=angular-linker-plugin.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"angular-linker-plugin.js","names":[],"sources":["../../../../src/lib/nitro/angular-linker-plugin.ts"],"sourcesContent":["import { createRequire } from 'node:module';\nimport { pathToFileURL } from 'node:url';\n\nimport type { PluginItem, transformAsync } from '@babel/core';\nimport type { Plugin, SourceMapInput } from 'rolldown';\n\nconst require = createRequire(import.meta.url);\n\ntype BabelCore = { transformAsync: typeof transformAsync };\n\n/**\n * Loads the `@babel/core` release that `@angular/compiler-cli` depends on.\n * The linker Babel plugin asserts the Babel major it was built against\n * (Angular 22.0 needs Babel 7, Angular 22.1+ needs Babel 8), so resolving\n * through compiler-cli keeps both in sync, including in strict pnpm layouts.\n */\nasync function loadBabel(): Promise<BabelCore> {\n const compilerCliRequire = createRequire(\n require.resolve('@angular/compiler-cli/package.json'),\n );\n const babelEntry = compilerCliRequire.resolve('@babel/core');\n const babel = await import(pathToFileURL(babelEntry).href);\n return babel.transformAsync ? babel : babel.default;\n}\n\n/**\n * Rolldown plugin that runs the Angular Linker against partially-compiled\n * Angular npm packages.\n *\n * Wired into `ssr.optimizeDeps.rolldownOptions.plugins` so the SSR /\n * `nitro` environment's dep optimizer turns `ɵɵngDeclare*` partial\n * declarations into fully-compiled definitions. Without this, the\n * server bundle would need JIT (eval) at runtime — forbidden on\n * `workerd` / edge runtimes and unnecessary anywhere else.\n *\n * Loaded lazily so apps that never trigger the SSR optimizer don't\n * incur the babel + compiler-cli/linker cost.\n */\nexport function angularLinkerPlugin(): Plugin {\n let linkerBabelPlugin: PluginItem | undefined;\n let needsLinkingFn: ((id: string, code: string) => boolean) | undefined;\n let transformAsyncFn: BabelCore['transformAsync'] | undefined;\n\n async function ensureLoaded() {\n if (linkerBabelPlugin && needsLinkingFn && transformAsyncFn) return;\n\n const linker = await import('@angular/compiler-cli/linker');\n needsLinkingFn = linker.needsLinking;\n\n const linkerBabel = await import('@angular/compiler-cli/linker/babel');\n linkerBabelPlugin =\n (linkerBabel as { default?: PluginItem }).default ??\n (linkerBabel as PluginItem);\n\n transformAsyncFn = (await loadBabel()).transformAsync;\n }\n\n return {\n name: 'analogjs-platform-angular-linker',\n async transform(code: string, id: string) {\n if (!id.endsWith('.mjs') && !id.endsWith('.js')) return;\n\n // Cheap pre-check before pulling babel/compiler-cli into memory.\n if (!code.includes('ɵɵngDeclare')) return;\n\n await ensureLoaded();\n if (!needsLinkingFn!(id, code)) return;\n\n const result = await transformAsyncFn!(code, {\n filename: id,\n plugins: [linkerBabelPlugin!],\n sourceMaps: true,\n compact: false,\n configFile: false,\n babelrc: false,\n });\n\n if (result?.code) {\n return {\n code: result.code,\n map: (result.map ?? null) as SourceMapInput,\n };\n }\n return;\n },\n };\n}\n"],"mappings":";;;AAMA,IAAM,UAAU,cAAc,YAAY,GAAG;;;;;;;AAU7C,eAAe,YAAgC;CAI7C,MAAM,aAHqB,cACzB,QAAQ,QAAQ,oCAAoC,CAEnC,CAAA,CAAmB,QAAQ,aAAa;CAC3D,MAAM,QAAQ,MAAM,OAAO,cAAc,UAAU,CAAC,CAAC;CACrD,OAAO,MAAM,iBAAiB,QAAQ,MAAM;AAC9C;;;;;;;;;;;;;;AAeA,SAAgB,sBAA8B;CAC5C,IAAI;CACJ,IAAI;CACJ,IAAI;CAEJ,eAAe,eAAe;EAC5B,IAAI,qBAAqB,kBAAkB,kBAAkB;EAG7D,kBAAiB,MADI,OAAO,gCAAA,CACJ;EAExB,MAAM,cAAc,MAAM,OAAO;EACjC,oBACG,YAAyC,WACzC;EAEH,oBAAoB,MAAM,UAAU,EAAA,CAAG;CACzC;CAEA,OAAO;EACL,MAAM;EACN,MAAM,UAAU,MAAc,IAAY;GACxC,IAAI,CAAC,GAAG,SAAS,MAAM,KAAK,CAAC,GAAG,SAAS,KAAK,GAAG;GAGjD,IAAI,CAAC,KAAK,SAAS,aAAa,GAAG;GAEnC,MAAM,aAAa;GACnB,IAAI,CAAC,eAAgB,IAAI,IAAI,GAAG;GAEhC,MAAM,SAAS,MAAM,iBAAkB,MAAM;IAC3C,UAAU;IACV,SAAS,CAAC,iBAAkB;IAC5B,YAAY;IACZ,SAAS;IACT,YAAY;IACZ,SAAS;GACX,CAAC;GAED,IAAI,QAAQ,MACV,OAAO;IACL,MAAM,OAAO;IACb,KAAM,OAAO,OAAO;GACtB;EAGJ;CACF;AACF"}
1
+ {"version":3,"file":"angular-linker-plugin.js","names":[],"sources":["../../../../src/lib/nitro/angular-linker-plugin.ts"],"sourcesContent":["import { createRequire } from 'node:module';\n\nimport type { JavaScriptTransformer } from '@angular/build/private';\nimport type { Plugin } from 'rolldown';\n\n// Resolve `@angular/build` through `@analogjs/vite-plugin-angular`, which\n// declares it as a peer, so strict pnpm layouts find the consumer's copy.\nconst require = createRequire(\n createRequire(import.meta.url).resolve(\n '@analogjs/vite-plugin-angular/package.json',\n ),\n);\n\n/**\n * Rolldown plugin that runs the Angular Linker against partially-compiled\n * Angular npm packages.\n *\n * Wired into `ssr.optimizeDeps.rolldownOptions.plugins` so the SSR /\n * `nitro` environment's dep optimizer turns `ɵɵngDeclare*` partial\n * declarations into fully-compiled definitions. Without this, the\n * server bundle would need JIT (eval) at runtime — forbidden on\n * `workerd` / edge runtimes and unnecessary anywhere else.\n *\n * Links through `@angular/build`'s JavaScript transformer, which uses\n * Angular's native (OXC) linker on 22.2+ and the Babel linker before that.\n * The constructor and `transformData` calls stay within the subset shared\n * by every supported `@angular/build` version.\n */\nexport function angularLinkerPlugin(): Plugin {\n let transformer: JavaScriptTransformer | undefined;\n\n return {\n name: 'analogjs-platform-angular-linker',\n async transform(code: string, id: string) {\n if (!id.endsWith('.mjs') && !id.endsWith('.js')) return;\n\n // Cheap pre-check before starting the transformer's worker pool.\n if (!code.includes('ɵɵngDeclare')) return;\n\n transformer ??= new (require('@angular/build/private')\n .JavaScriptTransformer as typeof JavaScriptTransformer)({\n sourcemap: true,\n thirdPartySourcemaps: true,\n maxConcurrency: 1,\n });\n const result = Buffer.from(\n await transformer.transformData(id, code),\n ).toString();\n\n return result === code ? undefined : splitInlineSourceMap(result);\n },\n async buildEnd() {\n await transformer?.close();\n transformer = undefined;\n },\n };\n}\n\nfunction splitInlineSourceMap(code: string): { code: string; map?: string } {\n const match = code.match(\n /\\n?\\/\\/# sourceMappingURL=data:application\\/json(?:;charset=[^;,]+)?;base64,([A-Za-z0-9+/=]+)\\s*$/,\n );\n if (match?.index === undefined) return { code };\n return {\n code: code.slice(0, match.index),\n map: Buffer.from(match[1]!, 'base64').toString(),\n };\n}\n"],"mappings":";;AAOA,IAAM,UAAU,cACd,cAAc,YAAY,GAAG,CAAC,CAAC,QAC7B,4CACF,CACF;;;;;;;;;;;;;;;;AAiBA,SAAgB,sBAA8B;CAC5C,IAAI;CAEJ,OAAO;EACL,MAAM;EACN,MAAM,UAAU,MAAc,IAAY;GACxC,IAAI,CAAC,GAAG,SAAS,MAAM,KAAK,CAAC,GAAG,SAAS,KAAK,GAAG;GAGjD,IAAI,CAAC,KAAK,SAAS,aAAa,GAAG;GAEnC,gBAAgB,KAAK,QAAQ,wBAAwB,EAAA,CAClD,sBAAuD;IACxD,WAAW;IACX,sBAAsB;IACtB,gBAAgB;GAClB,CAAC;GACD,MAAM,SAAS,OAAO,KACpB,MAAM,YAAY,cAAc,IAAI,IAAI,CAC1C,CAAC,CAAC,SAAS;GAEX,OAAO,WAAW,OAAO,KAAA,IAAY,qBAAqB,MAAM;EAClE;EACA,MAAM,WAAW;GACf,MAAM,aAAa,MAAM;GACzB,cAAc,KAAA;EAChB;CACF;AACF;AAEA,SAAS,qBAAqB,MAA8C;CAC1E,MAAM,QAAQ,KAAK,MACjB,mGACF;CACA,IAAI,OAAO,UAAU,KAAA,GAAW,OAAO,EAAE,KAAK;CAC9C,OAAO;EACL,MAAM,KAAK,MAAM,GAAG,MAAM,KAAK;EAC/B,KAAK,OAAO,KAAK,MAAM,IAAK,QAAQ,CAAC,CAAC,SAAS;CACjD;AACF"}
@@ -1,7 +1,7 @@
1
1
  import { resolve } from "node:path";
2
2
  import { existsSync, mkdirSync, writeFileSync } from "node:fs";
3
3
  import { create } from "xmlbuilder2";
4
- //#region packages/platform/src/lib/nitro/build-sitemap.ts
4
+ // packages/platform/src/lib/nitro/build-sitemap.ts
5
5
  async function buildSitemap(_config, sitemapConfig, routes, outputDir, routeSitemaps, buildOptions = {}) {
6
6
  const host = normalizeSitemapHost(sitemapConfig.host);
7
7
  const sitemapData = await resolveSitemapEntries(await collectSitemapRoutes(routes, sitemapConfig.include), host, routeSitemaps, sitemapConfig, buildOptions);
@@ -124,14 +124,13 @@ async function resolveRouteInputs(routes) {
124
124
  return routeList.filter(Boolean);
125
125
  }
126
126
  function createXml(elementName, includeXhtml = false) {
127
- const attrs = { xmlns: "https://www.sitemaps.org/schemas/sitemap/0.9" };
128
- if (includeXhtml) attrs["xmlns:xhtml"] = "https://www.w3.org/1999/xhtml";
127
+ const attrs = { xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9" };
128
+ if (includeXhtml) attrs["xmlns:xhtml"] = "http://www.w3.org/1999/xhtml";
129
129
  return create({
130
130
  version: "1.0",
131
131
  encoding: "UTF-8"
132
132
  }).ele(elementName, attrs).com(`This file was automatically generated by Analog.`);
133
133
  }
134
- //#endregion
135
134
  export { buildSitemap };
136
135
 
137
136
  //# sourceMappingURL=build-sitemap.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"build-sitemap.js","names":[],"sources":["../../../../src/lib/nitro/build-sitemap.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { create } from 'xmlbuilder2';\nimport type { XMLBuilder } from 'xmlbuilder2/lib/interfaces';\nimport { UserConfig } from 'vite';\nimport type {\n I18nPrerenderOptions,\n PrerenderSitemapConfig,\n SitemapConfig,\n SitemapEntry,\n SitemapExcludeRule,\n SitemapRouteDefinition,\n SitemapRouteInput,\n SitemapRouteSource,\n} from './types.js';\n\ntype RouteSitemapConfig =\n | PrerenderSitemapConfig\n | (() => PrerenderSitemapConfig)\n | undefined;\n\nexport type PagesJson = SitemapEntry;\n\nexport interface BuildSitemapOptions {\n apiPrefix?: string;\n}\n\nexport async function buildSitemap(\n _config: UserConfig,\n sitemapConfig: SitemapConfig,\n routes: (string | undefined)[] | (() => Promise<(string | undefined)[]>),\n outputDir: string,\n routeSitemaps: Record<string, RouteSitemapConfig>,\n buildOptions: BuildSitemapOptions = {},\n): Promise<void> {\n const host = normalizeSitemapHost(sitemapConfig.host);\n const routeList = await collectSitemapRoutes(routes, sitemapConfig.include);\n const sitemapData = await resolveSitemapEntries(\n routeList,\n host,\n routeSitemaps,\n sitemapConfig,\n buildOptions,\n );\n\n if (!sitemapData.length) {\n return;\n }\n\n const sitemap = createXml('urlset');\n\n for (const item of sitemapData) {\n const page = sitemap.ele('url');\n page.ele('loc').txt(item.loc);\n\n if (item.lastmod) {\n page.ele('lastmod').txt(item.lastmod);\n }\n\n if (item.changefreq) {\n page.ele('changefreq').txt(item.changefreq);\n }\n\n if (item.priority !== undefined) {\n page.ele('priority').txt(String(item.priority));\n }\n }\n\n const resolvedOutputDir = resolve(outputDir);\n const mapPath = resolve(resolvedOutputDir, 'sitemap.xml');\n try {\n if (!resolvedOutputDir || resolvedOutputDir === resolve()) {\n throw new Error(\n 'Refusing to write the sitemap to the current working directory. Expected the Nitro public output directory instead.',\n );\n }\n\n if (!existsSync(resolvedOutputDir)) {\n mkdirSync(resolvedOutputDir, { recursive: true });\n }\n console.log(`Writing sitemap at ${mapPath}`);\n writeFileSync(mapPath, sitemap.end({ prettyPrint: true }));\n } catch (e) {\n console.error(`Unable to write file at ${mapPath}`, e);\n }\n}\n\nasync function resolveSitemapEntries(\n routes: SitemapRouteInput[],\n host: string,\n routeSitemaps: Record<string, RouteSitemapConfig>,\n sitemapConfig: SitemapConfig,\n buildOptions: BuildSitemapOptions,\n): Promise<SitemapEntry[]> {\n const defaults = sitemapConfig.defaults ?? {};\n const seen = new Set<string>();\n const entries: SitemapEntry[] = [];\n\n for (const route of routes) {\n const entry = await toSitemapEntry(\n route,\n host,\n routeSitemaps,\n defaults,\n sitemapConfig.transform,\n );\n\n if (!entry) {\n continue;\n }\n\n if (\n isInternalSitemapRoute(entry.route, buildOptions.apiPrefix) ||\n (await isExcludedSitemapRoute(entry, sitemapConfig.exclude))\n ) {\n continue;\n }\n\n if (seen.has(entry.loc)) {\n continue;\n }\n\n seen.add(entry.loc);\n entries.push(entry);\n }\n\n return entries;\n}\n\nasync function toSitemapEntry(\n route: SitemapRouteInput,\n host: string,\n routeSitemaps: Record<string, RouteSitemapConfig>,\n defaults: PrerenderSitemapConfig,\n transform: SitemapConfig['transform'],\n): Promise<SitemapEntry | undefined> {\n const normalizedRoute = normalizeSitemapRoute(\n typeof route === 'string' ? route : route?.route,\n );\n if (!normalizedRoute) {\n return undefined;\n }\n\n const baseEntry = createSitemapEntry(\n {\n ...defaults,\n ...resolveRouteSitemapConfig(routeSitemaps[normalizedRoute]),\n ...(typeof route === 'object' ? route : {}),\n route: normalizedRoute,\n },\n host,\n );\n\n if (!transform) {\n return baseEntry;\n }\n\n const transformed = await transform(baseEntry);\n if (!transformed) {\n return undefined;\n }\n\n return createSitemapEntry(\n {\n ...baseEntry,\n ...transformed,\n },\n host,\n );\n}\n\nfunction createSitemapEntry(\n routeDefinition: SitemapRouteDefinition,\n host: string,\n): SitemapEntry {\n const route = normalizeSitemapRoute(routeDefinition.route) ?? '/';\n\n return {\n route,\n loc: new URL(route, ensureTrailingSlash(host)).toString(),\n lastmod: routeDefinition.lastmod,\n changefreq: routeDefinition.changefreq,\n priority: routeDefinition.priority,\n };\n}\n\nfunction resolveRouteSitemapConfig(\n config: RouteSitemapConfig,\n): PrerenderSitemapConfig {\n if (!config) {\n return {};\n }\n\n return typeof config === 'function' ? config() : config;\n}\n\nfunction normalizeSitemapHost(host: string): string {\n const resolvedHost = new URL(host);\n resolvedHost.hash = '';\n return resolvedHost.toString();\n}\n\nfunction ensureTrailingSlash(host: string): string {\n return host.endsWith('/') ? host : `${host}/`;\n}\n\nfunction normalizeSitemapRoute(route: string | undefined): string | undefined {\n if (!route) {\n return undefined;\n }\n\n const trimmedRoute = route.trim();\n if (!trimmedRoute) {\n return undefined;\n }\n\n const pathWithQuery = trimmedRoute.split('#', 1)[0] ?? '';\n const [pathname, search] = pathWithQuery.split('?', 2);\n const normalizedPathname = pathname\n ? `/${pathname.replace(/^\\/+/, '').replace(/\\/{2,}/g, '/')}`\n : '/';\n\n return search ? `${normalizedPathname}?${search}` : normalizedPathname;\n}\n\nfunction isInternalSitemapRoute(route: string, apiPrefix = 'api'): boolean {\n const normalizedApiPrefix = normalizeSitemapRoute(`/${apiPrefix}`) ?? '/api';\n return (\n route === `${normalizedApiPrefix}/_analog/pages` ||\n route.startsWith(`${normalizedApiPrefix}/_analog/pages/`)\n );\n}\n\nasync function isExcludedSitemapRoute(\n entry: SitemapEntry,\n excludeRules: SitemapExcludeRule[] | undefined,\n): Promise<boolean> {\n if (!excludeRules?.length) {\n return false;\n }\n\n for (const rule of excludeRules) {\n if (typeof rule === 'function') {\n if (await rule(entry)) {\n return true;\n }\n continue;\n }\n\n if (rule instanceof RegExp) {\n if (rule.test(entry.route)) {\n return true;\n }\n continue;\n }\n\n if (toGlobRegExp(rule).test(entry.route)) {\n return true;\n }\n }\n\n return false;\n}\n\nfunction toGlobRegExp(pattern: string): RegExp {\n const doubleStarToken = '__ANALOG_DOUBLE_STAR__';\n const singleStarToken = '__ANALOG_SINGLE_STAR__';\n const escapedPattern = pattern\n .replace(/\\*\\*/g, doubleStarToken)\n .replace(/\\*/g, singleStarToken)\n .replace(/[.+^${}()|[\\]\\\\]/g, '\\\\$&');\n const regexPattern = escapedPattern\n .replace(new RegExp(doubleStarToken, 'g'), '.*')\n .replace(new RegExp(singleStarToken, 'g'), '[^/]*');\n return new RegExp(`^${regexPattern}$`);\n}\n\nasync function collectSitemapRoutes(\n routes: (string | undefined)[] | (() => Promise<(string | undefined)[]>),\n include?: SitemapRouteSource,\n): Promise<SitemapRouteInput[]> {\n const routeList = await resolveRouteInputs(routes);\n const includedRoutes = include ? await resolveRouteInputs(include) : [];\n return [...routeList, ...includedRoutes];\n}\n\nasync function resolveRouteInputs(\n routes:\n | SitemapRouteSource\n | (string | undefined)[]\n | (() => Promise<(string | undefined)[]>),\n): Promise<SitemapRouteInput[]> {\n let routeList: SitemapRouteInput[];\n\n if (typeof routes === 'function') {\n routeList = await routes();\n } else if (Array.isArray(routes)) {\n routeList = routes;\n } else {\n routeList = [];\n }\n\n return routeList.filter(Boolean);\n}\n\n/**\n * Generates hreflang alternate URLs for a given page URL.\n * For a URL like `https://example.com/fr/about`, it produces alternates\n * for all configured locales.\n */\nexport function getHreflangAlternates(\n pageUrl: string,\n host: string,\n i18n: I18nPrerenderOptions,\n): { locale: string; href: string }[] {\n const alternates: { locale: string; href: string }[] = [];\n const normalizedHost = host.replace(/\\/+$/, '');\n\n const path = pageUrl.replace(normalizedHost, '');\n const basePath = stripLocalePrefix(path, i18n.locales);\n\n for (const locale of i18n.locales) {\n const localizedPath =\n basePath === '/' || basePath === ''\n ? `/${locale}`\n : `/${locale}${basePath}`;\n alternates.push({\n locale,\n href: `${normalizedHost}${localizedPath}`,\n });\n }\n\n const defaultPath =\n basePath === '/' || basePath === ''\n ? `/${i18n.defaultLocale}`\n : `/${i18n.defaultLocale}${basePath}`;\n alternates.push({\n locale: 'x-default',\n href: `${normalizedHost}${defaultPath}`,\n });\n\n return alternates;\n}\n\n/**\n * Strips a locale prefix from a URL path.\n * E.g., '/fr/about' -> '/about', '/en' -> '/'\n */\nexport function stripLocalePrefix(path: string, locales: string[]): string {\n const segments = path.split('/').filter(Boolean);\n if (segments.length > 0 && locales.includes(segments[0])) {\n const rest = segments.slice(1).join('/');\n return rest ? `/${rest}` : '/';\n }\n return path || '/';\n}\n\nfunction createXml(\n elementName: 'urlset' | 'sitemapindex',\n includeXhtml = false,\n): XMLBuilder {\n const attrs: Record<string, string> = {\n xmlns: 'https://www.sitemaps.org/schemas/sitemap/0.9',\n };\n if (includeXhtml) {\n attrs['xmlns:xhtml'] = 'https://www.w3.org/1999/xhtml';\n }\n\n return create({ version: '1.0', encoding: 'UTF-8' })\n .ele(elementName, attrs)\n .com(`This file was automatically generated by Analog.`);\n}\n"],"mappings":";;;;AA2BA,eAAsB,aACpB,SACA,eACA,QACA,WACA,eACA,eAAoC,CA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ACjD,MAAM,iBAAiB,UAAU,MAAM,mBAAmB,OAAO,IAAI,CAAC;CACtE,OAAO,CAAC,GAAG,WAAW,GAAG,cAAc;AACzC;AAEA,eAAe,mBACb,QAI8B;CAC9B,IAAI;CAEJ,IAAI,OAAO,WAAW,YACpB,YAAY,MAAM,OAAO;MACpB,IAAI,MAAM,QAAQ,MAAM,GAC7B,YAAY;MAEZ,YAAY,CAAC;CAGf,OAAO,UAAU,OAAO,OAAO;AACjC;AAsDA,SAAS,UACP,aACA,eAAe,OACH;CACZ,MAAM,QAAgC,EACpC,OAAO,+CACT;CACA,IAAI,cACF,MAAM,iBAAiB;CAGzB,OAAO,OAAO;EAAE,SAAS;EAAO,UAAU;CAAQ,CAAC,CAAC,CACjD,IAAI,aAAa,KAAK,CAAC,CACvB,IAAI,kDAAkD;AAC3D"}
1
+ {"version":3,"file":"build-sitemap.js","names":[],"sources":["../../../../src/lib/nitro/build-sitemap.ts"],"sourcesContent":["import { existsSync, mkdirSync, writeFileSync } from 'node:fs';\nimport { resolve } from 'node:path';\nimport { create } from 'xmlbuilder2';\nimport type { XMLBuilder } from 'xmlbuilder2/lib/interfaces';\nimport { UserConfig } from 'vite';\nimport type {\n I18nPrerenderOptions,\n PrerenderSitemapConfig,\n SitemapConfig,\n SitemapEntry,\n SitemapExcludeRule,\n SitemapRouteDefinition,\n SitemapRouteInput,\n SitemapRouteSource,\n} from './types.js';\n\ntype RouteSitemapConfig =\n | PrerenderSitemapConfig\n | (() => PrerenderSitemapConfig)\n | undefined;\n\nexport type PagesJson = SitemapEntry;\n\nexport interface BuildSitemapOptions {\n apiPrefix?: string;\n}\n\nexport async function buildSitemap(\n _config: UserConfig,\n sitemapConfig: SitemapConfig,\n routes: (string | undefined)[] | (() => Promise<(string | undefined)[]>),\n outputDir: string,\n routeSitemaps: Record<string, RouteSitemapConfig>,\n buildOptions: BuildSitemapOptions = {},\n): Promise<void> {\n const host = normalizeSitemapHost(sitemapConfig.host);\n const routeList = await collectSitemapRoutes(routes, sitemapConfig.include);\n const sitemapData = await resolveSitemapEntries(\n routeList,\n host,\n routeSitemaps,\n sitemapConfig,\n buildOptions,\n );\n\n if (!sitemapData.length) {\n return;\n }\n\n const sitemap = createXml('urlset');\n\n for (const item of sitemapData) {\n const page = sitemap.ele('url');\n page.ele('loc').txt(item.loc);\n\n if (item.lastmod) {\n page.ele('lastmod').txt(item.lastmod);\n }\n\n if (item.changefreq) {\n page.ele('changefreq').txt(item.changefreq);\n }\n\n if (item.priority !== undefined) {\n page.ele('priority').txt(String(item.priority));\n }\n }\n\n const resolvedOutputDir = resolve(outputDir);\n const mapPath = resolve(resolvedOutputDir, 'sitemap.xml');\n try {\n if (!resolvedOutputDir || resolvedOutputDir === resolve()) {\n throw new Error(\n 'Refusing to write the sitemap to the current working directory. Expected the Nitro public output directory instead.',\n );\n }\n\n if (!existsSync(resolvedOutputDir)) {\n mkdirSync(resolvedOutputDir, { recursive: true });\n }\n console.log(`Writing sitemap at ${mapPath}`);\n writeFileSync(mapPath, sitemap.end({ prettyPrint: true }));\n } catch (e) {\n console.error(`Unable to write file at ${mapPath}`, e);\n }\n}\n\nasync function resolveSitemapEntries(\n routes: SitemapRouteInput[],\n host: string,\n routeSitemaps: Record<string, RouteSitemapConfig>,\n sitemapConfig: SitemapConfig,\n buildOptions: BuildSitemapOptions,\n): Promise<SitemapEntry[]> {\n const defaults = sitemapConfig.defaults ?? {};\n const seen = new Set<string>();\n const entries: SitemapEntry[] = [];\n\n for (const route of routes) {\n const entry = await toSitemapEntry(\n route,\n host,\n routeSitemaps,\n defaults,\n sitemapConfig.transform,\n );\n\n if (!entry) {\n continue;\n }\n\n if (\n isInternalSitemapRoute(entry.route, buildOptions.apiPrefix) ||\n (await isExcludedSitemapRoute(entry, sitemapConfig.exclude))\n ) {\n continue;\n }\n\n if (seen.has(entry.loc)) {\n continue;\n }\n\n seen.add(entry.loc);\n entries.push(entry);\n }\n\n return entries;\n}\n\nasync function toSitemapEntry(\n route: SitemapRouteInput,\n host: string,\n routeSitemaps: Record<string, RouteSitemapConfig>,\n defaults: PrerenderSitemapConfig,\n transform: SitemapConfig['transform'],\n): Promise<SitemapEntry | undefined> {\n const normalizedRoute = normalizeSitemapRoute(\n typeof route === 'string' ? route : route?.route,\n );\n if (!normalizedRoute) {\n return undefined;\n }\n\n const baseEntry = createSitemapEntry(\n {\n ...defaults,\n ...resolveRouteSitemapConfig(routeSitemaps[normalizedRoute]),\n ...(typeof route === 'object' ? route : {}),\n route: normalizedRoute,\n },\n host,\n );\n\n if (!transform) {\n return baseEntry;\n }\n\n const transformed = await transform(baseEntry);\n if (!transformed) {\n return undefined;\n }\n\n return createSitemapEntry(\n {\n ...baseEntry,\n ...transformed,\n },\n host,\n );\n}\n\nfunction createSitemapEntry(\n routeDefinition: SitemapRouteDefinition,\n host: string,\n): SitemapEntry {\n const route = normalizeSitemapRoute(routeDefinition.route) ?? '/';\n\n return {\n route,\n loc: new URL(route, ensureTrailingSlash(host)).toString(),\n lastmod: routeDefinition.lastmod,\n changefreq: routeDefinition.changefreq,\n priority: routeDefinition.priority,\n };\n}\n\nfunction resolveRouteSitemapConfig(\n config: RouteSitemapConfig,\n): PrerenderSitemapConfig {\n if (!config) {\n return {};\n }\n\n return typeof config === 'function' ? config() : config;\n}\n\nfunction normalizeSitemapHost(host: string): string {\n const resolvedHost = new URL(host);\n resolvedHost.hash = '';\n return resolvedHost.toString();\n}\n\nfunction ensureTrailingSlash(host: string): string {\n return host.endsWith('/') ? host : `${host}/`;\n}\n\nfunction normalizeSitemapRoute(route: string | undefined): string | undefined {\n if (!route) {\n return undefined;\n }\n\n const trimmedRoute = route.trim();\n if (!trimmedRoute) {\n return undefined;\n }\n\n const pathWithQuery = trimmedRoute.split('#', 1)[0] ?? '';\n const [pathname, search] = pathWithQuery.split('?', 2);\n const normalizedPathname = pathname\n ? `/${pathname.replace(/^\\/+/, '').replace(/\\/{2,}/g, '/')}`\n : '/';\n\n return search ? `${normalizedPathname}?${search}` : normalizedPathname;\n}\n\nfunction isInternalSitemapRoute(route: string, apiPrefix = 'api'): boolean {\n const normalizedApiPrefix = normalizeSitemapRoute(`/${apiPrefix}`) ?? '/api';\n return (\n route === `${normalizedApiPrefix}/_analog/pages` ||\n route.startsWith(`${normalizedApiPrefix}/_analog/pages/`)\n );\n}\n\nasync function isExcludedSitemapRoute(\n entry: SitemapEntry,\n excludeRules: SitemapExcludeRule[] | undefined,\n): Promise<boolean> {\n if (!excludeRules?.length) {\n return false;\n }\n\n for (const rule of excludeRules) {\n if (typeof rule === 'function') {\n if (await rule(entry)) {\n return true;\n }\n continue;\n }\n\n if (rule instanceof RegExp) {\n if (rule.test(entry.route)) {\n return true;\n }\n continue;\n }\n\n if (toGlobRegExp(rule).test(entry.route)) {\n return true;\n }\n }\n\n return false;\n}\n\nfunction toGlobRegExp(pattern: string): RegExp {\n const doubleStarToken = '__ANALOG_DOUBLE_STAR__';\n const singleStarToken = '__ANALOG_SINGLE_STAR__';\n const escapedPattern = pattern\n .replace(/\\*\\*/g, doubleStarToken)\n .replace(/\\*/g, singleStarToken)\n .replace(/[.+^${}()|[\\]\\\\]/g, '\\\\$&');\n const regexPattern = escapedPattern\n .replace(new RegExp(doubleStarToken, 'g'), '.*')\n .replace(new RegExp(singleStarToken, 'g'), '[^/]*');\n return new RegExp(`^${regexPattern}$`);\n}\n\nasync function collectSitemapRoutes(\n routes: (string | undefined)[] | (() => Promise<(string | undefined)[]>),\n include?: SitemapRouteSource,\n): Promise<SitemapRouteInput[]> {\n const routeList = await resolveRouteInputs(routes);\n const includedRoutes = include ? await resolveRouteInputs(include) : [];\n return [...routeList, ...includedRoutes];\n}\n\nasync function resolveRouteInputs(\n routes:\n | SitemapRouteSource\n | (string | undefined)[]\n | (() => Promise<(string | undefined)[]>),\n): Promise<SitemapRouteInput[]> {\n let routeList: SitemapRouteInput[];\n\n if (typeof routes === 'function') {\n routeList = await routes();\n } else if (Array.isArray(routes)) {\n routeList = routes;\n } else {\n routeList = [];\n }\n\n return routeList.filter(Boolean);\n}\n\n/**\n * Generates hreflang alternate URLs for a given page URL.\n * For a URL like `https://example.com/fr/about`, it produces alternates\n * for all configured locales.\n */\nexport function getHreflangAlternates(\n pageUrl: string,\n host: string,\n i18n: I18nPrerenderOptions,\n): { locale: string; href: string }[] {\n const alternates: { locale: string; href: string }[] = [];\n const normalizedHost = host.replace(/\\/+$/, '');\n\n const path = pageUrl.replace(normalizedHost, '');\n const basePath = stripLocalePrefix(path, i18n.locales);\n\n for (const locale of i18n.locales) {\n const localizedPath =\n basePath === '/' || basePath === ''\n ? `/${locale}`\n : `/${locale}${basePath}`;\n alternates.push({\n locale,\n href: `${normalizedHost}${localizedPath}`,\n });\n }\n\n const defaultPath =\n basePath === '/' || basePath === ''\n ? `/${i18n.defaultLocale}`\n : `/${i18n.defaultLocale}${basePath}`;\n alternates.push({\n locale: 'x-default',\n href: `${normalizedHost}${defaultPath}`,\n });\n\n return alternates;\n}\n\n/**\n * Strips a locale prefix from a URL path.\n * E.g., '/fr/about' -> '/about', '/en' -> '/'\n */\nexport function stripLocalePrefix(path: string, locales: string[]): string {\n const segments = path.split('/').filter(Boolean);\n if (segments.length > 0 && locales.includes(segments[0])) {\n const rest = segments.slice(1).join('/');\n return rest ? `/${rest}` : '/';\n }\n return path || '/';\n}\n\nfunction createXml(\n elementName: 'urlset' | 'sitemapindex',\n includeXhtml = false,\n): XMLBuilder {\n const attrs: Record<string, string> = {\n xmlns: 'http://www.sitemaps.org/schemas/sitemap/0.9',\n };\n if (includeXhtml) {\n attrs['xmlns:xhtml'] = 'http://www.w3.org/1999/xhtml';\n }\n\n return create({ version: '1.0', encoding: 'UTF-8' })\n .ele(elementName, attrs)\n .com(`This file was automatically generated by Analog.`);\n}\n"],"mappings":";;;;AA2BA,eAAsB,aACpB,SACA,eACA,QACA,WACA,eACA,eAAoC,CAAC,GACtB;CACf,MAAM,OAAO,qBAAqB,cAAc,IAAI;CAEpD,MAAM,cAAc,MAAM,sBACxB,MAFsB,qBA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AG,cAAc;AACzC;AAEA,eAAe,mBACb,QAI8B;CAC9B,IAAI;CAEJ,IAAI,OAAO,WAAW,YACpB,YAAY,MAAM,OAAO;MACpB,IAAI,MAAM,QAAQ,MAAM,GAC7B,YAAY;MAEZ,YAAY,CAAC;CAGf,OAAO,UAAU,OAAO,OAAO;AACjC;AAsDA,SAAS,UACP,aACA,eAAe,OACH;CACZ,MAAM,QAAgC,EACpC,OAAO,8CACT;CACA,IAAI,cACF,MAAM,iBAAiB;CAGzB,OAAO,OAAO;EAAE,SAAS;EAAO,UAAU;CAAQ,CAAC,CAAC,CACjD,IAAI,aAAa,KAAK,CAAC,CACvB,IAAI,kDAAkD;AAC3D"}
@@ -1,5 +1,5 @@
1
1
  import { createDebug } from "obug";
2
- //#region packages/platform/src/lib/nitro/debug.ts
2
+ // packages/platform/src/lib/nitro/debug.ts
3
3
  var debugNitro = createDebug("analog:nitro");
4
4
  var debugSsr = createDebug("analog:nitro:ssr");
5
5
  var debugPrerender = createDebug("analog:nitro:prerender");
@@ -9,7 +9,6 @@ var nitroDebugInstances = [
9
9
  debugSsr,
10
10
  debugPrerender
11
11
  ];
12
- //#endregion
13
12
  export { debugNitro, debugPrerender, debugSsr, nitroDebugInstances };
14
13
 
15
14
  //# sourceMappingURL=debug.js.map
@@ -3,7 +3,7 @@ import { basename, join, relative, resolve } from "node:path";
3
3
  import { readFileSync } from "node:fs";
4
4
  import { normalizePath } from "vite";
5
5
  import { globSync } from "tinyglobby";
6
- //#region packages/platform/src/lib/nitro/get-content-files.ts
6
+ // packages/platform/src/lib/nitro/get-content-files.ts
7
7
  var require = createRequire(import.meta.url);
8
8
  /**
9
9
  * Discovers content files with front matter and extracts metadata for prerendering.
@@ -47,7 +47,6 @@ function getMatchingContentFilesWithFrontMatter(workspaceRoot, rootDir, glob, re
47
47
  };
48
48
  });
49
49
  }
50
- //#endregion
51
50
  export { getMatchingContentFilesWithFrontMatter };
52
51
 
53
52
  //# sourceMappingURL=get-content-files.js.map
@@ -1,22 +1,7 @@
1
1
  import type { NitroEventHandler } from "nitro/types";
2
- type GetHandlersArgs = {
3
- workspaceRoot: string;
4
- sourceRoot: string;
5
- rootDir: string;
6
- additionalPagesDirs?: string[];
7
- };
2
+ import { getPageEndpoints } from "../get-page-endpoints.js";
8
3
  /**
9
- * Discovers and generates Nitro event handlers for server-side page routes.
10
- *
11
- * Discovers all `.server.ts` files under `app/pages/**` and any additional
12
- * pages directories, then maps each file to a Nitro route pattern under
13
- * `/api/_analog/pages/...`.
14
- *
15
- * Route transformation examples:
16
- * - index.server.ts → /api/_analog/pages/index
17
- * - users/[id].server.ts → /api/_analog/pages/users/:id
18
- * - products/[...slug].server.ts → /api/_analog/pages/products/**:slug
19
- * - (auth)/login.server.ts → /api/_analog/pages/-auth-/login
4
+ * Nitro event handlers for server-side page routes: each discovered page
5
+ * endpoint, mounted under `/api/_analog`.
20
6
  */
21
- export declare function getPageHandlers({ workspaceRoot, sourceRoot, rootDir, additionalPagesDirs }: GetHandlersArgs): NitroEventHandler[];
22
- export {};
7
+ export declare function getPageHandlers(args: Parameters<typeof getPageEndpoints>[0]): NitroEventHandler[];