Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/router-plugin/src/core/code-splitter/compilers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export function compileCodeSplitReferenceRoute(
id: string
addHmr?: boolean
},
): GeneratorResult {
): GeneratorResult | null {
const ast = parseAst(opts)

const refIdents = findReferencedIdentifiers(ast)
Expand All @@ -132,6 +132,7 @@ export function compileCodeSplitReferenceRoute(

let createRouteFn: string

let modified = false as boolean
babel.traverse(ast, {
Program: {
enter(programPath) {
Expand Down Expand Up @@ -170,6 +171,7 @@ export function compileCodeSplitReferenceRoute(
if (t.isObjectProperty(prop)) {
if (t.isIdentifier(prop.key)) {
if (opts.deleteNodes!.has(prop.key.name as any)) {
modified = true
return false
}
}
Expand All @@ -181,6 +183,7 @@ export function compileCodeSplitReferenceRoute(
if (!splittableCreateRouteFns.includes(createRouteFn)) {
// we can't split this route but we still add HMR handling if enabled
if (opts.addHmr) {
modified = true
programPath.pushContainer('body', routeHmrStatement)
}
// exit traversal so this route is not split
Expand Down Expand Up @@ -268,6 +271,8 @@ export function compileCodeSplitReferenceRoute(
return
}

modified = true

// Prepend the import statement to the program along with the importer function
// Check to see if lazyRouteComponent is already imported before attempting
// to import it again
Expand Down Expand Up @@ -305,9 +310,8 @@ export function compileCodeSplitReferenceRoute(
if (opts.addHmr) {
programPath.pushContainer('body', routeHmrStatement)
}
}

if (splitNodeMeta.splitStrategy === 'lazyFn') {
} else {
// if (splitNodeMeta.splitStrategy === 'lazyFn') {
const value = prop.value

let shouldSplit = true
Expand Down Expand Up @@ -339,6 +343,7 @@ export function compileCodeSplitReferenceRoute(
if (!shouldSplit) {
return
}
modified = true

// Prepend the import statement to the program along with the importer function
if (!hasImportedOrDefinedIdentifier(LAZY_FN_IDENT)) {
Expand Down Expand Up @@ -404,6 +409,7 @@ export function compileCodeSplitReferenceRoute(
* specifiers
*/
if (removableImportPaths.size > 0) {
modified = true
programPath.traverse({
ImportDeclaration(path) {
if (path.node.specifiers.length > 0) return
Expand All @@ -417,6 +423,9 @@ export function compileCodeSplitReferenceRoute(
},
})

if (!modified) {
return null
}
deadCodeElimination(ast, refIdents)

// if there are exported identifiers, then we need to add a warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ export const unpluginRouterCodeSplitterFactory: UnpluginFactory<
(userConfig.codeSplittingOptions?.addHmr ?? true) && !isProduction,
})

if (compiledReferenceRoute === null) {
if (debug) {
console.info(
`No changes made to route "${id}", skipping code-splitting.`,
)
}
return null
}
if (debug) {
logDiff(code, compiledReferenceRoute.code)
console.log('Output:\n', compiledReferenceRoute.code + '\n\n')
Expand Down
4 changes: 2 additions & 2 deletions packages/router-plugin/tests/add-hmr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('add-hmr works', () => {
targetFramework: framework,
})

await expect(compileResult.code).toMatchFileSnapshot(
await expect(compileResult?.code || code).toMatchFileSnapshot(
path.join(dirs.snapshots, filename.replace('.tsx', '@true.tsx')),
)
},
Expand All @@ -53,7 +53,7 @@ describe('add-hmr works', () => {
targetFramework: framework,
})

await expect(compileResult.code).toMatchFileSnapshot(
await expect(compileResult?.code || code).toMatchFileSnapshot(
path.join(dirs.snapshots, filename.replace('.tsx', '@false.tsx')),
)
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import thing from 'thing';
import thing from 'thing'

export function test() {
const {
foo: {
bar: {
destructured
}
}
} = thing;
return destructured;
}
bar: { destructured },
},
} = thing

return destructured
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import thing from 'thing';
import thing from 'thing'

export function test() {
const {
foo: {
bar: {
destructured
}
}
} = thing;
return destructured;
}
bar: { destructured },
},
} = thing

return destructured
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { createFileRoute } from '@tanstack/react-router';
import React from 'react'
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/')({
component: undefined
});
component: undefined,
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { createFileRoute } from '@tanstack/react-router';
import React from 'react'
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/')({
component: undefined
});
component: undefined,
})
2 changes: 1 addition & 1 deletion packages/router-plugin/tests/code-splitter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('code-splitter works', () => {
targetFramework: framework,
})

await expect(compileResult.code).toMatchFileSnapshot(
await expect(compileResult?.code || code).toMatchFileSnapshot(
path.join(dirs.snapshots, groupName, filename),
)
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import thing from 'thing';
import thing from 'thing'

export function test() {
const {
foo: {
bar: {
destructured
}
}
} = thing;
return destructured;
}
bar: { destructured },
},
} = thing

return destructured
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import { createFileRoute } from '@tanstack/react-router';
import React from 'react'
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/')({
component: undefined
});
component: undefined,
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
console.warn("[tanstack-router] These exports from \"export-default-component.tsx\" will not be code-split and will increase your bundle size:\n- Home\nFor the best optimization, these items should either have their export statements removed, or be imported from another location that is not a route file.");
import React, { useState } from 'react';
import { createFileRoute } from '@tanstack/react-router';
import React, { useState } from 'react'
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/home')({
component: Home
});
component: Home,
})

