1- import * as fs from 'node:fs'
2- import * as path from 'node:path'
31import * as vscode from 'vscode'
42import { getNonce } from '../../utils/webview'
53
64const DEV_WEBVIEW_URL = 'http://localhost:5173'
7- const DEV_TIMEOUT = 3000 // 3 seconds timeout for dev server connection
85
96/**
107 * Generates the HTML content for the webview, choosing between dev and prod.
@@ -26,14 +23,6 @@ export function getHtmlForWebview(
2623function getDevHtml ( webview : vscode . Webview , extensionUri : vscode . Uri ) : string {
2724 const nonce = getNonce ( )
2825
29- // Check if we have built webview assets for fallback
30- const webviewAssetsDir = vscode . Uri . joinPath (
31- extensionUri ,
32- 'dist' ,
33- 'webview-ui' ,
34- )
35- const hasBuiltAssets = checkIfAssetsExist ( webviewAssetsDir )
36-
3726 // Path to codicons from the extension's node_modules
3827 const codiconUri = webview . asWebviewUri (
3928 vscode . Uri . joinPath (
@@ -46,116 +35,10 @@ function getDevHtml(webview: vscode.Webview, extensionUri: vscode.Uri): string {
4635 ) ,
4736 )
4837
49- if ( hasBuiltAssets ) {
50- // Use built assets with development features
51- return getDevHtmlWithBuiltAssets ( webview , extensionUri , nonce , codiconUri )
52- }
53-
54- // Use dev server (original behavior)
38+ // Use dev server (original behavior) - prefer dev server for development
5539 return getDevHtmlWithServer ( webview , extensionUri , nonce , codiconUri )
5640}
5741
58- /**
59- * Check if built webview assets exist and are valid.
60- */
61- function checkIfAssetsExist ( assetsDir : vscode . Uri ) : boolean {
62- try {
63- const assetsPath = assetsDir . fsPath
64-
65- // Check if directory exists
66- if ( ! fs . existsSync ( assetsPath ) ) {
67- return false
68- }
69-
70- // Check for required files
71- const requiredFiles = [
72- path . join ( assetsPath , 'assets' , 'index.js' ) ,
73- path . join ( assetsPath , 'assets' , 'index.css' ) ,
74- path . join ( assetsPath , 'assets' , 'codicon.css' ) ,
75- ]
76-
77- return requiredFiles . every ( ( file ) => fs . existsSync ( file ) )
78- } catch {
79- return false
80- }
81- }
82-
83- /**
84- * Development HTML using built assets with hot reload capabilities.
85- */
86- function getDevHtmlWithBuiltAssets (
87- webview : vscode . Webview ,
88- extensionUri : vscode . Uri ,
89- nonce : string ,
90- codiconUri : vscode . Uri ,
91- ) : string {
92- const cspSource = webview . cspSource
93-
94- // Paths to built assets
95- const scriptUri = webview . asWebviewUri (
96- vscode . Uri . joinPath (
97- extensionUri ,
98- 'dist' ,
99- 'webview-ui' ,
100- 'assets' ,
101- 'index.js' ,
102- ) ,
103- )
104- const styleUri = webview . asWebviewUri (
105- vscode . Uri . joinPath (
106- extensionUri ,
107- 'dist' ,
108- 'webview-ui' ,
109- 'assets' ,
110- 'index.css' ,
111- ) ,
112- )
113-
114- // CSP for development with built assets
115- const csp = [
116- `default-src 'none'` ,
117- `font-src ${ cspSource } data:` ,
118- `connect-src ws://localhost:5173 ${ cspSource } ` , // Allow WebSocket for HMR
119- `img-src ${ cspSource } https: data:` ,
120- `script-src 'nonce-${ nonce } ' 'unsafe-eval' 'unsafe-inline'` , // Allow eval for HMR
121- `style-src ${ cspSource } 'unsafe-inline'` ,
122- ] . join ( '; ' )
123-
124- return `<!DOCTYPE html>
125- <html lang="en">
126- <head>
127- <meta charset="UTF-8">
128- <meta name="viewport" content="width=device-width, initial-scale=1.0">
129- <link href="${ codiconUri } " rel="stylesheet" id="vscode-codicon-stylesheet" />
130- <meta http-equiv="Content-Security-Policy" content="${ csp } ">
131- <link rel="stylesheet" type="text/css" href="${ styleUri } ">
132- <title>Overwrite (Dev)</title>
133- </head>
134- <body>
135- <div id="root"></div>
136- <script type="module" nonce="${ nonce } " src="${ scriptUri } "></script>
137- <script nonce="${ nonce } ">
138- // Pass vscode API and nonce to the webview
139- window.nonce = "${ nonce } "
140- window.vscodeApi = acquireVsCodeApi()
141-
142- // Attempt to connect to Vite dev server for HMR
143- setTimeout(() => {
144- try {
145- const script = document.createElement('script')
146- script.type = 'module'
147- script.src = '${ DEV_WEBVIEW_URL } /@vite/client'
148- document.head.appendChild(script)
149- console.log('[HMR] Attempting to connect to Vite dev server...')
150- } catch (err) {
151- console.log('[HMR] Could not connect to Vite dev server, using built assets')
152- }
153- }, ${ DEV_TIMEOUT } )
154- </script>
155- </body>
156- </html>`
157- }
158-
15942/**
16043 * Development HTML using Vite dev server (original behavior).
16144 */
0 commit comments