如何将文本文件打包成 vscode 扩展 [英] how package text files into vscode extension

查看:23
本文介绍了如何将文本文件打包成 vscode 扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的扩展程序使用把手将替换变量应用于文本模板.我可以在哪里存储扩展中的模板文本?

my extension is using handlebars to apply substitution variables to a text template. Where can I store the template text in the extension?

我目前将模板作为字符串存储在 extension.ts 文件中.宁愿使用存储模板文件的文件夹并将该文件夹包含在 vsce 包中.

I am currently storing the templates as strings in the extension.ts file. Would rather use a folder that stores the template files and include that folder in the vsce package.

推荐答案

要存储、打包和访问文件,您可以执行以下操作:

To store, package and access files, you can do the following:

  1. 在您的扩展根目录中创建一个子文件夹(与 package.json 相同级别),让我们为其命名resources.
  2. 在该文件夹中放置一个 file.txt
  3. 只要您不在 .vscodeignore 中列出该文件,该文件就会被打包(如 Gama11 所述)
  4. 使用 context.asAbsolutePath(...) API 访问它
  1. create a sub-folder in your extension root (same level as package.json), let's give it a name resources.
  2. Place a file.txt in that folder
  3. The file will get packaged as long as you do not list it in .vscodeignore (as mentioned by Gama11)
  4. Access it using the context.asAbsolutePath(...) API

示例:

import * as path from 'path';
import * as fs from 'fs';

export function activate(context: ExtensionContext) {
    let fullFilePath = context.asAbsolutePath(path.join('templates', 'file.txt'));
    fs.readFile(fullFilePath, (err, data) => { ... });
}

这篇关于如何将文本文件打包成 vscode 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