Skip to content

Commit 21259b3

Browse files
committed
feat: guess chunk name
1 parent 93916a3 commit 21259b3

File tree

6 files changed

+92
-26
lines changed

6 files changed

+92
-26
lines changed

packages/vite/src/app/components/data/ChunkDetails.vue

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,11 @@ withDefaults(defineProps<{
3636
<slot />
3737
</div>
3838

39-
<template v-if="showImports && chunk.imports.length > 0">
40-
<div op50>
41-
Imports
42-
</div>
43-
<div flex="~ col gap-1" ws-nowrap>
44-
<DisplayChunkImports
45-
v-for="(importChunk, index) in chunk.imports"
46-
:key="index"
47-
:chunk-import="importChunk"
48-
:session="session"
49-
:importer="chunk"
50-
hover="bg-active"
51-
border="~ base rounded" px2 py1 w-full
52-
/>
53-
</div>
54-
</template>
55-
56-
<template v-if="showModules">
57-
<div op50>
58-
Modules
59-
</div>
60-
<div flex="~ col gap-1" ws-nowrap>
39+
<details v-if="showModules" open="true">
40+
<summary op50>
41+
<span>Modules ({{ chunk.modules.length }})</span>
42+
</summary>
43+
<div flex="~ col gap-1" mt2 ws-nowrap>
6144
<DisplayModuleId
6245
v-for="module of chunk.modules"
6346
:id="module"
@@ -69,6 +52,23 @@ withDefaults(defineProps<{
6952
border="~ base rounded" px2 py1 w-full
7053
/>
7154
</div>
72-
</template>
55+
</details>
56+
57+
<details v-if="showImports && chunk.imports.length > 0" open="true">
58+
<summary op50>
59+
<span>Imports ({{ chunk.imports.length }})</span>
60+
</summary>
61+
<div flex="~ col gap-1" mt2 ws-nowrap>
62+
<DisplayChunkImports
63+
v-for="(importChunk, index) in chunk.imports"
64+
:key="index"
65+
:chunk-import="importChunk"
66+
:session="session"
67+
:importer="chunk"
68+
hover="bg-active"
69+
border="~ base rounded" px2 py1 w-full
70+
/>
71+
</div>
72+
</details>
7373
</div>
7474
</template>

packages/vite/src/node/rpc/functions/rolldown-get-chunk-info.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineRpcFunction } from '@vitejs/devtools-kit'
2+
import { guessChunkName } from '../../../shared/utils/guess-chunk-name'
23
import { getLogsManager } from '../utils'
34

45
export const rolldownGetChunkInfo = defineRpcFunction({
@@ -9,7 +10,10 @@ export const rolldownGetChunkInfo = defineRpcFunction({
910
return {
1011
handler: async ({ session, id }: { session: string, id: number }) => {
1112
const reader = await manager.loadSession(session)
12-
return reader.manager.chunks.get(id)
13+
const chunk = reader.manager.chunks.get(id)
14+
if (chunk && !chunk.name)
15+
chunk.name = guessChunkName(chunk)
16+
return chunk
1317
},
1418
}
1519
},

packages/vite/src/node/rpc/functions/rolldown-get-chunks-graph.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineRpcFunction } from '@vitejs/devtools-kit'
2+
import { guessChunkName } from '../../../shared/utils/guess-chunk-name'
23
import { getLogsManager } from '../utils'
34

45
export const rolldownGetChunksGraph = defineRpcFunction({
@@ -9,7 +10,12 @@ export const rolldownGetChunksGraph = defineRpcFunction({
910
return {
1011
handler: async ({ session }: { session: string }) => {
1112
const reader = await manager.loadSession(session)
12-
return Array.from(reader.manager.chunks.values())
13+
const chunks = Array.from(reader.manager.chunks.values())
14+
chunks.forEach((chunk) => {
15+
if (chunk && !chunk.name)
16+
chunk.name = guessChunkName(chunk)
17+
})
18+
return chunks
1319
},
1420
}
1521
},

packages/vite/src/nuxt.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export default defineNuxtConfig({
130130
},
131131

132132
devtools: {
133-
enabled: true,
133+
enabled: false,
134134
},
135135

136136
typescript: {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { simplifyModuleName } from '../guess-chunk-name'
3+
4+
describe('simplifyModuleName', () => {
5+
it('should return the simplified module name', () => {
6+
expect(simplifyModuleName('foo/bar')).toBe('foo_bar')
7+
expect(simplifyModuleName('foo/bar/')).toBe('foo_bar')
8+
expect(simplifyModuleName('/foo/bar')).toBe('foo_bar')
9+
expect(simplifyModuleName('/foo/bar/')).toBe('foo_bar')
10+
expect(simplifyModuleName('foo/bar/index')).toBe('foo_bar')
11+
expect(simplifyModuleName('foo/bar/index/')).toBe('foo_bar')
12+
expect(simplifyModuleName('foo/bar/index.ts')).toBe('foo_bar')
13+
expect(simplifyModuleName('node_modules/foo/bar/index.tsx')).toBe('foo_bar')
14+
expect(simplifyModuleName('node_modules/foo/bar/index.jsx')).toBe('foo_bar')
15+
expect(simplifyModuleName('foo/bar/index.mjs')).toBe('foo_bar')
16+
expect(simplifyModuleName('foo/bar/index.cjs')).toBe('foo_bar')
17+
expect(simplifyModuleName('node_modules/foo/bar/index.tsx')).toBe('foo_bar')
18+
expect(simplifyModuleName('node_modules/foo/bar/index.jsx')).toBe('foo_bar')
19+
expect(simplifyModuleName('node_modules/foo/bar/index.mjs')).toBe('foo_bar')
20+
expect(simplifyModuleName('node_modules/foo/bar/index.cjs')).toBe('foo_bar')
21+
})
22+
})
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { Chunk as ChunkInfo } from '@rolldown/debug'
2+
3+
export function guessChunkName(chunk: ChunkInfo) {
4+
if (chunk.name)
5+
return chunk.name
6+
if (chunk.modules.length === 1)
7+
return `[${simplifyModuleName(chunk.modules[0]!)}]`
8+
if (chunk.modules.length > 1)
9+
return `[${simplifyModuleName(`${chunk.modules[0]}`)}_${chunk.modules.length}]`
10+
return '[unnamed]'
11+
}
12+
13+
export function simplifyModuleName(module: string) {
14+
let parts = module
15+
.replace(/^.*(\.pnpm|node_modules|src|app|packages)\//gi, '') // remove anything before node_modules or src
16+
.replace(/\b(index|main|dist|test|component|components)\b/gi, '') // boring names
17+
.replace(/\/+/g, '/') // duplicate slashes
18+
.replace(/\?.*$/, '') // query string
19+
.replace(/\.\w+$/, '') // extension
20+
.replace(/\W/g, '_') // non-alphanumeric characters
21+
.replace(/_+/g, '_') // duplicate underscores
22+
.replace(/^_+|_+$/g, '') // leading/trailing underscores
23+
.replace(/([a-z])([A-Z])/g, '$1_$2') // camel case to snake case
24+
.toLowerCase()
25+
.split('_')
26+
.filter(Boolean)
27+
28+
parts = Array.from(new Set(parts))
29+
30+
if (parts.length > 5)
31+
parts = parts.slice(0, 5)
32+
33+
return parts.join('_')
34+
}

0 commit comments

Comments
 (0)