在 Visual Studio 代码扩展开发中使用 HTML 个文件
Use HTML files in Visual Studio Code extension development
我想知道是否可以在单独的文件中使用 HTML、CSS 和 JavaScript 以在 Visual Studio 代码中创建扩展。
我正在使用 VS Code Webview API。我试图创建一个文件夹并放入我的 HTML 和 CSS 文件,但它没有用。我现在不知道如何在扩展 GUI 上使用这些文件。我什至尝试使用 NodeJS readFile()
来读取 HTML 文件,但它无法读取文件。使用 ./html/file...
的相对路径无效。
我错过了什么?我是 JS、Node 和 VS Code 的新手...
在文档示例中,他们将 HTML 放在这样的字符串中:
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('catCoding.start',
() => {
// Create and show panel
const panel = vscode.window.createWebviewPanel('catCoding', "Cat Coding",
vscode.ViewColumn.One, { });
// And set its HTML content
panel.webview.html = getWebviewContent();
}));
}
function getWebviewContent() {
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cat Coding</title>
</head>
<body>
<img src="https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif" width="300" />
</body>
</html>`;
}
是否可以将 HTML 或至少 JS 和 CSS 放在单独的文件中?这样可以使用库并获得更清晰的代码。
Link 到文档:VS Code Webview API
谢谢!
I even tried to use NodeJS readFile() to read the HTML file but it failed to read the file. Relative path using ./html/file...
didn't work.
事先尝试使用 context.asAbsolutePath()
method。它根据扩展的根目录解析相对路径,而不是当前工作目录(即 VSCode 安装目录)。
文件夹 src/html/
中文件的工作方式是:
import * as path from 'path'
import * as fs from 'fs'
const filePath: vscode.Uri = vscode.Uri.file(path.join(context.extensionPath, 'src', 'html', 'file.html'));
panel.webview.html = fs.readFileSync(filePath.fsPath, 'utf8');
您可以进行如下操作:
import * as fs from 'fs';
const pathToHtml = vscode.Uri.file(
path.join(context.extensionPath, 'src','index.html')
);
const pathUri = pathToHtml.with({scheme: 'vscode-resource'});
panel.webview.html = fs.readFileSync(pathUri.fsPath,'utf8');
此处 context.extensionPath
包含您的扩展程序的目录。 src
是包含您的 html 文件的扩展内的文件夹。
我想知道是否可以在单独的文件中使用 HTML、CSS 和 JavaScript 以在 Visual Studio 代码中创建扩展。
我正在使用 VS Code Webview API。我试图创建一个文件夹并放入我的 HTML 和 CSS 文件,但它没有用。我现在不知道如何在扩展 GUI 上使用这些文件。我什至尝试使用 NodeJS readFile()
来读取 HTML 文件,但它无法读取文件。使用 ./html/file...
的相对路径无效。
我错过了什么?我是 JS、Node 和 VS Code 的新手...
在文档示例中,他们将 HTML 放在这样的字符串中:
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('catCoding.start',
() => {
// Create and show panel
const panel = vscode.window.createWebviewPanel('catCoding', "Cat Coding",
vscode.ViewColumn.One, { });
// And set its HTML content
panel.webview.html = getWebviewContent();
}));
}
function getWebviewContent() {
return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cat Coding</title>
</head>
<body>
<img src="https://media.giphy.com/media/JIX9t2j0ZTN9S/giphy.gif" width="300" />
</body>
</html>`;
}
是否可以将 HTML 或至少 JS 和 CSS 放在单独的文件中?这样可以使用库并获得更清晰的代码。
Link 到文档:VS Code Webview API
谢谢!
I even tried to use NodeJS readFile() to read the HTML file but it failed to read the file. Relative path using
./html/file...
didn't work.
事先尝试使用 context.asAbsolutePath()
method。它根据扩展的根目录解析相对路径,而不是当前工作目录(即 VSCode 安装目录)。
文件夹 src/html/
中文件的工作方式是:
import * as path from 'path'
import * as fs from 'fs'
const filePath: vscode.Uri = vscode.Uri.file(path.join(context.extensionPath, 'src', 'html', 'file.html'));
panel.webview.html = fs.readFileSync(filePath.fsPath, 'utf8');
您可以进行如下操作:
import * as fs from 'fs';
const pathToHtml = vscode.Uri.file(
path.join(context.extensionPath, 'src','index.html')
);
const pathUri = pathToHtml.with({scheme: 'vscode-resource'});
panel.webview.html = fs.readFileSync(pathUri.fsPath,'utf8');
此处 context.extensionPath
包含您的扩展程序的目录。 src
是包含您的 html 文件的扩展内的文件夹。