Skip to content

Commit 84228d5

Browse files
wtlin1228weitang.linSeanCassiere
authored
fix(router-plugin): code-splitting to handle explicit undefined (#4564)
Co-authored-by: weitang.lin <weitang.lin@mujin.co.jp> Co-authored-by: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com>
1 parent e0dfe30 commit 84228d5

22 files changed

+82
-1
lines changed

packages/router-plugin/src/core/code-splitter/compilers.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,16 @@ export function compileCodeSplitReferenceRoute(
209209
return
210210
}
211211

212+
// Exit early if the value is undefined
213+
// Since we don't need to run an import just to get the value of `undefined`
214+
// This is useful for cases like: `createFileRoute('/')({ component: undefined })`
215+
if (
216+
t.isIdentifier(prop.value) &&
217+
prop.value.name === 'undefined'
218+
) {
219+
return
220+
}
221+
212222
const splitNodeMeta = SPLIT_NODES_CONFIG.get(key as any)!
213223

214224
// We need to extract the existing search params from the filename, if any
@@ -253,7 +263,6 @@ export function compileCodeSplitReferenceRoute(
253263
// Prepend the import statement to the program along with the importer function
254264
// Check to see if lazyRouteComponent is already imported before attempting
255265
// to import it again
256-
257266
if (
258267
!hasImportedOrDefinedIdentifier(
259268
LAZY_ROUTE_COMPONENT_IDENT,
@@ -461,6 +470,14 @@ export function compileCodeSplitVirtualRoute(
461470

462471
const value = prop.value
463472

473+
// If the value for the `key` is `undefined`, then we don't need to include it
474+
// in the split file, so we can just return, since it will kept in-place in the
475+
// reference file
476+
// This is useful for cases like: `createFileRoute('/')({ component: undefined })`
477+
if (t.isIdentifier(value) && value.name === 'undefined') {
478+
return
479+
}
480+
464481
let isExported = false
465482
if (t.isIdentifier(value)) {
466483
isExported = hasExport(ast, value)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
import { createFileRoute } from '@tanstack/react-router';
3+
export const Route = createFileRoute('/')({
4+
component: undefined
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import React from 'react';
2+
import { Route } from "explicit-undefined-component.tsx";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import React from 'react';
2+
import { Route } from "explicit-undefined-component.tsx";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import React from 'react';
2+
import { Route } from "explicit-undefined-component.tsx";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
import { createFileRoute } from '@tanstack/react-router';
3+
export const Route = createFileRoute('/')({
4+
component: undefined
5+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import React from 'react';
2+
import { Route } from "explicit-undefined-component.tsx";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import React from 'react';
2+
import { Route } from "explicit-undefined-component.tsx";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import React from 'react';
2+
import { Route } from "explicit-undefined-component.tsx";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react';
2+
import { createFileRoute } from '@tanstack/react-router';
3+
export const Route = createFileRoute('/')({
4+
component: undefined
5+
});

0 commit comments

Comments
 (0)