@tanstack/start-plugin-core 1.120.4-alpha.2 → 1.120.4-alpha.21

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 (42) hide show
  1. package/dist/cjs/constants.cjs +10 -0
  2. package/dist/cjs/constants.cjs.map +1 -0
  3. package/dist/cjs/constants.d.cts +4 -0
  4. package/dist/cjs/nitro/dev-server-plugin.cjs +11 -3
  5. package/dist/cjs/nitro/dev-server-plugin.cjs.map +1 -1
  6. package/dist/cjs/nitro/nitro-plugin.cjs +24 -5
  7. package/dist/cjs/nitro/nitro-plugin.cjs.map +1 -1
  8. package/dist/cjs/plugin.cjs +49 -15
  9. package/dist/cjs/plugin.cjs.map +1 -1
  10. package/dist/cjs/plugin.d.cts +620 -1
  11. package/dist/cjs/prerender.cjs +19 -15
  12. package/dist/cjs/prerender.cjs.map +1 -1
  13. package/dist/cjs/schema.cjs +21 -2
  14. package/dist/cjs/schema.cjs.map +1 -1
  15. package/dist/cjs/schema.d.cts +2281 -520
  16. package/dist/cjs/start-server-routes-plugin/plugin.cjs +6 -2
  17. package/dist/cjs/start-server-routes-plugin/plugin.cjs.map +1 -1
  18. package/dist/esm/constants.d.ts +4 -0
  19. package/dist/esm/constants.js +10 -0
  20. package/dist/esm/constants.js.map +1 -0
  21. package/dist/esm/nitro/dev-server-plugin.js +11 -3
  22. package/dist/esm/nitro/dev-server-plugin.js.map +1 -1
  23. package/dist/esm/nitro/nitro-plugin.js +24 -5
  24. package/dist/esm/nitro/nitro-plugin.js.map +1 -1
  25. package/dist/esm/plugin.d.ts +620 -1
  26. package/dist/esm/plugin.js +49 -15
  27. package/dist/esm/plugin.js.map +1 -1
  28. package/dist/esm/prerender.js +19 -15
  29. package/dist/esm/prerender.js.map +1 -1
  30. package/dist/esm/schema.d.ts +2281 -520
  31. package/dist/esm/schema.js +21 -2
  32. package/dist/esm/schema.js.map +1 -1
  33. package/dist/esm/start-server-routes-plugin/plugin.js +6 -2
  34. package/dist/esm/start-server-routes-plugin/plugin.js.map +1 -1
  35. package/package.json +11 -5
  36. package/src/constants.ts +6 -0
  37. package/src/nitro/dev-server-plugin.ts +14 -4
  38. package/src/nitro/nitro-plugin.ts +28 -3
  39. package/src/plugin.ts +64 -15
  40. package/src/prerender.ts +26 -19
  41. package/src/schema.ts +26 -1
  42. package/src/start-server-routes-plugin/plugin.ts +10 -2
package/src/prerender.ts CHANGED
@@ -6,6 +6,7 @@ import { build as buildNitro, createNitro } from 'nitropack'
6
6
  import { joinURL, withBase, withoutBase } from 'ufo'
7
7
  import { Queue } from './queue'
8
8
  import { buildNitroEnvironment } from './nitro/build-nitro'
9
+ import { VITE_ENVIRONMENT_NAMES } from './constants'
9
10
  import type { ViteBuilder } from 'vite'
10
11
  import type { $Fetch, Nitro } from 'nitropack'
11
12
  import type { TanStackStartOutputConfig } from './plugin'
