简体   繁体   English

如何在vs代码webview扩展开发中导入npm包?

[英]How to import npm packages in vs code webview extension development?

I want to use npm packages(latextoMathML) but when i use it on my webview it gives me error on running the extension that is: undefined refrence caught: package name(latextoXML) not defined.我想使用 npm 包(latextoMathML)但是当我在我的 webview 上使用它时,它在运行扩展程序时给了我错误:未定义的引用被捕获:ZEFE90A8E604A7C840E88D03AxtoXML7 未定义。 i have tried through let / var / const with require method.我已经尝试通过 let / var / const 使用 require 方法。 i wants to use in my js code under webview function.我想在 webview function 下的 js 代码中使用。

You cannot import the npm package into webview in the usual way, for example like this:您不能以通常的方式将 npm package 导入 webview,例如:

var somelibrary = require('somelibrary')

Instead, you can load the package as local resource.相反,您可以将 package 作为本地资源加载。

See the Webview API documentation and example code for a detailed explanation有关详细说明,请参阅 Webview API 文档示例代码

To do this, follow these minimum steps:为此,请按照以下最少步骤操作:

  • when creating a new panel, enable scripts创建新面板时,启用脚本
const panel = vscode.window.createWebviewPanel(
            'viewType',
            'view name',
            vscode.ViewColumn.One,
            {
                // Enable javascript in the webview
                enableScripts: true
            }
        );
  • wrap the npm package path in a special vscode uri.将 npm package 路径包装在一个特殊的 vscode uri 中。 This uri will actually contain this string: "vscode-resource://file///path-to-extension/node_modules/somelibrary/somelibrary.js"这个 uri 实际上会包含这个字符串:“vscode-resource://file///path-to-extension/node_modules/somelibrary/somelibrary.js”
const libraryPath = vscode.Uri.file(
    path.join(extensionPath, 'node_modules', 'somelibrary', 'somelibrary.js')
);

const scriptUri = webview.asWebviewUri(libraryPath);
  • pass path when creating html panel structure创建 html 面板结构时的传递路径
return '
...
<script src='$(scriptUri)'></script>
...
';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript:导入 npm 包 - JavaScript: import npm packages 如何用NPM软件包中的ES6导入替换CDN导入? - How to replace CDN import with ES6 import from NPM packages? 开发过程中 vs 代码扩展中的contributors.configuration 属性 - contributes.configuration property in vs code extension during development 在 flutter 移动应用 webview 中使用 npm 包 - Use npm packages in flutter mobile app webview 如何在用 TypeScript 编写的 VS Code 扩展中导入 ES6 JavaScript 模块? - How do I import an ES6 JavaScript module in my VS Code extension written in TypeScript? 如何导入NPM模块mark.js以在Chrome扩展程序中使用? - How to import npm module mark.js to use in chrome extension? 如何在webview中专注于div元素在android开发中使用代码 - how to focus on a div element in webview use code in android development 如何在 CDN 服务器上为 vanilla JavaScript 前端开发和生产构建捆绑 npm 包? - How to bundle npm packages for vanilla JavaScript frontend development and production builds on CDN servers? 如何创建树作为 Vs 代码的扩展 - How to create a tree as extension for Vs code 如何在没有TypeScript定义的情况下导入npm包,以便IDE仍提供intellisense? - How to import npm packages without TypeScript definition such that IDE still provide intellisense?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM