vscode 扩展名。从 web 视图中“JS 导入”

Kop*_*niy 5 javascript visual-studio-code vscode-extensions

我正在为 vscode 编写扩展,将 webview 作为单个 Web 应用程序。我的项目具有以下结构: 在此输入图像描述

在索引 html 中我想导入一个脚本。

<script type="module">
import * as CommandHandler from '/core/CommandHadnler.js'
//some code
</script>
Run Code Online (Sandbox Code Playgroud)

WebView设置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)
        ]
    } 
 );
Run Code Online (Sandbox Code Playgroud)

我看到有人尝试加载模块,但收到错误(忽略 RPC* )。 在此输入图像描述

加载网址:

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

可能是内容安全策略不正确。我的政策设置:

<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'; ">
Run Code Online (Sandbox Code Playgroud)

小智 2

该路径'/core/CommandHadnler.js'无效。您必须指定本地计算机上 JavaScript 文件的完整路径并将其转换为Webview.asWebviewUri.

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