* 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.
The route generator stores file paths with forward slashes
but rspack and webpack on Windows pass native paths with
backslashes to transform handlers.
This caused TSR_ROUTES_BY_ID_MAP lookups to fail, preventing
autoCodeSplitting from working on Windows with rspack.The
route generator stores file paths with forward slashes, but
rspack and webpack on Windows pass native paths with backslashes to
transform handlers.
This caused TSR_ROUTES_BY_ID_MAP lookups to fail, preventing
autoCodeSplitting from working on Windows with rspack.
This change, makes it so that the `pendingComponent` is no longer
code-split by default when using the `automaticCodeSplitting` feature.
With this change, both the `loader` and `pendingComponent` will not be
code-split by _default_.
Currently, when the code-splitter encounters a known identifier used for
configuration in `createFileRoute` (such as `component`, `loader`,
`errorComponent`, etc.) it retains the export without splitting it out
into a separate file.
This functionality is currently partially working when the export is a
"default export" which references an already declared identifier.
```ts
const loader = () => {}
export default loader
```
However, it does not work when the "default export" is also making a
function declaration for the export.
```ts
export default function loader() {}
```
This PR adds the functionality to detect this use case and perform the
same transformations to align itself with the behaviour for all others
exports.
Currently, when using `automaticCodeSplitting`, as part of compiling the "reference" route, we add a component (named `TSRDummyComponent`) to aid with the HMR trickery.
In removing this component from the production bundle, it impacts the visitor by reducing the initial js bundle that wires the route-tree.
* chore(router-plugin): add `babel-dead-code-elimination`
* feat(router-plugin): initial implementation
* chore(router-plugin): better test scripts
* test(router-plugin): check it with the new functions
* fix(start-vite-plugin): add vite
* feat(router-plugin): bump unplugin
* fix(router-plugin): making sure it works
* chore(router-plugin): cleanup
* chore(router-plugin): delete "eliminateUnreferencedIdentifiers.ts"
* fix(router-plugin): esm problems
* chore(router-plugin): restore minification for production
* chore(start-vite-plugin): undo the vite
* Bump vite
* pnpm dedupe
* test(router-plugin): bit of renaming here
---------
Co-authored-by: Lachlan Collins <1667261+lachlancollins@users.noreply.github.com>
* test(router-plugin): tests for the route-items being wrapped in React.memo
* fix(router-plugin): React.memo breaks the codesplitting
* test(router-plugin): snapshots for the React.memo stuff
* refactor(router-plugin): variable naming is hard 😅
* chore(router-plugin): whoops
* refactor(router-plugin): use the helpers
* fix(router-plugin): unnecessarily importing items that should be eliminated
* fix(router-plugin): update snapshots
* fix(router-plugin): same treatment for loaders
* test(router-plugin): update snapshots
* style(router-plugin): eslint
* refactor(router-plugin): use guard clauses
* perf(router-plugin): reduce the double traversals being done here
* test(router-plugin): starting point
* fix(router-plugin): fix default imported components
* test(router-plugin): test case for there being both a default component import and a destructured loader import
* chore(router-plugin): cleanup of my testing test file...
* fix(router-plugin): package.json fields causing issues during imports
* fix: make the inlineConfig optional
* chore: trigger ci
* chore: try ci again
* chore: add a better readme
* test: just something to trigger CI honestly
* feat: scaffold the new `@tanstack/router-plugin` package
* chore: include in the overrides
* feat: more scaffolding
* test: just a random test that'll be removed later
* build: hopefully fixes build
* style: import order
* chore: peer deps for router-plugin
* feat: setup the generator
* refactor: combined needs to be done for the individual plugin
* chore: copy all the babel stuff over from router-vite-plugin
* feat: copy the babel plugin setup from router-plugin
* feat: enfore pre
* refactor: turn the error into its own custom one
* style: eslint
* style: more eslint
* chore: update the readme