Skip to content

Commit 964a618

Browse files
docs(backend-integration): add an example importedChunks (#18357)
Co-authored-by: 翠 / green <[email protected]>
1 parent f6b9074 commit 964a618

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

docs/guide/backend-integration.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,38 @@ If you need a custom integration, you can follow the steps in this guide to conf
151151
<!-- optional -->
152152
<link rel="modulepreload" href="assets/shared-B7PI925R.js" />
153153
```
154+
155+
::: details Pseudo implementation of `importedChunks`
156+
An example pseudo implementation of `importedChunks` in TypeScript (This will
157+
need to be adapted for your programming language and templating language):
158+
159+
```ts
160+
import type { Manifest, ManifestChunk } from 'vite'
161+
162+
export default function importedChunks(
163+
manifest: Manifest,
164+
name: string,
165+
): ManifestChunk[] {
166+
const seen = new Set<string>()
167+
168+
function getImportedChunks(chunk: ManifestChunk): ManifestChunk[] {
169+
const chunks: ManifestChunk[] = []
170+
for (const file of chunk.imports ?? []) {
171+
const importee = manifest[file]
172+
if (seen.has(file)) {
173+
continue
174+
}
175+
seen.add(file)
176+
177+
chunks.push(...getImportedChunks(importee))
178+
chunks.push(importee)
179+
}
180+
181+
return chunks
182+
}
183+
184+
return getImportedChunks(manifest[name])
185+
}
186+
```
187+
188+
:::

0 commit comments

Comments
 (0)