All files / src webviewHost.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 3/3
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                  19x           18x       19x 19x     19x       19x                 7x    
import * as vscode from 'vscode';
import * as path from 'path';
import { buildPreviewHtml, getNonce } from './utils';
 
export function configureEditorWebview(
  context: vscode.ExtensionContext,
  panel: vscode.WebviewPanel,
  resourceUri: vscode.Uri,
) {
  panel.webview.options = {
    enableScripts: true,
    localResourceRoots: [
      vscode.Uri.file(path.join(context.extensionPath, 'dist')),
      vscode.Uri.file(path.join(context.extensionPath, 'dist', 'webview-assets')),
      vscode.Uri.file(path.dirname(resourceUri.fsPath)),
      ...(vscode.workspace.workspaceFolders?.map(folder => folder.uri) ?? []),
    ],
  };
 
  const webviewAssetsDir = path.join(context.extensionPath, 'dist', 'webview-assets');
  const scriptUri = panel.webview.asWebviewUri(
    vscode.Uri.file(path.join(webviewAssetsDir, 'webview.js'))
  );
  const cssUri = panel.webview.asWebviewUri(
    vscode.Uri.file(path.join(webviewAssetsDir, 'webview.css'))
  );
 
  panel.webview.html = buildPreviewHtml({
    cspSource: panel.webview.cspSource,
    nonce: getNonce(),
    scriptUri: scriptUri.toString(),
    cssUri: cssUri.toString(),
  });
}
 
export function getResourceBaseUri(panel: vscode.WebviewPanel, resourceUri: vscode.Uri) {
  return panel.webview.asWebviewUri(vscode.Uri.file(path.dirname(resourceUri.fsPath))).toString();
}