Skip to content

Commit 59aa0a6

Browse files
fix
1 parent 84eee6a commit 59aa0a6

2 files changed

Lines changed: 185 additions & 28 deletions

File tree

‎packages/start-plugin-core/src/start-manifest-plugin/manifestBuilder.ts‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { tsrSplit } from '@tanstack/router-plugin'
55
import type { RouterManagedTag } from '@tanstack/router-core'
66
import type { Rollup } from 'vite'
77

8-
const STATIC_IMPORT_MODE = 1
9-
const DYNAMIC_IMPORT_MODE = 2
8+
const ROUTER_MANAGED_MODE = 1
9+
const NON_ROUTE_DYNAMIC_MODE = 2
1010
const VISITING_CHUNK = 1
1111

1212
type RouteTreeRoute = {
@@ -135,14 +135,14 @@ export function buildStartManifest(options: {
135135
basePath: string
136136
}) {
137137
const scannedChunks = scanClientChunks(options.clientBundle)
138-
const dynamicCssFiles = collectDynamicImportCss(
138+
const hashedCssFiles = collectDynamicImportCss(
139139
scannedChunks.routeEntryChunks,
140140
scannedChunks.chunksByFileName,
141141
scannedChunks.entryChunk,
142142
)
143143
const assetResolvers = createManifestAssetResolvers({
144144
basePath: options.basePath,
145-
dynamicCssFiles,
145+
hashedCssFiles,
146146
})
147147

148148
const routes = buildRouteManifestRoutes({
@@ -271,19 +271,21 @@ export function collectDynamicImportCss(
271271
chunksByFileName: Map<string, Rollup.OutputChunk>,
272272
entryChunk?: Rollup.OutputChunk,
273273
) {
274-
const dynamicCssFiles = new Set<string>()
274+
const routerManagedCssFiles = new Set<string>()
275+
const nonRouteDynamicCssFiles = new Set<string>()
276+
const hashedCssFiles = new Set<string>()
275277
const visitedByChunk = new Map<Rollup.OutputChunk, number>()
276278
const chunkStack: Array<Rollup.OutputChunk> = []
277279
const modeStack: Array<number> = []
278280

279281
for (const routeEntryChunk of routeEntryChunks) {
280282
chunkStack.push(routeEntryChunk)
281-
modeStack.push(STATIC_IMPORT_MODE)
283+
modeStack.push(ROUTER_MANAGED_MODE)
282284
}
283285

284286
if (entryChunk) {
285287
chunkStack.push(entryChunk)
286-
modeStack.push(STATIC_IMPORT_MODE)
288+
modeStack.push(ROUTER_MANAGED_MODE)
287289
}
288290

289291
while (chunkStack.length > 0) {
@@ -297,9 +299,15 @@ export function collectDynamicImportCss(
297299

298300
visitedByChunk.set(chunk, previousMode | mode)
299301

300-
if ((mode & DYNAMIC_IMPORT_MODE) !== 0) {
302+
if ((mode & ROUTER_MANAGED_MODE) !== 0) {
301303
for (const cssFile of chunk.viteMetadata?.importedCss ?? []) {
302-
dynamicCssFiles.add(cssFile)
304+
routerManagedCssFiles.add(cssFile)
305+
}
306+
}
307+
308+
if ((mode & NON_ROUTE_DYNAMIC_MODE) !== 0) {
309+
for (const cssFile of chunk.viteMetadata?.importedCss ?? []) {
310+
nonRouteDynamicCssFiles.add(cssFile)
303311
}
304312
}
305313

@@ -317,17 +325,28 @@ export function collectDynamicImportCss(
317325
)
318326
if (dynamicImportedChunk) {
319327
chunkStack.push(dynamicImportedChunk)
320-
modeStack.push(DYNAMIC_IMPORT_MODE)
328+
modeStack.push(
329+
(mode & NON_ROUTE_DYNAMIC_MODE) !== 0 ||
330+
!routeEntryChunks.has(dynamicImportedChunk)
331+
? NON_ROUTE_DYNAMIC_MODE
332+
: ROUTER_MANAGED_MODE,
333+
)
321334
}
322335
}
323336
}
324337

325-
return dynamicCssFiles
338+
for (const cssFile of routerManagedCssFiles) {
339+
if (nonRouteDynamicCssFiles.has(cssFile)) {
340+
hashedCssFiles.add(cssFile)
341+
}
342+
}
343+
344+
return hashedCssFiles
326345
}
327346

328347
export function createManifestAssetResolvers(options: {
329348
basePath: string
330-
dynamicCssFiles: Set<string>
349+
hashedCssFiles?: Set<string>
331350
}): ManifestAssetResolvers {
332351
const assetPathByFileName = new Map<string, string>()
333352
const stylesheetAssetByFileName = new Map<string, RouterManagedTag>()
@@ -355,7 +374,7 @@ export function createManifestAssetResolvers(options: {
355374
tag: 'link',
356375
attrs: {
357376
rel: 'stylesheet',
358-
href: options.dynamicCssFiles.has(cssFile) ? `${href}#` : href,
377+
href: options.hashedCssFiles?.has(cssFile) ? `${href}#` : href,
359378
type: 'text/css',
360379
},
361380
} satisfies RouterManagedTag
@@ -462,9 +481,8 @@ export function buildRouteManifestRoutes(options: {
462481
continue
463482
}
464483

465-
const targetRoute = (routes[routeId] = routes[routeId]
466-
? routes[routeId]
467-
: { ...route })
484+
const existing = routes[routeId]
485+
const targetRoute = (routes[routeId] = existing ? existing : { ...route })
468486

469487
for (const chunk of chunks) {
470488
mergeRouteChunkData({

‎packages/start-plugin-core/tests/start-manifest-plugin/manifestBuilder.test.ts‎

Lines changed: 151 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe('appendUniqueAssets', () => {
139139
test('returns original target array when nothing new is appended', () => {
140140
const target = [
141141
{
142-
tag: 'link',
142+
tag: 'link' as const,
143143
attrs: {
144144
rel: 'stylesheet',
145145
href: '/assets/a.css',
@@ -151,7 +151,7 @@ describe('appendUniqueAssets', () => {
151151
expect(
152152
appendUniqueAssets(target, [
153153
{
154-
tag: 'link',
154+
tag: 'link' as const,
155155
attrs: {
156156
rel: 'stylesheet',
157157
href: '/assets/a.css',
@@ -221,20 +221,18 @@ describe('collectDynamicImportCss', () => {
221221
]),
222222
)
223223

224-
expect(Array.from(dynamicCss).sort()).toEqual(['lazy.css', 'shared.css'])
225-
expect(dynamicCss.has('route.css')).toBe(false)
224+
expect(Array.from(dynamicCss)).toEqual([])
226225
})
227226

228-
test('collects css reachable both statically and dynamically from different entry points', () => {
229-
// shared.css is statically imported by routeA, and dynamically imported by routeB
227+
test('ignores CSS shared only across router-managed routes', () => {
230228
const routeA = makeChunk({
231229
fileName: 'routeA.js',
232230
imports: ['shared.js'],
233231
moduleIds: ['/routes/a.tsx?tsr-split=component'],
234232
})
235233
const routeB = makeChunk({
236234
fileName: 'routeB.js',
237-
dynamicImports: ['shared.js'],
235+
imports: ['shared.js'],
238236
moduleIds: ['/routes/b.tsx?tsr-split=component'],
239237
})
240238
const sharedChunk = makeChunk({
@@ -251,7 +249,40 @@ describe('collectDynamicImportCss', () => {
251249
]),
252250
)
253251

254-
// shared.css is reachable via dynamic import from routeB, so it should be in the set
252+
expect(dynamicCss.has('shared.css')).toBe(false)
253+
})
254+
255+
test('collects css reachable through both router-managed and non-route dynamic imports', () => {
256+
const entryChunk = makeChunk({
257+
fileName: 'entry.js',
258+
isEntry: true,
259+
dynamicImports: ['lazy.js'],
260+
})
261+
const routeChunk = makeChunk({
262+
fileName: 'route.js',
263+
imports: ['shared.js'],
264+
moduleIds: ['/routes/home.tsx?tsr-split=component'],
265+
})
266+
const lazyChunk = makeChunk({
267+
fileName: 'lazy.js',
268+
imports: ['shared.js'],
269+
})
270+
const sharedChunk = makeChunk({
271+
fileName: 'shared.js',
272+
importedCss: ['shared.css'],
273+
})
274+
275+
const dynamicCss = collectDynamicImportCss(
276+
new Set([routeChunk]),
277+
new Map([
278+
['entry.js', entryChunk],
279+
['route.js', routeChunk],
280+
['lazy.js', lazyChunk],
281+
['shared.js', sharedChunk],
282+
]),
283+
entryChunk,
284+
)
285+
255286
expect(dynamicCss.has('shared.css')).toBe(true)
256287
})
257288
})
@@ -274,7 +305,7 @@ describe('createManifestAssetResolvers + createChunkCssAssetCollector', () => {
274305

275306
const resolvers = createManifestAssetResolvers({
276307
basePath: '/assets',
277-
dynamicCssFiles: new Set(['shared.css']),
308+
hashedCssFiles: new Set(['shared.css']),
278309
})
279310
const cssAssetCollector = createChunkCssAssetCollector({
280311
chunksByFileName,
@@ -343,7 +374,7 @@ describe('createChunkCssAssetCollector', () => {
343374
})
344375

345376
describe('buildStartManifest', () => {
346-
test('builds route and root assets with dynamic css hash handling', () => {
377+
test('hashes css shared by route chunks and nested non-route dynamic imports', () => {
347378
const entryChunk = makeChunk({
348379
fileName: 'entry.js',
349380
isEntry: true,
@@ -410,6 +441,114 @@ describe('buildStartManifest', () => {
410441
'/assets/route-lazy.js',
411442
])
412443
})
444+
445+
test('keeps entry-owned css unsuffixed when shared only with route chunks', () => {
446+
const entryChunk = makeChunk({
447+
fileName: 'entry.js',
448+
isEntry: true,
449+
imports: ['shared.js'],
450+
dynamicImports: ['about.js'],
451+
})
452+
const sharedChunk = makeChunk({
453+
fileName: 'shared.js',
454+
importedCss: ['main.css'],
455+
})
456+
const aboutChunk = makeChunk({
457+
fileName: 'about.js',
458+
imports: ['shared.js'],
459+
importedCss: ['about.css'],
460+
moduleIds: ['/routes/about.tsx?tsr-split=component'],
461+
})
462+
463+
const manifest = buildStartManifest({
464+
clientBundle: {
465+
'entry.js': entryChunk,
466+
'shared.js': sharedChunk,
467+
'about.js': aboutChunk,
468+
},
469+
routeTreeRoutes: {
470+
__root__: { children: ['/about'] } as any,
471+
'/about': { filePath: '/routes/about.tsx' },
472+
},
473+
basePath: '/assets',
474+
})
475+
476+
expect(manifest.routes.__root__!.assets).toEqual([
477+
{
478+
tag: 'link',
479+
attrs: {
480+
rel: 'stylesheet',
481+
href: '/assets/main.css',
482+
type: 'text/css',
483+
},
484+
},
485+
])
486+
487+
expect(manifest.routes['/about']!.assets).toEqual([
488+
{
489+
tag: 'link',
490+
attrs: {
491+
rel: 'stylesheet',
492+
href: '/assets/about.css',
493+
type: 'text/css',
494+
},
495+
},
496+
{
497+
tag: 'link',
498+
attrs: {
499+
rel: 'stylesheet',
500+
href: '/assets/main.css',
501+
type: 'text/css',
502+
},
503+
},
504+
])
505+
})
506+
507+
test('adds hash only when css is shared by router-managed and non-route dynamic imports', () => {
508+
const entryChunk = makeChunk({
509+
fileName: 'entry.js',
510+
isEntry: true,
511+
dynamicImports: ['global-lazy.js'],
512+
})
513+
const routeChunk = makeChunk({
514+
fileName: 'route.js',
515+
imports: ['shared.js'],
516+
moduleIds: ['/routes/about.tsx?tsr-split=component'],
517+
})
518+
const sharedChunk = makeChunk({
519+
fileName: 'shared.js',
520+
importedCss: ['shared.css'],
521+
})
522+
const globalLazyChunk = makeChunk({
523+
fileName: 'global-lazy.js',
524+
imports: ['shared.js'],
525+
})
526+
527+
const manifest = buildStartManifest({
528+
clientBundle: {
529+
'entry.js': entryChunk,
530+
'route.js': routeChunk,
531+
'shared.js': sharedChunk,
532+
'global-lazy.js': globalLazyChunk,
533+
},
534+
routeTreeRoutes: {
535+
__root__: { children: ['/about'] } as any,
536+
'/about': { filePath: '/routes/about.tsx' },
537+
},
538+
basePath: '/assets',
539+
})
540+
541+
expect(manifest.routes['/about']!.assets).toEqual([
542+
{
543+
tag: 'link',
544+
attrs: {
545+
rel: 'stylesheet',
546+
href: '/assets/shared.css#',
547+
type: 'text/css',
548+
},
549+
},
550+
])
551+
})
413552
})
414553

415554
describe('dedupeNestedRoutePreloads', () => {
@@ -642,8 +781,8 @@ describe('entry chunk dynamic imports must be scanned for dynamic CSS', () => {
642781
basePath: '/assets',
643782
})
644783

645-
// global-lazy.css is reachable via dynamic import from entry chunk,
646-
// so the route's reference to it should have the # suffix
784+
// global-lazy.css is reachable through both the router-managed route
785+
// and the entry chunk's non-route dynamic import, so it should have #
647786
const homeAssets = manifest.routes['/home']!.assets!
648787
const globalLazyCss = homeAssets.find((a: any) =>
649788
a.attrs.href.includes('global-lazy.css'),

0 commit comments

Comments
 (0)