11import { JSDOM } from 'jsdom'
22import * as path from 'path'
33import * as vscode from 'vscode'
4+ import onError from 'services/sentry/onError'
45
56const getNonce = ( ) :string => {
67let text = ''
@@ -12,74 +13,79 @@ const getNonce = (): string => {
1213}
1314
1415async function render ( panel :vscode . WebviewPanel , rootPath :string ) {
15- // load copied index.html from web app build
16- const dom = await JSDOM . fromFile ( path . join ( rootPath , 'index.html' ) )
17- const { document} = dom . window
16+ try {
17+ // load copied index.html from web app build
18+ const dom = await JSDOM . fromFile ( path . join ( rootPath , 'index.html' ) )
19+ const { document} = dom . window
1820
19- // set base href
20- const base :HTMLBaseElement = document . createElement ( 'base' )
21- base . href = `vscode-resource:${ rootPath } /`
21+ // set base href
22+ const base :HTMLBaseElement = document . createElement ( 'base' )
23+ base . href = `vscode-resource:${ rootPath } /`
2224
23- document . head . appendChild ( base )
25+ document . head . appendChild ( base )
2426
25- // used for CSP
26- const nonces :string [ ] = [ ]
27+ // used for CSP
28+ const nonces :string [ ] = [ ]
2729
28- // generate vscode-resource build path uri
29- const createUri = ( filePath :string ) :any => {
30- return panel . webview
31- . asWebviewUri ( vscode . Uri . file ( filePath ) )
32- . toString ( )
33- . replace ( / ^ \/ + / g, '' ) // remove leading '/'
34- . replace ( '/vscode-resource%3A' , rootPath ) // replace mangled resource path with root
35- }
30+ // generate vscode-resource build path uri
31+ const createUri = ( filePath :string ) :any => {
32+ return panel . webview
33+ . asWebviewUri ( vscode . Uri . file ( filePath ) )
34+ . toString ( )
35+ . replace ( / ^ \/ + / g, '' ) // remove leading '/'
36+ . replace ( '/vscode-resource%3A' , rootPath ) // replace mangled resource path with root
37+ }
3638
37- // fix paths for scripts
38- const scripts :HTMLScriptElement [ ] = Array . from ( document . getElementsByTagName ( 'script' ) )
39- for ( const script of scripts ) {
40- if ( script . src ) {
41- const nonce :string = getNonce ( )
42- nonces . push ( nonce )
43- script . nonce = nonce
44- script . src = createUri ( script . src )
39+ // fix paths for scripts
40+ const scripts :HTMLScriptElement [ ] = Array . from ( document . getElementsByTagName ( 'script' ) )
41+ for ( const script of scripts ) {
42+ if ( script . src ) {
43+ const nonce :string = getNonce ( )
44+ nonces . push ( nonce )
45+ script . nonce = nonce
46+ script . src = createUri ( script . src )
47+ }
4548}
46- }
4749
48- // add run-time script from webpack
49- const runTimeScript = document . createElement ( 'script' )
50- runTimeScript . nonce = getNonce ( )
51- nonces . push ( runTimeScript . nonce )
52- const manifest = await import ( path . join ( rootPath , 'asset-manifest.json' ) )
53- runTimeScript . src = createUri ( path . join ( rootPath , manifest . files [ 'runtime-main.js' ] ) )
54- document . body . appendChild ( runTimeScript )
50+ // add run-time script from webpack
51+ const runTimeScript = document . createElement ( 'script' )
52+ runTimeScript . nonce = getNonce ( )
53+ nonces . push ( runTimeScript . nonce )
54+ const manifest = await import ( path . join ( rootPath , 'asset-manifest.json' ) )
55+ runTimeScript . src = createUri ( path . join ( rootPath , manifest . files [ 'runtime-main.js' ] ) )
56+ document . body . appendChild ( runTimeScript )
5557
56- // fix paths for links
57- const styles :HTMLLinkElement [ ] = Array . from ( document . getElementsByTagName ( 'link' ) )
58- for ( const style of styles ) {
59- if ( style . href ) {
60- style . href = createUri ( style . href )
58+ // fix paths for links
59+ const styles :HTMLLinkElement [ ] = Array . from ( document . getElementsByTagName ( 'link' ) )
60+ for ( const style of styles ) {
61+ if ( style . href ) {
62+ style . href = createUri ( style . href )
63+ }
6164}
62- }
6365
64- // set CSP (content security policy) to grant permission to local files
65- const cspMeta :HTMLMetaElement = document . createElement ( 'meta' )
66- cspMeta . httpEquiv = 'Content-Security-Policy'
67- cspMeta . content =
68- [
69- `default-src 'self'` ,
70- `connect-src https: http:` ,
71- `font-src${ panel . webview . cspSource } http: https: data:` ,
72- `img-src${ panel . webview . cspSource } https:` ,
73- `script-src${ nonces . map ( nonce => `'nonce-${ nonce } '` ) . join ( ' ' ) } data:` ,
74- `style-src${ panel . webview . cspSource } https: 'self' 'unsafe-inline'` ,
75- ] . join ( '; ' ) + ';'
76- document . head . appendChild ( cspMeta )
66+ // set CSP (content security policy) to grant permission to local files
67+ const cspMeta :HTMLMetaElement = document . createElement ( 'meta' )
68+ cspMeta . httpEquiv = 'Content-Security-Policy'
69+ cspMeta . content =
70+ [
71+ `default-src 'self'` ,
72+ `connect-src https: http:` ,
73+ `font-src${ panel . webview . cspSource } http: https: data:` ,
74+ `img-src${ panel . webview . cspSource } https:` ,
75+ `script-src${ nonces . map ( nonce => `'nonce-${ nonce } '` ) . join ( ' ' ) } data:` ,
76+ `style-src${ panel . webview . cspSource } https: 'self' 'unsafe-inline'` ,
77+ ] . join ( '; ' ) + ';'
78+ document . head . appendChild ( cspMeta )
7779
78- // stringify dom
79- const html = dom . serialize ( )
80+ // stringify dom
81+ const html = dom . serialize ( )
8082
81- // set view
82- panel . webview . html = html
83+ // set view
84+ panel . webview . html = html
85+ } catch ( error ) {
86+ onError ( error )
87+ console . error ( error )
88+ }
8389}
8490
8591export default render