Skip to content

fix(alpha): Recursively import CSS assets #4254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 26, 2025
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
4 changes: 2 additions & 2 deletions examples/react/start-bare/src/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as React from 'react'
import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
import {
createRootRoute,
HeadContent,
Link,
Outlet,
Scripts,
createRootRoute,
} from '@tanstack/react-router'
import appCss from '~/styles/app.css?url'
import * as React from 'react'

export const Route = createRootRoute({
head: () => ({
Expand Down
3 changes: 3 additions & 0 deletions examples/react/start-bare/src/routes/about.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Counter from '~/components/Counter'

export const Route = createFileRoute({
component: RouteComponent,
})
Expand All @@ -6,6 +8,7 @@ function RouteComponent() {
return (
<main>
<h1>About</h1>
<Counter />
</main>
)
}
2 changes: 0 additions & 2 deletions examples/react/start-bare/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Counter from '~/components/Counter'
export const Route = createFileRoute({
component: RouteComponent,
})
Expand All @@ -7,7 +6,6 @@ function RouteComponent() {
return (
<main>
<h1 className="text-3xl text-blue-500 mb-5">Hello world!</h1>
<Counter />
</main>
)
}
54 changes: 33 additions & 21 deletions packages/start-plugin-core/src/routesManifestPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,35 @@ import type {
import type { Manifest, RouterManagedTag } from '@tanstack/router-core'
import type { TanStackStartOutputConfig } from './plugin'

const getCSSRecursively = (
file: ViteManifestChunk,
filesByRouteFilePath: ViteManifest,
) => {
const result: Array<RouterManagedTag> = []

// Get all css imports from the file
for (const cssFile of file.css ?? []) {
result.push({
tag: 'link',
attrs: {
rel: 'stylesheet',
href: joinURL('/', cssFile),
type: 'text/css',
},
})
}

// Recursively get CSS from imports
for (const imp of file.imports ?? []) {
const importInfo = filesByRouteFilePath[imp]
if (importInfo) {
result.push(...getCSSRecursively(importInfo, filesByRouteFilePath))
}
}

return result
}

export function startManifestPlugin(
opts: TanStackStartOutputConfig,
): PluginOption {
Expand Down Expand Up @@ -127,17 +156,7 @@ export function startManifestPlugin(
preloads.unshift(path.join('/', file.file))
}

const cssFiles = file.css ?? []
const cssAssetsList: Array<RouterManagedTag> = cssFiles.map(
(cssFile) => ({
tag: 'link',
attrs: {
rel: 'stylesheet',
href: joinURL('/', cssFile),
type: 'text/css',
},
}),
)
const cssAssetsList = getCSSRecursively(file, filesByRouteFilePath)

routes[k] = {
...v,
Expand All @@ -157,16 +176,9 @@ export function startManifestPlugin(

// Gather all the CSS files from the entry file in
// the `css` key and add them to the root route
const entryCssFiles = entryFile.css ?? []
const entryCssAssetsList: Array<RouterManagedTag> = entryCssFiles.map(
(cssFile) => ({
tag: 'link',
attrs: {
rel: 'stylesheet',
href: joinURL('/', cssFile),
type: 'text/css',
},
}),
const entryCssAssetsList = getCSSRecursively(
entryFile,
filesByRouteFilePath,
)

routes[rootRouteId]!.assets = [
Expand Down