#Route file export shows "not be code-split and will increase your bundle size" warning

8 messages · Page 1 of 1 (latest)

trail portal
#

After upgrading to router v1.170.4, I'm starting to see warnings that read like:

[tanstack-router] These exports from "/Users/me/repos/myrepo/apps/myapp/src/routes/my-route/index.tsx" will not be code-split and will increase your bundle size:
- RouteComponent

The only reason I'm exporting that RouteComponent is to unit test it. It's the component that I pass to createFileRoute(url, { component: RouteComponent }) Is there some way I can unit test it without exporting it?

potent dew
#

There are a few ways I can think of

#
  1. If you have vitest, you can use in-source testing with
if (import.meta.vitest) {
... 
}
#
  1. I think this is the hacky solution (does Route have a component property?)
const RouteComponent = jest.mocked(Route.component)
trail portal
#

Route does not have a component property, I checked that

potent dew
#
  1. If Route does not have component property, then use mock functions to obtain the component

jest.mock('@tanstack/react-router', () => jest.fn())

// somewhere
const mockRouteFactory = jest.fn();
jest.mocked(createFileRoute).mockReturnValue(mockRouteFactory);
const RouteComponent = mockRouteFactory.mock.calls[0][0];
lavish crag
#

move the route component into a separate file outside the route

native bramble
#

move the route component into a separate file outside the route

Maintaining seperate file for each route component and route does not makes that much sense to me