@octanejs/tanstack-start 0.1.0

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 (266) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +52 -0
  3. package/THIRD_PARTY_NOTICES.md +11 -0
  4. package/package.json +149 -0
  5. package/src/client-only.d.ts +1 -0
  6. package/src/client-only.js +0 -0
  7. package/src/client-rpc.d.ts +1 -0
  8. package/src/client-rpc.js +1 -0
  9. package/src/client.d.ts +3 -0
  10. package/src/client.js +2 -0
  11. package/src/default-entry/client.js +12 -0
  12. package/src/default-entry/server.js +13 -0
  13. package/src/default-entry/start.js +1 -0
  14. package/src/default-entry-paths.d.ts +5 -0
  15. package/src/default-entry-paths.js +11 -0
  16. package/src/index.d.ts +11 -0
  17. package/src/index.js +22 -0
  18. package/src/internal/README.md +15 -0
  19. package/src/internal/router-generator/config.d.ts +255 -0
  20. package/src/internal/router-generator/config.js +134 -0
  21. package/src/internal/router-generator/filesystem/physical/getRouteNodes.d.ts +53 -0
  22. package/src/internal/router-generator/filesystem/physical/getRouteNodes.js +335 -0
  23. package/src/internal/router-generator/filesystem/physical/rootPathId.d.ts +1 -0
  24. package/src/internal/router-generator/filesystem/physical/rootPathId.js +4 -0
  25. package/src/internal/router-generator/filesystem/virtual/config.d.ts +3 -0
  26. package/src/internal/router-generator/filesystem/virtual/config.js +36 -0
  27. package/src/internal/router-generator/filesystem/virtual/getRouteNodes.d.ts +31 -0
  28. package/src/internal/router-generator/filesystem/virtual/getRouteNodes.js +272 -0
  29. package/src/internal/router-generator/filesystem/virtual/loadConfigFile.d.ts +1 -0
  30. package/src/internal/router-generator/filesystem/virtual/loadConfigFile.js +10 -0
  31. package/src/internal/router-generator/generator.d.ts +119 -0
  32. package/src/internal/router-generator/generator.js +1080 -0
  33. package/src/internal/router-generator/index.d.ts +40 -0
  34. package/src/internal/router-generator/index.js +49 -0
  35. package/src/internal/router-generator/logger.d.ts +8 -0
  36. package/src/internal/router-generator/logger.js +30 -0
  37. package/src/internal/router-generator/plugin/types.d.ts +37 -0
  38. package/src/internal/router-generator/template.d.ts +38 -0
  39. package/src/internal/router-generator/template.js +220 -0
  40. package/src/internal/router-generator/transform/transform.d.ts +8 -0
  41. package/src/internal/router-generator/transform/transform.js +385 -0
  42. package/src/internal/router-generator/transform/types.d.ts +34 -0
  43. package/src/internal/router-generator/types.d.ts +73 -0
  44. package/src/internal/router-generator/utils.d.ts +270 -0
  45. package/src/internal/router-generator/utils.js +864 -0
  46. package/src/internal/router-generator/validate-route-params.d.ts +13 -0
  47. package/src/internal/router-generator/validate-route-params.js +75 -0
  48. package/src/internal/router-plugin/context.d.ts +2 -0
  49. package/src/internal/router-plugin/context.js +2 -0
  50. package/src/internal/router-plugin/core/code-splitter/compilers.d.ts +69 -0
  51. package/src/internal/router-plugin/core/code-splitter/compilers.js +1042 -0
  52. package/src/internal/router-plugin/core/code-splitter/framework-options.d.ts +11 -0
  53. package/src/internal/router-plugin/core/code-splitter/framework-options.js +41 -0
  54. package/src/internal/router-plugin/core/code-splitter/plugins/framework-plugins.d.ts +12 -0
  55. package/src/internal/router-plugin/core/code-splitter/plugins/framework-plugins.js +35 -0
  56. package/src/internal/router-plugin/core/code-splitter/plugins/octane-hmr-split-route-components.d.ts +5 -0
  57. package/src/internal/router-plugin/core/code-splitter/plugins/octane-hmr-split-route-components.js +106 -0
  58. package/src/internal/router-plugin/core/code-splitter/plugins/octane-split-route-components.d.ts +8 -0
  59. package/src/internal/router-plugin/core/code-splitter/plugins/octane-split-route-components.js +85 -0
  60. package/src/internal/router-plugin/core/code-splitter/plugins/react-refresh-ignored-route-exports.d.ts +2 -0
  61. package/src/internal/router-plugin/core/code-splitter/plugins/react-refresh-ignored-route-exports.js +55 -0
  62. package/src/internal/router-plugin/core/code-splitter/plugins/react-refresh-route-components.d.ts +2 -0
  63. package/src/internal/router-plugin/core/code-splitter/plugins/react-refresh-route-components.js +50 -0
  64. package/src/internal/router-plugin/core/code-splitter/plugins/react-stable-hmr-split-route-components.d.ts +5 -0
  65. package/src/internal/router-plugin/core/code-splitter/plugins/react-stable-hmr-split-route-components.js +54 -0
  66. package/src/internal/router-plugin/core/code-splitter/plugins.d.ts +62 -0
  67. package/src/internal/router-plugin/core/code-splitter/types.d.ts +9 -0
  68. package/src/internal/router-plugin/core/config.d.ts +294 -0
  69. package/src/internal/router-plugin/core/config.js +56 -0
  70. package/src/internal/router-plugin/core/constants.d.ts +12 -0
  71. package/src/internal/router-plugin/core/constants.js +13 -0
  72. package/src/internal/router-plugin/core/framework-plugin-order.d.ts +8 -0
  73. package/src/internal/router-plugin/core/framework-plugin-order.js +78 -0
  74. package/src/internal/router-plugin/core/hmr/handle-route-update.d.ts +1 -0
  75. package/src/internal/router-plugin/core/hmr/handle-route-update.js +126 -0
  76. package/src/internal/router-plugin/core/hmr/index.d.ts +5 -0
  77. package/src/internal/router-plugin/core/hmr/select-adapter.d.ts +17 -0
  78. package/src/internal/router-plugin/core/hmr/select-adapter.js +24 -0
  79. package/src/internal/router-plugin/core/hmr/vite-adapter.d.ts +18 -0
  80. package/src/internal/router-plugin/core/hmr/vite-adapter.js +73 -0
  81. package/src/internal/router-plugin/core/hmr/webpack-adapter.d.ts +22 -0
  82. package/src/internal/router-plugin/core/hmr/webpack-adapter.js +78 -0
  83. package/src/internal/router-plugin/core/router-code-splitter-plugin.d.ts +10 -0
  84. package/src/internal/router-plugin/core/router-code-splitter-plugin.js +244 -0
  85. package/src/internal/router-plugin/core/router-composed-plugin.d.ts +5 -0
  86. package/src/internal/router-plugin/core/router-composed-plugin.js +62 -0
  87. package/src/internal/router-plugin/core/router-generator-plugin.d.ts +10 -0
  88. package/src/internal/router-plugin/core/router-generator-plugin.js +125 -0
  89. package/src/internal/router-plugin/core/router-hmr-plugin.d.ts +13 -0
  90. package/src/internal/router-plugin/core/router-hmr-plugin.js +101 -0
  91. package/src/internal/router-plugin/core/router-plugin-context.d.ts +5 -0
  92. package/src/internal/router-plugin/core/router-plugin-context.js +6 -0
  93. package/src/internal/router-plugin/core/utils.d.ts +17 -0
  94. package/src/internal/router-plugin/core/utils.js +47 -0
  95. package/src/internal/router-plugin/esbuild.d.ts +197 -0
  96. package/src/internal/router-plugin/esbuild.js +58 -0
  97. package/src/internal/router-plugin/index.d.ts +19 -0
  98. package/src/internal/router-plugin/index.js +17 -0
  99. package/src/internal/router-plugin/rspack.d.ts +61 -0
  100. package/src/internal/router-plugin/rspack.js +92 -0
  101. package/src/internal/router-plugin/vite.d.ts +201 -0
  102. package/src/internal/router-plugin/vite.js +62 -0
  103. package/src/internal/router-plugin/webpack.d.ts +50 -0
  104. package/src/internal/router-plugin/webpack.js +77 -0
  105. package/src/internal/start-plugin-core/build-sitemap.d.ts +34 -0
  106. package/src/internal/start-plugin-core/build-sitemap.js +125 -0
  107. package/src/internal/start-plugin-core/config-context.d.ts +30 -0
  108. package/src/internal/start-plugin-core/config-context.js +89 -0
  109. package/src/internal/start-plugin-core/constants.d.ts +20 -0
  110. package/src/internal/start-plugin-core/constants.js +24 -0
  111. package/src/internal/start-plugin-core/debug.d.ts +1 -0
  112. package/src/internal/start-plugin-core/hydrate-when-transform.d.ts +5 -0
  113. package/src/internal/start-plugin-core/hydrate-when-transform.js +661 -0
  114. package/src/internal/start-plugin-core/hydration-constants.d.ts +1 -0
  115. package/src/internal/start-plugin-core/hydration-constants.js +4 -0
  116. package/src/internal/start-plugin-core/import-protection/adapterUtils.d.ts +40 -0
  117. package/src/internal/start-plugin-core/import-protection/adapterUtils.js +42 -0
  118. package/src/internal/start-plugin-core/import-protection/analysis.d.ts +63 -0
  119. package/src/internal/start-plugin-core/import-protection/analysis.js +533 -0
  120. package/src/internal/start-plugin-core/import-protection/constants.d.ts +11 -0
  121. package/src/internal/start-plugin-core/import-protection/constants.js +39 -0
  122. package/src/internal/start-plugin-core/import-protection/defaults.d.ts +19 -0
  123. package/src/internal/start-plugin-core/import-protection/defaults.js +34 -0
  124. package/src/internal/start-plugin-core/import-protection/extensionlessAbsoluteIdResolver.d.ts +22 -0
  125. package/src/internal/start-plugin-core/import-protection/extensionlessAbsoluteIdResolver.js +94 -0
  126. package/src/internal/start-plugin-core/import-protection/matchers.d.ts +18 -0
  127. package/src/internal/start-plugin-core/import-protection/matchers.js +39 -0
  128. package/src/internal/start-plugin-core/import-protection/rewrite.d.ts +29 -0
  129. package/src/internal/start-plugin-core/import-protection/rewrite.js +184 -0
  130. package/src/internal/start-plugin-core/import-protection/sourceLocation.d.ts +170 -0
  131. package/src/internal/start-plugin-core/import-protection/sourceLocation.js +376 -0
  132. package/src/internal/start-plugin-core/import-protection/trace.d.ts +89 -0
  133. package/src/internal/start-plugin-core/import-protection/trace.js +186 -0
  134. package/src/internal/start-plugin-core/import-protection/utils.d.ts +78 -0
  135. package/src/internal/start-plugin-core/import-protection/utils.js +171 -0
  136. package/src/internal/start-plugin-core/import-protection/virtualModules.d.ts +38 -0
  137. package/src/internal/start-plugin-core/import-protection/virtualModules.js +241 -0
  138. package/src/internal/start-plugin-core/index.d.ts +8 -0
  139. package/src/internal/start-plugin-core/index.js +2 -0
  140. package/src/internal/start-plugin-core/planning.d.ts +44 -0
  141. package/src/internal/start-plugin-core/planning.js +98 -0
  142. package/src/internal/start-plugin-core/post-build.d.ts +12 -0
  143. package/src/internal/start-plugin-core/post-build.js +40 -0
  144. package/src/internal/start-plugin-core/prerender.d.ts +17 -0
  145. package/src/internal/start-plugin-core/prerender.js +172 -0
  146. package/src/internal/start-plugin-core/queue.d.ts +37 -0
  147. package/src/internal/start-plugin-core/queue.js +118 -0
  148. package/src/internal/start-plugin-core/resolve-entries.d.ts +10 -0
  149. package/src/internal/start-plugin-core/resolve-entries.js +32 -0
  150. package/src/internal/start-plugin-core/schema.d.ts +1799 -0
  151. package/src/internal/start-plugin-core/schema.js +252 -0
  152. package/src/internal/start-plugin-core/serialization-adapters-module.d.ts +18 -0
  153. package/src/internal/start-plugin-core/serialization-adapters-module.js +37 -0
  154. package/src/internal/start-plugin-core/start-compiler/compiler.d.ts +181 -0
  155. package/src/internal/start-plugin-core/start-compiler/compiler.js +1078 -0
  156. package/src/internal/start-plugin-core/start-compiler/config.d.ts +20 -0
  157. package/src/internal/start-plugin-core/start-compiler/config.js +84 -0
  158. package/src/internal/start-plugin-core/start-compiler/handleClientOnlyJSX.d.ts +24 -0
  159. package/src/internal/start-plugin-core/start-compiler/handleClientOnlyJSX.js +25 -0
  160. package/src/internal/start-plugin-core/start-compiler/handleCreateIsomorphicFn.d.ts +11 -0
  161. package/src/internal/start-plugin-core/start-compiler/handleCreateIsomorphicFn.js +24 -0
  162. package/src/internal/start-plugin-core/start-compiler/handleCreateMiddleware.d.ts +11 -0
  163. package/src/internal/start-plugin-core/start-compiler/handleCreateMiddleware.js +35 -0
  164. package/src/internal/start-plugin-core/start-compiler/handleCreateServerFn.d.ts +22 -0
  165. package/src/internal/start-plugin-core/start-compiler/handleCreateServerFn.js +292 -0
  166. package/src/internal/start-plugin-core/start-compiler/handleEnvOnly.d.ts +14 -0
  167. package/src/internal/start-plugin-core/start-compiler/handleEnvOnly.js +41 -0
  168. package/src/internal/start-plugin-core/start-compiler/host.d.ts +44 -0
  169. package/src/internal/start-plugin-core/start-compiler/host.js +75 -0
  170. package/src/internal/start-plugin-core/start-compiler/server-fn-resolver-module.d.ts +10 -0
  171. package/src/internal/start-plugin-core/start-compiler/server-fn-resolver-module.js +87 -0
  172. package/src/internal/start-plugin-core/start-compiler/types.d.ts +119 -0
  173. package/src/internal/start-plugin-core/start-compiler/utils.d.ts +27 -0
  174. package/src/internal/start-plugin-core/start-compiler/utils.js +36 -0
  175. package/src/internal/start-plugin-core/start-manifest-plugin/inlineCss.d.ts +16 -0
  176. package/src/internal/start-plugin-core/start-manifest-plugin/inlineCss.js +131 -0
  177. package/src/internal/start-plugin-core/start-manifest-plugin/manifestBuilder.d.ts +64 -0
  178. package/src/internal/start-plugin-core/start-manifest-plugin/manifestBuilder.js +441 -0
  179. package/src/internal/start-plugin-core/start-router-plugin/constants.d.ts +1 -0
  180. package/src/internal/start-plugin-core/start-router-plugin/constants.js +4 -0
  181. package/src/internal/start-plugin-core/start-router-plugin/generator-plugins/compose-generator-plugins.d.ts +6 -0
  182. package/src/internal/start-plugin-core/start-router-plugin/generator-plugins/compose-generator-plugins.js +6 -0
  183. package/src/internal/start-plugin-core/start-router-plugin/generator-plugins/prerender-routes-plugin.d.ts +6 -0
  184. package/src/internal/start-plugin-core/start-router-plugin/generator-plugins/prerender-routes-plugin.js +27 -0
  185. package/src/internal/start-plugin-core/start-router-plugin/generator-plugins/routes-manifest-plugin.d.ts +6 -0
  186. package/src/internal/start-plugin-core/start-router-plugin/generator-plugins/routes-manifest-plugin.js +34 -0
  187. package/src/internal/start-plugin-core/start-router-plugin/pruneServerOnlySubtrees.d.ts +11 -0
  188. package/src/internal/start-plugin-core/start-router-plugin/pruneServerOnlySubtrees.js +42 -0
  189. package/src/internal/start-plugin-core/start-router-plugin/route-tree-footer.d.ts +6 -0
  190. package/src/internal/start-plugin-core/start-router-plugin/route-tree-footer.js +59 -0
  191. package/src/internal/start-plugin-core/types.d.ts +135 -0
  192. package/src/internal/start-plugin-core/types.js +4 -0
  193. package/src/internal/start-plugin-core/utils.d.ts +13 -0
  194. package/src/internal/start-plugin-core/utils.js +42 -0
  195. package/src/internal/start-plugin-core/vite/createVirtualModule.d.ts +15 -0
  196. package/src/internal/start-plugin-core/vite/createVirtualModule.js +27 -0
  197. package/src/internal/start-plugin-core/vite/dev-server-plugin/dev-styles.d.ts +26 -0
  198. package/src/internal/start-plugin-core/vite/dev-server-plugin/dev-styles.js +165 -0
  199. package/src/internal/start-plugin-core/vite/dev-server-plugin/plugin.d.ts +11 -0
  200. package/src/internal/start-plugin-core/vite/dev-server-plugin/plugin.js +170 -0
  201. package/src/internal/start-plugin-core/vite/import-protection-plugin/plugin.d.ts +4 -0
  202. package/src/internal/start-plugin-core/vite/import-protection-plugin/plugin.js +1553 -0
  203. package/src/internal/start-plugin-core/vite/import-protection-plugin/types.d.ts +103 -0
  204. package/src/internal/start-plugin-core/vite/import-protection-plugin/virtualModules.d.ts +40 -0
  205. package/src/internal/start-plugin-core/vite/import-protection-plugin/virtualModules.js +68 -0
  206. package/src/internal/start-plugin-core/vite/index.d.ts +13 -0
  207. package/src/internal/start-plugin-core/vite/index.js +4 -0
  208. package/src/internal/start-plugin-core/vite/load-env-plugin/plugin.d.ts +2 -0
  209. package/src/internal/start-plugin-core/vite/load-env-plugin/plugin.js +13 -0
  210. package/src/internal/start-plugin-core/vite/output-directory.d.ts +7 -0
  211. package/src/internal/start-plugin-core/vite/output-directory.js +18 -0
  212. package/src/internal/start-plugin-core/vite/planning.d.ts +112 -0
  213. package/src/internal/start-plugin-core/vite/planning.js +152 -0
  214. package/src/internal/start-plugin-core/vite/plugin.d.ts +7 -0
  215. package/src/internal/start-plugin-core/vite/plugin.js +208 -0
  216. package/src/internal/start-plugin-core/vite/plugins.d.ts +17 -0
  217. package/src/internal/start-plugin-core/vite/plugins.js +72 -0
  218. package/src/internal/start-plugin-core/vite/post-server-build.d.ts +9 -0
  219. package/src/internal/start-plugin-core/vite/post-server-build.js +22 -0
  220. package/src/internal/start-plugin-core/vite/prerender.d.ts +9 -0
  221. package/src/internal/start-plugin-core/vite/prerender.js +52 -0
  222. package/src/internal/start-plugin-core/vite/preview-server-plugin/plugin.d.ts +2 -0
  223. package/src/internal/start-plugin-core/vite/preview-server-plugin/plugin.js +54 -0
  224. package/src/internal/start-plugin-core/vite/schema.d.ts +2423 -0
  225. package/src/internal/start-plugin-core/vite/schema.js +17 -0
  226. package/src/internal/start-plugin-core/vite/serialization-adapters-plugin.d.ts +5 -0
  227. package/src/internal/start-plugin-core/vite/serialization-adapters-plugin.js +63 -0
  228. package/src/internal/start-plugin-core/vite/start-compiler-plugin/hot-update.d.ts +5 -0
  229. package/src/internal/start-plugin-core/vite/start-compiler-plugin/hot-update.js +14 -0
  230. package/src/internal/start-plugin-core/vite/start-compiler-plugin/module-specifier.d.ts +5 -0
  231. package/src/internal/start-plugin-core/vite/start-compiler-plugin/module-specifier.js +25 -0
  232. package/src/internal/start-plugin-core/vite/start-compiler-plugin/plugin.d.ts +29 -0
  233. package/src/internal/start-plugin-core/vite/start-compiler-plugin/plugin.js +380 -0
  234. package/src/internal/start-plugin-core/vite/start-manifest-plugin/normalized-client-build.d.ts +11 -0
  235. package/src/internal/start-plugin-core/vite/start-manifest-plugin/normalized-client-build.js +114 -0
  236. package/src/internal/start-plugin-core/vite/start-manifest-plugin/plugin.d.ts +3 -0
  237. package/src/internal/start-plugin-core/vite/start-manifest-plugin/plugin.js +103 -0
  238. package/src/internal/start-plugin-core/vite/start-router-plugin/plugin.d.ts +9 -0
  239. package/src/internal/start-plugin-core/vite/start-router-plugin/plugin.js +143 -0
  240. package/src/internal/start-plugin-core/vite/types.d.ts +17 -0
  241. package/src/package-names.js +10 -0
  242. package/src/plugin-vite.d.ts +23 -0
  243. package/src/plugin-vite.js +62 -0
  244. package/src/runtime/client/StartClient.d.ts +11 -0
  245. package/src/runtime/client/StartClient.js +16 -0
  246. package/src/runtime/client/hydrateStart.d.ts +4 -0
  247. package/src/runtime/client/hydrateStart.js +30 -0
  248. package/src/runtime/server/StartServer.d.ts +10 -0
  249. package/src/runtime/server/StartServer.js +6 -0
  250. package/src/runtime/server/defaultRenderHandler.d.ts +3 -0
  251. package/src/runtime/server/defaultRenderHandler.js +11 -0
  252. package/src/runtime/server/defaultStreamHandler.d.ts +3 -0
  253. package/src/runtime/server/defaultStreamHandler.js +13 -0
  254. package/src/server-entry.d.ts +11 -0
  255. package/src/server-entry.js +13 -0
  256. package/src/server-only.d.ts +1 -0
  257. package/src/server-only.js +0 -0
  258. package/src/server-rpc.d.ts +1 -0
  259. package/src/server-rpc.js +1 -0
  260. package/src/server.d.ts +6 -0
  261. package/src/server.js +4 -0
  262. package/src/ssr-rpc.d.ts +1 -0
  263. package/src/ssr-rpc.js +1 -0
  264. package/src/use-server-fn.d.ts +3 -0
  265. package/src/use-server-fn.js +18 -0
  266. package/src/validate-options.js +7 -0
