import * as path from 'path' ; import * as vscode from 'vscode' ; /**
 * The index.html file in the dist folder of the @salesforce/soql-builder-ui
 * is built by webpack to be displayed in a normal web context.  however, vscode
 * uses a custom protocol ( vscode-webview-resource instead of http ) to load resources
 * so the html needs to be manipulated dynamcially to run inside of vscode.
 */ export class HtmlUtils { /**
     * This regex will match tags in a string like this
     * <script defer="defer" src="./0.app.js"></script><script defer="defer" src="./app.js"></script>
     * And store just the filename section of the script tag as group[1]
     */ protected static readonly scriptRegex = /script defer="defer"\ssrc=\"\.\/(?<app>[^\"]*app.js)\"/g ; param html param pathToLwcDist param webview public static transformHtml ( html , pathToLwcDist , webview ) { html = HtmlUtils . transformScriptTags ( html , pathToLwcDist , webview ) ; html = HtmlUtils . replaceCspMetaTag ( html , webview ) ; return html ; } param html param pathToLwcDist param webview public static transformScriptTags ( html , pathToLwcDist , webview ) { let matches ; let newScriptSrc ; while ( ( matches = HtmlUtils . scriptRegex . exec ( html ) ) !== null ) { newScriptSrc = webview . asWebviewUri ( vscode . Uri . file ( path . join ( pathToLwcDist , matches [ 1 ] ) ) ) ; html = html . replace ( `./${ matches [ 1 ] }` , newScriptSrc . toString ( ) ) ; } return html ; } param html param webview public static replaceCspMetaTag ( html , webview ) { const cspMetaTag = `<meta
      http-equiv="Content-Security-Policy"
      content="default-src 'self';
      img-src ${ webview . cspSource };
      script-src ${ webview . cspSource };
      style-src 'unsafe-inline' ${ webview . cspSource };"
    />` ; html = html . replace ( '<!-- CSP TAG -->' , cspMetaTag ) ; return html ; } }