export default function Home() {
const [one, setOne] = useState('this is from a state');
return <div>
const [one, setOne] = useState('this is from a state')

return (
<div>
<h1>{one}</h1>
</div>;
}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import * as React from 'react';
import * as React from 'react'
// @ts-expect-error
import { useMemo } from 'tan-react';
const useUsedVar = 'i-am-unused';
import { useMemo } from 'tan-react'

const useUsedVar = 'i-am-unused'

const ReactUseMemoCall1 = React.useMemo(function performAction() {
return 'true';
}, []);
console.info(ReactUseMemoCall1);
return 'true'
}, [])

console.info(ReactUseMemoCall1)

const ReactUseMemoCall2 = React.useMemo(() => {
return 'true';
}, []);
console.info(ReactUseMemoCall2);
return 'true'
}, [])

console.info(ReactUseMemoCall2)

const UseMemoCall1 = useMemo(function performAction() {
return 'true';
}, []);
console.info(UseMemoCall1);
return 'true'
}, [])

console.info(UseMemoCall1)

const UseMemoCall2 = useMemo(() => {
return 'true';
}, []);
console.info(UseMemoCall2);
return 'true'
}, [])

console.info(UseMemoCall2)
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
console.warn("[tanstack-router] These exports from \"retain-export-component.tsx\" will not be code-split and will increase your bundle size:\n- Layout\nFor the best optimization, these items should either have their export statements removed, or be imported from another location that is not a route file.");
import * as React from 'react';
import { createFileRoute, Outlet } from '@tanstack/react-router';
import { importedComponent as ImportedComponent, importedLoader } from '../../shared/imported';
import * as React from 'react'
import { createFileRoute, Outlet } from '@tanstack/react-router'
import {
importedComponent as ImportedComponent,
importedLoader,
} from '../../shared/imported'

export function Layout() {
return <main>
<header style={{
height: HEADER_HEIGHT
}}>
return (
<main>
<header style={{ height: HEADER_HEIGHT }}>
<nav>
<ul>
<li>
Expand All @@ -17,13 +19,16 @@ export function Layout() {
</header>
<ImportedComponent />
<Outlet />
</main>;
</main>
)
}

export const Route = createFileRoute('/_layout')({
component: Layout,
loader: importedLoader
});
const HEADER_HEIGHT = '63px';
export const SIDEBAR_WIDTH = '150px';
const SIDEBAR_MINI_WIDTH = '80px';
const ASIDE_WIDTH = '250px';
loader: importedLoader,
})

const HEADER_HEIGHT = '63px'
export const SIDEBAR_WIDTH = '150px'
const SIDEBAR_MINI_WIDTH = '80px'
const ASIDE_WIDTH = '250px'
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
console.warn("[tanstack-router] These exports from \"retain-exports-const.tsx\" will not be code-split and will increase your bundle size:\n- Layout\nFor the best optimization, these items should either have their export statements removed, or be imported from another location that is not a route file.");
import * as React from 'react';
import { createFileRoute, Outlet } from '@tanstack/react-router';
import { importedComponent as ImportedComponent, importedLoader } from '../../shared/imported';
import * as React from 'react'
import { createFileRoute, Outlet } from '@tanstack/react-router'
import {
importedComponent as ImportedComponent,
importedLoader,
} from '../../shared/imported'

export const loaderFn = () => {
return importedLoader();
};
return importedLoader()
}

const Layout = () => {
return <main>
<header style={{
height: HEADER_HEIGHT
}}>
return (
<main>
<header style={{ height: HEADER_HEIGHT }}>
<nav>
<ul>
<li>
Expand All @@ -20,14 +23,18 @@ const Layout = () => {
</header>
<ImportedComponent />
<Outlet />
</main>;
};
</main>
)
}

export const Route = createFileRoute('/_layout')({
component: Layout,
loader: loaderFn
});
const HEADER_HEIGHT = '63px';
export const SIDEBAR_WIDTH = '150px';
export const SIDEBAR_MINI_WIDTH = '80px';
const ASIDE_WIDTH = '250px';
export default Layout;
loader: loaderFn,
})

const HEADER_HEIGHT = '63px'
export const SIDEBAR_WIDTH = '150px'
export const SIDEBAR_MINI_WIDTH = '80px'
const ASIDE_WIDTH = '250px'

export default Layout
Loading
Loading