@@ -40,6 +40,7 @@ import { MessageRouter } from './MessageRouter';
4040import { initializeChatBackend } from './chatBackendInitializer' ;
4141import type { HandlerContext , DiffPreviewContentProvider as IDiffPreviewContentProvider } from './types' ;
4242import { isRecord , parseWebviewRequest } from './protocol' ;
43+ import { readFrontendBuildAssets , resolveFrontendAssetFsPath } from './utils' ;
4344
4445/**
4546 * Diff 预览内容提供者
@@ -455,15 +456,38 @@ export class ChatViewProvider implements vscode.WebviewViewProvider {
455456 */
456457 private getHtmlForWebview ( webview : vscode . Webview ) : string {
457458 const nonce = getNonce ( ) ;
458- const scriptUri = webview . asWebviewUri (
459- vscode . Uri . file ( path . join ( this . context . extensionPath , 'frontend' , 'dist' , 'index.js' ) )
460- ) ;
461- const styleUri = webview . asWebviewUri (
462- vscode . Uri . file ( path . join ( this . context . extensionPath , 'frontend' , 'dist' , 'index.css' ) )
463- ) ;
459+ const distDir = path . join ( this . context . extensionPath , 'frontend' , 'dist' ) ;
460+ const indexHtmlPath = path . join ( distDir , 'index.html' ) ;
461+ const fallbackScriptPath = path . join ( distDir , 'index.js' ) ;
462+ const fallbackStylePath = path . join ( distDir , 'index.css' ) ;
463+ let scriptUris = [ webview . asWebviewUri ( vscode . Uri . file ( fallbackScriptPath ) ) ] ;
464+ let styleUris = [ webview . asWebviewUri ( vscode . Uri . file ( fallbackStylePath ) ) ] ;
465+
466+ try {
467+ const assets = readFrontendBuildAssets ( indexHtmlPath ) ;
468+ if ( assets . scriptPaths . length > 0 ) {
469+ scriptUris = assets . scriptPaths . map ( assetPath =>
470+ webview . asWebviewUri ( vscode . Uri . file ( resolveFrontendAssetFsPath ( distDir , assetPath ) ) )
471+ ) ;
472+ }
473+ if ( assets . stylePaths . length > 0 ) {
474+ styleUris = assets . stylePaths . map ( assetPath =>
475+ webview . asWebviewUri ( vscode . Uri . file ( resolveFrontendAssetFsPath ( distDir , assetPath ) ) )
476+ ) ;
477+ }
478+ } catch ( error ) {
479+ console . warn ( 'Failed to resolve frontend build assets from dist/index.html, using fallback asset names.' , error ) ;
480+ }
481+
464482 const codiconsUri = webview . asWebviewUri (
465483 vscode . Uri . file ( path . join ( this . context . extensionPath , 'resources' , 'codicons' , 'codicon.css' ) )
466484 ) ;
485+ const styleLinks = styleUris
486+ . map ( styleUri => `<link href="${ styleUri } " rel="stylesheet">` )
487+ . join ( '\n ' ) ;
488+ const scriptTags = scriptUris
489+ . map ( scriptUri => `<script nonce="${ nonce } " src="${ scriptUri } "></script>` )
490+ . join ( '\n ' ) ;
467491
468492 return `<!DOCTYPE html>
469493<html lang="zh-CN">
@@ -472,12 +496,12 @@ export class ChatViewProvider implements vscode.WebviewViewProvider {
472496 <meta name="viewport" content="width=device-width, initial-scale=1.0">
473497 <meta http-equiv="Content-Security-Policy" content="default-src 'none'; style-src ${ webview . cspSource } 'unsafe-inline'; font-src ${ webview . cspSource } ; script-src 'nonce-${ nonce } '; img-src ${ webview . cspSource } data: blob:; media-src ${ webview . cspSource } data: blob:;">
474498 <link href="${ codiconsUri } " rel="stylesheet">
475- <link href=" ${ styleUri } " rel="stylesheet">
499+ ${ styleLinks }
476500 <title>Acopilot Chat</title>
477501</head>
478502<body>
479503 <div id="app"></div>
480- <script nonce=" ${ nonce } " src=" ${ scriptUri } "></script>
504+ ${ scriptTags }
481505</body>
482506</html>` ;
483507 }
0 commit comments