@@ -20,7 +21,7 @@ export async function prerender({
20
21
  nitro: Nitro
21
22
  builder: ViteBuilder
22
23
  }) {
23
- nitro.logger.info('Prendering pages...')
24
+ console.info('Prendering pages...')
24
25
 
25
26
  // If prerender is enabled but no pages are provided, default to prerendering the root page
26
27
  if (options.prerender?.enabled && !options.pages.length) {
@@ -31,10 +32,12 @@ export async function prerender({
31
32
  ]
32
33
  }
33
34
 
34
- const serverEnv = builder.environments['server']
35
+ const serverEnv = builder.environments[VITE_ENVIRONMENT_NAMES.server]
35
36
 
36
37
  if (!serverEnv) {
37
- throw new Error(`Vite's "server" environment not found`)
38
+ throw new Error(
39
+ `Vite's "${VITE_ENVIRONMENT_NAMES.server}" environment not found`,
40
+ )
38
41
  }
39
42
 
40
43
  const prerenderOutputDir = path.resolve(
@@ -90,14 +93,14 @@ export async function prerender({
90
93
  // Crawl all pages
91
94
  const pages = await prerenderPages()
92
95
 
93
- nitro.logger.info(`Prerendered ${pages.length} pages:`)
96
+ console.info(`Prerendered ${pages.length} pages:`)
94
97
  pages.forEach((page) => {
95
- nitro.logger.info(`- ${page}`)
98
+ console.info(`- ${page}`)
96
99
  })
97
100
 
98
101
  // TODO: Write the prerendered pages to the output directory
99
102
  } catch (error) {
100
- nitro.logger.error(error)
103
+ console.error(error)
101
104
  } finally {
102
105
  // Ensure server is always closed
103
106
  // server.process.kill()
@@ -123,7 +126,7 @@ export async function prerender({
123
126
  const seen = new Set<string>()
124
127
  const retriesByPath = new Map<string, number>()
125
128
  const concurrency = options.prerender?.concurrency ?? os.cpus().length
126
- nitro.logger.info(`Concurrency: ${concurrency}`)
129
+ console.info(`Concurrency: ${concurrency}`)
127
130
  const queue = new Queue({ concurrency })
128
131
 
129
132
  options.pages.forEach((_page) => {
@@ -165,7 +168,7 @@ export async function prerender({
165
168
 
166
169
  // Add the task
167
170
  queue.add(async () => {
168
- nitro.logger.info(`Crawling: ${page.path}`)
171
+ console.info(`Crawling: ${page.path}`)
169
172
  const retries = retriesByPath.get(page.path) || 0
170
173
  try {
171
174
  // Fetch the route
@@ -179,23 +182,29 @@ export async function prerender({
179
182
  )
180
183
 
181
184
  if (!res.ok) {
182
- throw new Error(`Failed to fetch ${page.path}: ${res.statusText}`)
185
+ throw new Error(`Failed to fetch ${page.path}: ${res.statusText}`, {
186
+ cause: res,
187
+ })
183
188
  }
184
189
 
190
+ const cleanPagePath = (
191
+ prerenderOptions.outputPath || page.path
192
+ ).split(/[?#]/)[0]!
193
+
185
194
  // Guess route type and populate fileName
186
195
  const contentType = res.headers.get('content-type') || ''
187
196
  const isImplicitHTML =
188
- !page.path.endsWith('.html') && contentType.includes('html')
197
+ !cleanPagePath.endsWith('.html') && contentType.includes('html')
189
198
  // &&
190
199
  // !JsonSigRx.test(dataBuff.subarray(0, 32).toString('utf8'))
191
- const routeWithIndex = page.path.endsWith('/')
192
- ? page.path + 'index'
193
- : page.path
200
+ const routeWithIndex = cleanPagePath.endsWith('/')
201
+ ? cleanPagePath + 'index'
202
+ : cleanPagePath
194
203
 
195
204
  const htmlPath =
196
- page.path.endsWith('/') || prerenderOptions.autoSubfolderIndex
197
- ? joinURL(page.path, 'index.html')
198
- : page.path + '.html'
205
+ cleanPagePath.endsWith('/') || prerenderOptions.autoSubfolderIndex
206
+ ? joinURL(cleanPagePath, 'index.html')
207
+ : cleanPagePath + '.html'
199
208
 
200
209
  const filename = withoutBase(
201
210
  isImplicitHTML ? htmlPath : routeWithIndex,
@@ -227,9 +236,7 @@ export async function prerender({
227
236
  }
228
237
  } catch (error) {
229
238
  if (retries < (prerenderOptions.retryCount ?? 0)) {
230
- nitro.logger.warn(
231
- `Encountered error, retrying: ${page.path} in 500ms`,
232
- )
239
+ console.warn(`Encountered error, retrying: ${page.path} in 500ms`)
233
240
  await new Promise((resolve) =>
234
241
  setTimeout(resolve, prerenderOptions.retryDelay),
235
242
  )
package/src/schema.ts CHANGED
@@ -48,6 +48,14 @@ export function createTanStackConfig<
48
48
  return path.join(srcDirectory, 'server.tsx')
49
49
  }
50
50
 
51
+ if (existsSync(path.join(srcDirectory, 'server.ts'))) {
52
+ return path.join(srcDirectory, 'server.ts')
53
+ }
54
+
55
+ if (existsSync(path.join(srcDirectory, 'server.js'))) {
56
+ return path.join(srcDirectory, 'server.js')
57
+ }
58
+
51
59
  return '/~start/default-server-entry'
52
60
  })()
53
61
 
@@ -92,7 +100,7 @@ export function createTanStackStartOptionsSchema(
92
100
  .default({}),
93
101
  serverFns: z
94
102
  .object({
95
- base: z.string().optional().default('/_server'),
103
+ base: z.string().optional().default('/_serverFn'),
96
104
  })
97
105
  .optional()
98
106
  .default({}),
@@ -121,6 +129,7 @@ export function createTanStackStartOptionsSchema(
121
129
  })
122
130
  .and(pagePrerenderOptionsSchema.optional())
123
131
  .optional(),
132
+ spa: spaSchema.optional(),
124
133
  })
125
134
  .optional()
126
135
  .default({})
@@ -170,6 +179,7 @@ const pageBaseSchema = z.object({
170
179
 
171
180
  const pagePrerenderOptionsSchema = z.object({
172
181
  enabled: z.boolean().optional(),
182
+ outputPath: z.string().optional(),
173
183
  autoSubfolderIndex: z.boolean().optional(),
174
184
  crawlLinks: z.boolean().optional(),
175
185
  retryCount: z.number().optional(),
@@ -186,6 +196,21 @@ const pagePrerenderOptionsSchema = z.object({
186
196
  .optional(),
187
197
  })
188
198
 
199
+ const spaSchema = z.object({
200
+ enabled: z.boolean().optional().default(true),
201
+ maskPath: z.string().optional().default('/'),
202
+ prerender: pagePrerenderOptionsSchema
203
+ .optional()
204
+ .default({})
205
+ .transform((opts) => ({
206
+ outputPath: opts.outputPath ?? '/_shell',
207
+ crawlLinks: false,
208
+ retryCount: 0,
209
+ ...opts,
210
+ enabled: true,
211
+ })),
212
+ })
213
+
189
214
  export const pageSchema = pageBaseSchema.extend({
190
215
  prerender: pagePrerenderOptionsSchema.optional(),
191
216
  })
@@ -244,7 +244,9 @@ async function generator(config: Config, root: string) {
244
244
  removeUnderscores(removeLayoutSegments(node.path)) ?? '',
245
245
  )
246
246
 
247
- const routeCode = fs.readFileSync(node.fullPath, 'utf-8')
247
+ const routeCode = node.fullPath
248
+ ? fs.readFileSync(node.fullPath, 'utf-8')
249
+ : ''
248
250
 
249
251
  // Ensure the boilerplate for the route exists, which can be skipped for virtual parent routes and virtual routes
250
252
  if (!node.isVirtualParentRoute && !node.isVirtual) {
@@ -682,7 +684,13 @@ function buildStartDeclarationFile({
682
684
  }: {
683
685
  serverRoutesRelativePath: string
684
686
  }) {
685
- return [`import '${serverRoutesRelativePath}'`].join('\n') + '\n'
687
+ const serverRoutesPath = replaceBackslash(serverRoutesRelativePath)
688
+ return (
689
+ [
690
+ '/// <reference types="vite/client" />',
691
+ `import '${serverRoutesPath}'`,
692
+ ].join('\n') + '\n'
693
+ )
686
694
  }
687
695
 
688
696
  function removeGroups(s: string) {