0
Fork 0
mirror of https://github.com/TanStack/router.git synced 2026-11-16 19:38:07 +00:00
router/packages/router-plugin/tests/code-splitter/test-files/react
Manuel Schiller 523972e0df
fix: shared virtual module for code-split routes (#6659)
* fix: shared virtual module for code-split routes

When a route file has module-level bindings (variables, functions, classes) referenced by **both** split and non-split route properties, the bindings are duplicated — initialized independently in the reference file and in each virtual split file. This breaks singletons, causes double side effects, and wastes bundle size.

```tsx
const collection = { name: 'todos', preload: async () => {} }
export const Route = createFileRoute('/todos')({
  loader: async () => {
    await collection.preload()
  }, // stays in reference
  component: () => <div>{collection.name}</div>, // split to virtual
})
```
In this example, `collection` is initialized in **both** the reference file (because `loader` references it) and the `?tsr-split=component` virtual file (because the component references it). Two separate instances exist at runtime.

To solve this issue, this PR introduces a third virtual module `route.tsx?tsr-shared=1` that contains shared bindings. Both the reference and virtual files import from it.

When `sharedBindings` is empty (the common case — component-only helpers, loader-only code), no shared module exists and the system works exactly as today.

The reference transform runs first and will emit a static import of `?tsr-shared=1` **only when needed**. Since split chunks are only discovered via the reference file's emitted `import('...?tsr-split=...')`, the bundler will always process/evaluate the reference module first. Result: the shared module is evaluated once and then reused by any later-loaded split chunk.
History 2026-02-14 13:10:54 +01:00
..
arrow-function.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
boolean-null-literals.tsx fix(router-plugin): add support for boolean and null literals in code splitter (#4915) 2025-08-13 13:01:57 +12:00
chinese.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
circular-reference-arrow-function.tsx fix: do not rename functions / variables when code splitting (#4960) 2025-08-15 17:13:26 +02:00
circular-reference-function.tsx fix: do not rename functions / variables when code splitting (#4960) 2025-08-15 17:13:26 +02:00
conditional-properties.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
destructured-export-multiple.tsx fix: autoCodeSplitting` with destructured export (#6656) 2026-02-14 02:26:03 +01:00
destructured-export-nested.tsx fix: autoCodeSplitting` with destructured export (#6656) 2026-02-14 02:26:03 +01:00
destructured-export.tsx fix: autoCodeSplitting` with destructured export (#6656) 2026-02-14 02:26:03 +01:00
destructured-react-memo-imported-component.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
destructured-route-options-defaults.tsx fix: autoCodeSplitting` with destructured export (#6656) 2026-02-14 02:26:03 +01:00
destructuring.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
directive-prologue.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
explicit-undefined-component.tsx fix(router-plugin): code-splitting to handle explicit undefined (#4564) 2025-07-07 15:35:55 +12:00
export-default-component-and-normal-notFound.tsx fix(router-plugin): code-splitting to handle default exported function declarations (#3901) 2025-04-01 10:14:32 +13:00
export-default-component.tsx fix(router-plugin): code-splitting to handle default exported function declarations (#3901) 2025-04-01 10:14:32 +13:00
function-as-parameter.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
function-declaration.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
importAttribute.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
imported-default-component-destructured-loader.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
imported-default-component.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
imported-errorComponent.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
imported-notFoundComponent.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
imported-pendingComponent.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
imported.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
inline.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
random-number.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
react-memo-component.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
react-memo-imported-component.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
retain-export-component.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
retain-exports-const.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
retain-exports-destructured.tsx fix: autoCodeSplitting` with destructured export (#6656) 2026-02-14 02:26:03 +01:00
retain-exports-function.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
retain-exports-loader.tsx refactor(router-plugin): code-splitting to be agnostic to support 'targets' (#3400) 2025-02-12 15:21:06 +13:00
shared-class.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
shared-destructured-export.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
shared-destructured.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
shared-exported.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
shared-function.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
shared-imported-binding.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
shared-indirect-ref.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
shared-jsx-component-ref.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
shared-none.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
shared-referencing-route.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
shared-transitive.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
shared-variable.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
shared-with-side-effect.tsx fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
undefined-literals.tsx fix(router-plugin): add support for boolean and null literals in code splitter (#4915) 2025-08-13 13:01:57 +12:00
useStateDestructure.tsx chore: optimize links to github (#6633) 2026-02-10 23:42:52 +01:00
using.tsx feat: Merge alpha to main (#4365) 2025-06-10 23:19:32 +02:00