@@ -0,0 +1,40 @@
1
+ import { buildSitemap } from './build-sitemap.js';
2
+ import { HEADERS } from '@tanstack/start-server-core';
3
+ //#region src/post-build.ts
4
+ async function postBuild({ startConfig, adapter }) {
5
+ if (startConfig.prerender?.enabled !== false)
6
+ startConfig.prerender = {
7
+ ...startConfig.prerender,
8
+ enabled:
9
+ startConfig.prerender?.enabled ??
10
+ startConfig.pages.some((d) => (typeof d === 'string' ? false : !!d.prerender?.enabled)),
11
+ };
12
+ if (startConfig.spa?.enabled) {
13
+ startConfig.prerender = {
14
+ ...startConfig.prerender,
15
+ enabled: true,
16
+ };
17
+ const maskUrl = new URL(startConfig.spa.maskPath, 'http://localhost');
18
+ if (maskUrl.origin !== 'http://localhost')
19
+ throw new Error('spa.maskPath must be a path (no protocol/host)');
20
+ startConfig.pages.push({
21
+ path: maskUrl.toString().replace('http://localhost', ''),
22
+ prerender: {
23
+ ...startConfig.spa.prerender,
24
+ headers: {
25
+ ...startConfig.spa.prerender.headers,
26
+ [HEADERS.TSS_SHELL]: 'true',
27
+ },
28
+ },
29
+ sitemap: { exclude: true },
30
+ });
31
+ }
32
+ if (startConfig.prerender.enabled) await adapter.prerender(startConfig);
33
+ if (startConfig.sitemap?.enabled)
34
+ buildSitemap({
35
+ startConfig,
36
+ publicDir: adapter.getClientOutputDirectory(),
37
+ });
38
+ }
39
+ //#endregion
40
+ export { postBuild };
@@ -0,0 +1,17 @@
1
+ import { Page, TanStackStartOutputConfig } from './schema.js';
2
+ export interface PrerenderHandler {
3
+ getClientOutputDirectory: () => string;
4
+ request: (path: string, options?: RequestInit) => Promise<Response>;
5
+ close?: () => Promise<void>;
6
+ }
7
+ export declare function prerender({
8
+ startConfig,
9
+ handler,
10
+ }: {
11
+ startConfig: TanStackStartOutputConfig;
12
+ handler: PrerenderHandler;
13
+ }): Promise<void>;
14
+ export declare function validateAndNormalizePrerenderPages(
15
+ pages: Array<Page>,
16
+ routerBaseUrl: URL,
17
+ ): Array<Page>;
@@ -0,0 +1,172 @@
1
+ import { createLogger } from './utils.js';
2
+ import { Queue } from './queue.js';
3
+ import path from 'pathe';
4
+ import { promises } from 'node:fs';
5
+ import { joinURL, withBase, withTrailingSlash, withoutBase } from 'ufo';
6
+ import os from 'node:os';
7
+ //#region src/prerender.ts
8
+ var DEFAULT_RETRY_DELAY = 500;
9
+ async function prerender({ startConfig, handler }) {
10
+ const logger = createLogger('prerender');
11
+ logger.info('Prerendering pages...');
12
+ if (startConfig.prerender?.enabled) {
13
+ let pages = startConfig.pages.length ? startConfig.pages : [{ path: '/' }];
14
+ if (startConfig.prerender.autoStaticPathsDiscovery ?? true) {
15
+ const pagesMap = new Map(pages.map((item) => [item.path, item]));
16
+ const discoveredPages = globalThis.TSS_PRERENDABLE_PATHS || [];
17
+ for (const page of discoveredPages)
18
+ if (!pagesMap.has(page.path)) pagesMap.set(page.path, page);
19
+ pages = Array.from(pagesMap.values());
20
+ }
21
+ startConfig.pages = pages;
22
+ }
23
+ const routerBasePath = joinURL('/', startConfig.router.basepath ?? '');
24
+ const routerBaseUrl = new URL(routerBasePath, 'http://localhost');
25
+ startConfig.pages = validateAndNormalizePrerenderPages(startConfig.pages, routerBaseUrl);
26
+ try {
27
+ const pages = await prerenderPages({ outputDir: handler.getClientOutputDirectory() });
28
+ logger.info(`Prerendered ${pages.length} pages:`);
29
+ pages.forEach((page) => {
30
+ logger.info(`- ${page}`);
31
+ });
32
+ } catch (error) {
33
+ logger.error(error);
34
+ throw error;
35
+ } finally {
36
+ await handler.close?.();
37
+ }
38
+ function extractLinks(html) {
39
+ const linkRegex = /<a[^>]+href=["']([^"']+)["'][^>]*>/g;
40
+ const links = [];
41
+ let match;
42
+ while ((match = linkRegex.exec(html)) !== null) {
43
+ const href = match[1];
44
+ if (href && (href.startsWith('/') || href.startsWith('./'))) links.push(href);
45
+ }
46
+ return links;
47
+ }
48
+ async function prerenderPages({ outputDir }) {
49
+ const seen = /* @__PURE__ */ new Set();
50
+ const prerendered = /* @__PURE__ */ new Set();
51
+ const retriesByPath = /* @__PURE__ */ new Map();
52
+ const concurrency = startConfig.prerender?.concurrency ?? os.cpus().length;
53
+ logger.info(`Concurrency: ${concurrency}`);
54
+ const queue = new Queue({ concurrency });
55
+ const routerBasePath = joinURL('/', startConfig.router.basepath ?? '');
56
+ const routerBaseUrl = new URL(routerBasePath, 'http://localhost');
57
+ startConfig.pages = validateAndNormalizePrerenderPages(startConfig.pages, routerBaseUrl);
58
+ startConfig.pages.forEach((page) => addCrawlPageTask(page));
59
+ if (queue.isSettled()) {
60
+ logger.info('No pages matched prerender filter; skipping.');
61
+ return Array.from(prerendered);
62
+ }
63
+ await queue.start();
64
+ return Array.from(prerendered);
65
+ function addCrawlPageTask(page) {
66
+ if (seen.has(page.path)) return;
67
+ seen.add(page.path);
68
+ if (page.fromCrawl) startConfig.pages.push(page);
69
+ if (!(page.prerender?.enabled ?? true)) return;
70
+ if (startConfig.prerender?.filter && !startConfig.prerender.filter(page)) return;
71
+ const prerenderOptions = {
72
+ ...startConfig.prerender,
73
+ ...page.prerender,
74
+ };
75
+ queue.add(async () => {
76
+ logger.info(`Crawling: ${page.path}`);
77
+ const retries = retriesByPath.get(page.path) || 0;
78
+ try {
79
+ const res = await requestWithRedirects(
80
+ withTrailingSlash(withBase(page.path, routerBasePath)),
81
+ { headers: { ...(prerenderOptions.headers ?? {}) } },
82
+ prerenderOptions.maxRedirects,
83
+ );
84
+ if (!res.ok) {
85
+ if (isRedirectResponse(res)) logger.warn(`Max redirects reached for ${page.path}`);
86
+ throw new Error(`Failed to fetch ${page.path}: ${res.statusText}`, { cause: res });
87
+ }
88
+ const cleanPagePath = (prerenderOptions.outputPath || page.path).split(/[?#]/)[0];
89
+ const contentType = res.headers.get('content-type') || '';
90
+ const isImplicitHTML = !cleanPagePath.endsWith('.html') && contentType.includes('html');
91
+ const routeWithIndex = cleanPagePath.endsWith('/')
92
+ ? cleanPagePath + 'index'
93
+ : cleanPagePath;
94
+ const isSpaShell = startConfig.spa?.prerender.outputPath === cleanPagePath;
95
+ let htmlPath;
96
+ if (isSpaShell) htmlPath = cleanPagePath + '.html';
97
+ else if (cleanPagePath.endsWith('/') || (prerenderOptions.autoSubfolderIndex ?? true))
98
+ htmlPath = joinURL(cleanPagePath, 'index.html');
99
+ else htmlPath = cleanPagePath + '.html';
100
+ const filename = withoutBase(isImplicitHTML ? htmlPath : routeWithIndex, routerBasePath);
101
+ const html = await res.text();
102
+ const filepath = path.join(outputDir, filename);
103
+ await promises.mkdir(path.dirname(filepath), { recursive: true });
104
+ await promises.writeFile(filepath, html);
105
+ prerendered.add(page.path);
106
+ const newPage = await prerenderOptions.onSuccess?.({
107
+ page,
108
+ html,
109
+ });
110
+ if (newPage) Object.assign(page, newPage);
111
+ if (prerenderOptions.crawlLinks ?? true) {
112
+ const links = extractLinks(html);
113
+ for (const link of links)
114
+ addCrawlPageTask({
115
+ path: link,
116
+ fromCrawl: true,
117
+ });
118
+ }
119
+ } catch (error) {
120
+ if (retries < (prerenderOptions.retryCount ?? 0)) {
121
+ const retryDelay = normalizeRetryDelay(prerenderOptions.retryDelay);
122
+ logger.warn(`Encountered error, retrying: ${page.path} in ${retryDelay}ms`);
123
+ await new Promise((resolve) => setTimeout(resolve, retryDelay));
124
+ retriesByPath.set(page.path, retries + 1);
125
+ addCrawlPageTask(page);
126
+ } else if (prerenderOptions.failOnError ?? true) throw error;
127
+ }
128
+ });
129
+ }
130
+ }
131
+ function normalizeRetryDelay(value) {
132
+ const retryDelay = Number(value);
133
+ if (!Number.isFinite(retryDelay) || retryDelay < 0) return DEFAULT_RETRY_DELAY;
134
+ return Math.trunc(retryDelay);
135
+ }
136
+ async function requestWithRedirects(path, options, maxRedirects = 5) {
137
+ const response = await handler.request(path, options);
138
+ if (isRedirectResponse(response) && maxRedirects > 0) {
139
+ const location = response.headers.get('location');
140
+ if (location.startsWith('http://localhost') || location.startsWith('/'))
141
+ return requestWithRedirects(
142
+ location.replace('http://localhost', ''),
143
+ options,
144
+ maxRedirects - 1,
145
+ );
146
+ logger.warn(`Skipping redirect to external location: ${location}`);
147
+ }
148
+ return response;
149
+ }
150
+ }
151
+ function isRedirectResponse(res) {
152
+ return res.status >= 300 && res.status < 400 && res.headers.get('location');
153
+ }
154
+ function validateAndNormalizePrerenderPages(pages, routerBaseUrl) {
155
+ return pages.map((page) => {
156
+ let url;
157
+ try {
158
+ url = new URL(page.path, routerBaseUrl);
159
+ } catch (err) {
160
+ throw new Error(`prerender page path must be relative: ${page.path}`, { cause: err });
161
+ }
162
+ if (url.origin !== 'http://localhost')
163
+ throw new Error(`prerender page path must be relative: ${page.path}`);
164
+ const decodedPathname = decodeURIComponent(url.pathname);
165
+ return {
166
+ ...page,
167
+ path: decodedPathname + url.search + url.hash,
168
+ };
169
+ });
170
+ }
171
+ //#endregion
172
+ export { prerender };
@@ -0,0 +1,37 @@
1
+ interface PoolConfig {
2
+ concurrency?: number;
3
+ started?: boolean;
4
+ tasks?: Array<() => Promise<any>>;
5
+ }
6
+ export declare class Queue<T> {
7
+ private onSettles;
8
+ private onErrors;
9
+ private onSuccesses;
10
+ private running;
11
+ private active;
12
+ private pending;
13
+ private currentConcurrency;
14
+ constructor(config?: PoolConfig);
15
+ private tick;
16
+ add(
17
+ fn: () => Promise<T> | T,
18
+ {
19
+ priority,
20
+ }?: {
21
+ priority?: boolean;
22
+ },
23
+ ): Promise<any>;
24
+ throttle(n: number): void;
25
+ onSettled(cb: () => void): () => void;
26
+ onError(cb: (error: any, task: () => Promise<any>) => void): () => void;
27
+ onSuccess(cb: (result: any, task: () => Promise<any>) => void): () => void;
28
+ stop(): void;
29
+ start(): Promise<void>;
30
+ clear(): void;
31
+ getActive(): (() => Promise<any>)[];
32
+ getPending(): (() => Promise<any>)[];
33
+ getAll(): (() => Promise<any>)[];
34
+ isRunning(): boolean;
35
+ isSettled(): boolean;
36
+ }
37
+ export {};
@@ -0,0 +1,118 @@
1
+ //#region src/queue.ts
2
+ var defaultConfig = {
3
+ concurrency: 5,
4
+ started: false,
5
+ tasks: [],
6
+ };
7
+ var Queue = class {
8
+ onSettles = [];
9
+ onErrors = [];
10
+ onSuccesses = [];
11
+ running;
12
+ active = [];
13
+ pending;
14
+ currentConcurrency;
15
+ constructor(config = defaultConfig) {
16
+ const { concurrency, started, tasks } = {
17
+ ...defaultConfig,
18
+ ...config,
19
+ };
20
+ this.running = started;
21
+ this.pending = tasks;
22
+ this.currentConcurrency = concurrency;
23
+ }
24
+ tick() {
25
+ if (!this.running) return;
26
+ while (this.active.length < this.currentConcurrency && this.pending.length) {
27
+ const nextFn = this.pending.shift();
28
+ if (!nextFn) throw new Error('Found task that is not a function');
29
+ this.active.push(nextFn);
30
+ (async () => {
31
+ let success = false;
32
+ let res;
33
+ let error;
34
+ try {
35
+ res = await nextFn();
36
+ success = true;
37
+ } catch (e) {
38
+ error = e;
39
+ }
40
+ this.active = this.active.filter((d) => d !== nextFn);
41
+ if (success) this.onSuccesses.forEach((d) => d(res, nextFn));
42
+ else this.onErrors.forEach((d) => d(error, nextFn));
43
+ this.onSettles.forEach((d) => d(res, error));
44
+ this.tick();
45
+ })();
46
+ }
47
+ }
48
+ add(fn, { priority } = {}) {
49
+ return new Promise((resolve, reject) => {
50
+ const task = () =>
51
+ Promise.resolve(fn())
52
+ .then((res) => {
53
+ resolve(res);
54
+ return res;
55
+ })
56
+ .catch((err) => {
57
+ reject(err);
58
+ throw err;
59
+ });
60
+ if (priority) this.pending.unshift(task);
61
+ else this.pending.push(task);
62
+ this.tick();
63
+ });
64
+ }
65
+ throttle(n) {
66
+ this.currentConcurrency = n;
67
+ }
68
+ onSettled(cb) {
69
+ this.onSettles.push(cb);
70
+ return () => {
71
+ this.onSettles = this.onSettles.filter((d) => d !== cb);
72
+ };
73
+ }
74
+ onError(cb) {
75
+ this.onErrors.push(cb);
76
+ return () => {
77
+ this.onErrors = this.onErrors.filter((d) => d !== cb);
78
+ };
79
+ }
80
+ onSuccess(cb) {
81
+ this.onSuccesses.push(cb);
82
+ return () => {
83
+ this.onSuccesses = this.onSuccesses.filter((d) => d !== cb);
84
+ };
85
+ }
86
+ stop() {
87
+ this.running = false;
88
+ }
89
+ start() {
90
+ this.running = true;
91
+ this.tick();
92
+ return new Promise((resolve) => {
93
+ this.onSettled(() => {
94
+ if (this.isSettled()) resolve();
95
+ });
96
+ });
97
+ }
98
+ clear() {
99
+ this.pending = [];
100
+ }
101
+ getActive() {
102
+ return this.active;
103
+ }
104
+ getPending() {
105
+ return this.pending;
106
+ }
107
+ getAll() {
108
+ return [...this.active, ...this.pending];
109
+ }
110
+ isRunning() {
111
+ return this.running;
112
+ }
113
+ isSettled() {
114
+ return !this.active.length && !this.pending.length;
115
+ }
116
+ };
117
+ //#endregion
118
+ export { Queue };
@@ -0,0 +1,10 @@
1
+ export declare function resolveEntry<
2
+ TRequired extends boolean,
3
+ TReturn = TRequired extends true ? string : string | undefined,
4
+ >(opts: {
5
+ type: string;
6
+ configuredEntry?: string;
7
+ defaultEntry: string;
8
+ resolvedSrcDirectory: string;
9
+ required: TRequired;
10
+ }): TReturn;
@@ -0,0 +1,32 @@
1
+ import { resolveModulePath } from 'exsolve';
2
+ //#region src/resolve-entries.ts
3
+ function resolveModule(opts) {
4
+ let baseName = opts.baseName;
5
+ if (!baseName.startsWith('./')) baseName = `./${baseName}`;
6
+ return resolveModulePath(baseName, {
7
+ from: opts.from,
8
+ extensions: ['.ts', '.js', '.mts', '.mjs', '.tsx', '.jsx', '.tsrx'],
9
+ try: true,
10
+ });
11
+ }
12
+ function resolveEntry(opts) {
13
+ let resolveOptions;
14
+ if (!opts.configuredEntry)
15
+ resolveOptions = {
16
+ baseName: opts.defaultEntry,
17
+ from: opts.resolvedSrcDirectory,
18
+ };
19
+ else
20
+ resolveOptions = {
21
+ baseName: opts.configuredEntry,
22
+ from: opts.resolvedSrcDirectory,
23
+ };
24
+ const resolvedEntry = resolveModule(resolveOptions);
25
+ if (opts.required && !resolvedEntry)
26
+ throw new Error(
27
+ `Could not resolve entry for ${opts.type}: ${resolveOptions.baseName} in ${resolveOptions.from}`,
28
+ );
29
+ return resolvedEntry;
30
+ }
31
+ //#endregion
32
+ export { resolveEntry };