@tanstack/router-plugin 1.159.10 → 1.159.12

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.
@@ -1,4 +1,5 @@
1
1
  export const tsrSplit = 'tsr-split'
2
+ export const tsrShared = 'tsr-shared'
2
3
 
3
4
  export const splitRouteIdentNodes = [
4
5
  'loader',
@@ -8,12 +8,15 @@ import { logDiff } from '@tanstack/router-utils'
8
8
  import { getConfig, splitGroupingsSchema } from './config'
9
9
  import {
10
10
  compileCodeSplitReferenceRoute,
11
+ compileCodeSplitSharedRoute,
11
12
  compileCodeSplitVirtualRoute,
13
+ computeSharedBindings,
12
14
  detectCodeSplitGroupingsFromRoute,
13
15
  } from './code-splitter/compilers'
14
16
  import {
15
17
  defaultCodeSplitGroupings,
16
18
  splitRouteIdentNodes,
19
+ tsrShared,
17
20
  tsrSplit,
18
21
  } from './constants'
19
22
  import { decodeIdentifier } from './code-splitter/path-ids'
@@ -88,6 +91,10 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
88
91
  }
89
92
  const isProduction = process.env.NODE_ENV === 'production'
90
93
 
94
+ // Map from normalized route file path → set of shared binding names.
95
+ // Populated by the reference compiler, consumed by virtual and shared compilers.
96
+ const sharedBindingsMap = new Map<string, Set<string>>()
97
+
91
98
  const getGlobalCodeSplitGroupings = () => {
92
99
  return (
93
100
  userConfig.codeSplittingOptions?.defaultBehavior ||
@@ -138,6 +145,17 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
138
145
  const splitGroupings: CodeSplitGroupings =
139
146
  fromCode.groupings || pluginSplitBehavior || getGlobalCodeSplitGroupings()
140
147
 
148
+ // Compute shared bindings before compiling the reference route
149
+ const sharedBindings = computeSharedBindings({
150
+ code,
151
+ codeSplitGroupings: splitGroupings,
152
+ })
153
+ if (sharedBindings.size > 0) {
154
+ sharedBindingsMap.set(id, sharedBindings)
155
+ } else {
156
+ sharedBindingsMap.delete(id)
157
+ }
158
+
141
159
  const compiledReferenceRoute = compileCodeSplitReferenceRoute({
142
160
  code,
143
161
  codeSplitGroupings: splitGroupings,
@@ -149,6 +167,7 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
149
167
  : undefined,
150
168
  addHmr:
151
169
  (userConfig.codeSplittingOptions?.addHmr ?? true) && !isProduction,
170
+ sharedBindings: sharedBindings.size > 0 ? sharedBindings : undefined,
152
171
  })
153
172
 
154
173
  if (compiledReferenceRoute === null) {
@@ -189,10 +208,14 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
189
208
  splitRouteIdentNodes.includes(p as any),
190
209
  ) as Array<SplitRouteIdentNodes>
191
210
 
211
+ const baseId = id.split('?')[0]!
212
+ const resolvedSharedBindings = sharedBindingsMap.get(baseId)
213
+
192
214
  const result = compileCodeSplitVirtualRoute({
193
215
  code,
194
216
  filename: id,
195
217
  splitTargets: grouping,
218
+ sharedBindings: resolvedSharedBindings,
196
219
  })
197
220
 
198
221
  if (debug) {
@@ -216,7 +239,7 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
216
239
  transform: {
217
240
  filter: {
218
241
  id: {
219
- exclude: tsrSplit,
242
+ exclude: [tsrSplit, tsrShared],
220
243
  // this is necessary for webpack / rspack to avoid matching .html files
221
244
  include: /\.(m|c)?(j|t)sx?$/,
222
245
  },
@@ -325,6 +348,60 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
325
348
  return handleCompilingVirtualFile(code, normalizedId)
326
349
  },
327
350
  },
351
+
352
+ vite: {
353
+ applyToEnvironment(environment) {
354
+ if (userConfig.plugin?.vite?.environmentName) {
355
+ return userConfig.plugin.vite.environmentName === environment.name
356
+ }
357
+ return true
358
+ },
359
+ },
360
+ },
361
+ {
362
+ name: 'tanstack-router:code-splitter:compile-shared-file',
363
+ enforce: 'pre',
364
+
365
+ transform: {
366
+ filter: {
367
+ id: /tsr-shared/,
368
+ },
369
+ handler(code, id) {
370
+ const url = pathToFileURL(id)
371
+ url.searchParams.delete('v')
372
+ const normalizedId = normalizePath(fileURLToPath(url))
373
+ const [baseId] = normalizedId.split('?')
374
+
375
+ if (!baseId) return null
376
+
377
+ const sharedBindings = sharedBindingsMap.get(baseId)
378
+ if (!sharedBindings || sharedBindings.size === 0) return null
379
+
380
+ if (debug) console.info('Compiling Shared Module: ', id)
381
+
382
+ const result = compileCodeSplitSharedRoute({
383
+ code,
384
+ sharedBindings,
385
+ filename: normalizedId,
386
+ })
387
+
388
+ if (debug) {
389
+ logDiff(code, result.code)
390
+ console.log('Output:\n', result.code + '\n\n')
391
+ }
392
+
393
+ return result
394
+ },
395
+ },
396
+
397
+ vite: {
398
+ applyToEnvironment(environment) {
399
+ if (userConfig.plugin?.vite?.environmentName) {
400
+ return userConfig.plugin.vite.environmentName === environment.name
401
+ }
402
+ return true
403
+ },
404
+ },
328
405
  },
329
406
  ]
330
407
  }