Skip to content

Commit 8cc8914

Browse files
KJ7LNWEric Wheelerdaniel-lxs
authored
fix: resolve Mermaid CSP errors with enhanced bundling strategy (#3681)
Implements a more robust bundling strategy for Mermaid diagrams to prevent dynamic chunk loading at runtime, which was causing CSP violations. Key changes: - Added manualChunks function in vite.config.ts to consolidate all Mermaid code and dependencies into a single bundle - Added mermaid and dagre to optimizeDeps.include for pre-bundling - Customized chunkFileNames to ensure predictable output - Removed incorrect static imports in MermaidBlock.tsx This resolves the CSP errors where Mermaid was attempting to dynamically load chunks like chunk-RZ5BOZE2.js and channel.js from the webview origin. Fixes: #3680 Signed-off-by: Eric Wheeler <[email protected]> Co-authored-by: Eric Wheeler <[email protected]> Co-authored-by: Daniel <[email protected]>
1 parent 4befe5b commit 8cc8914

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

webview-ui/src/components/common/MermaidBlock.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { useEffect, useRef, useState } from "react"
22
import mermaid from "mermaid"
3-
import { useDebounceEffect } from "@src/utils/useDebounceEffect"
43
import styled from "styled-components"
4+
import { useDebounceEffect } from "@src/utils/useDebounceEffect"
55
import { vscode } from "@src/utils/vscode"
66
import { useAppTranslation } from "@src/i18n/TranslationContext"
77
import { useCopyToClipboard } from "@src/utils/clipboard"
88
import CodeBlock from "./CodeBlock"
99

10+
// Removed previous attempts at static imports for individual diagram types
11+
// as the paths were incorrect for Mermaid v11.4.1 and caused errors.
12+
// The primary strategy will now rely on Vite's bundling configuration.
13+
1014
const MERMAID_THEME = {
1115
background: "#1e1e1e", // VS Code dark theme background
1216
textColor: "#ffffff", // Main text color

webview-ui/vite.config.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,36 @@ export default defineConfig(({ mode }) => {
9898
rollupOptions: {
9999
output: {
100100
entryFileNames: `assets/[name].js`,
101-
chunkFileNames: `assets/[name].js`,
101+
chunkFileNames: (chunkInfo) => {
102+
if (chunkInfo.name === "mermaid-bundle") {
103+
return `assets/mermaid-bundle.js`
104+
}
105+
// Default naming for other chunks, ensuring uniqueness from entry
106+
return `assets/chunk-[hash].js`
107+
},
102108
assetFileNames: `assets/[name].[ext]`,
109+
manualChunks: (id, { getModuleInfo }) => {
110+
// Consolidate all mermaid code and its direct large dependencies (like dagre)
111+
// into a single chunk. The 'channel.js' error often points to dagre.
112+
if (
113+
id.includes("node_modules/mermaid") ||
114+
id.includes("node_modules/dagre") || // dagre is a common dep for graph layout
115+
id.includes("node_modules/cytoscape") // another potential graph lib
116+
// Add other known large mermaid dependencies if identified
117+
) {
118+
return "mermaid-bundle"
119+
}
120+
121+
// Check if the module is part of any explicitly defined mermaid-related dynamic import
122+
// This is a more advanced check if simple path matching isn't enough.
123+
const moduleInfo = getModuleInfo(id)
124+
if (moduleInfo?.importers.some((importer) => importer.includes("node_modules/mermaid"))) {
125+
return "mermaid-bundle"
126+
}
127+
if (moduleInfo?.dynamicImporters.some((importer) => importer.includes("node_modules/mermaid"))) {
128+
return "mermaid-bundle"
129+
}
130+
},
103131
},
104132
},
105133
},
@@ -116,6 +144,11 @@ export default defineConfig(({ mode }) => {
116144
},
117145
define,
118146
optimizeDeps: {
147+
include: [
148+
"mermaid",
149+
"dagre", // Explicitly include dagre for pre-bundling
150+
// Add other known large mermaid dependencies if identified
151+
],
119152
exclude: ["@vscode/codicons", "vscode-oniguruma", "shiki"],
120153
},
121154
assetsInclude: ["**/*.wasm", "**/*.wav"],

0 commit comments

Comments
 (0)