简体   繁体   中英

vscode extension. 'JS Import' from webview

I'm write extension for vscode with webview as single web application. My project has the following structure: 在此处输入图片说明

In index html I want to import a script.

<script type="module">
import * as CommandHandler from '/core/CommandHadnler.js'
//some code
</script>

WebView setup localResourceRoots

const UI_PATH = path.join(context.extensionPath, 'UI');
this._panel = vscode.window.createWebviewPanel(
    'JSONRPCTester',
    'JSONRPC Tester',
    vscode.ViewColumn.Two,
    {
        enableScripts: true,
        localResourceRoots: [
           vscode.Uri.file(UI_PATH)
        ]
    } 
 );

I see that there is an attempt to load the module, but I get the error (Ignore RPC* ). 在此处输入图片说明

load url:

vscode-webview://a8f78cdc-7d22-4793-810f-67c2d10dfb67/core/ClientProxy.js

Probably it incorrect Content Security Policy. My policy setup:

<meta http-equiv="Content-Security-Policy" content="default-src self; img-src vscode-webview-resource:; script-src vscode-webview-resource: 'self' 'unsafe-inline'; style-src vscode-webview-resource: 'self' 'unsafe-inline'; ">

The path '/core/CommandHadnler.js' is invalid. You have to specify the full path of the JavaScript file on the local machine and convert it with Webview.asWebviewUri .

See https://code.visualstudio.com/api/extension-guides/webview#loading-local-content

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM