From 077ae9cea51e26ef4e71435fdaa41e659dcb0cfc Mon Sep 17 00:00:00 2001
From: Jun Shindo <46585162+jay-es@users.noreply.github.com>
Date: Sun, 20 Oct 2024 15:47:27 +0900
Subject: [PATCH] docs(backend-integration): add an example `importedChunks`
(#1634)
---
guide/backend-integration.md | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/guide/backend-integration.md b/guide/backend-integration.md
index 7697b793..c3f2f73d 100644
--- a/guide/backend-integration.md
+++ b/guide/backend-integration.md
@@ -151,3 +151,37 @@
```
+
+ ::: details `importedChunks` の疑似実装
+ TypeScript での `importedChunks` の疑似実装の例(これは、プログラミング言語とテンプレート言語に合わせて調整する必要があります):
+
+ ```ts
+ import type { Manifest, ManifestChunk } from 'vite'
+
+ export default function importedChunks(
+ manifest: Manifest,
+ name: string,
+ ): ManifestChunk[] {
+ const seen = new Set()
+
+ function getImportedChunks(chunk: ManifestChunk): ManifestChunk[] {
+ const chunks: ManifestChunk[] = []
+ for (const file of chunk.imports ?? []) {
+ const importee = manifest[file]
+ if (seen.has(file)) {
+ continue
+ }
+ seen.add(file)
+
+ chunks.push(...getImportedChunks(importee))
+ chunks.push(importee)
+ }
+
+ return chunks
+ }
+
+ return getImportedChunks(manifest[name])
+ }
+ ```
+
+ :::