如何使用引用的节点模块中的样式表

问题描述 投票:0回答:1

我正在写一个VS Code模块。它使用highlight.js生成HTML,用于语法突出显示源代码。 highlight.js npm模块包含一个文件夹styles,其中包含我想要使用的CSS文件。

我该怎么做才能在运行时访问这些CSS文件?

我的意思是所有这些。

我怀疑在没有明确引用每个CSS文件的情况下,需要像require这样的东西来捆绑它们,但我需要有关如何执行此操作的指导。

另外如何访问捆绑的资源?

css npm visual-studio-code vscode-extensions
1个回答
0
投票

你不需要做任何事情。捆绑自动发生。部署时,项目的node_modules文件夹随附扩展名。

要访问这些模块中的资源,您需要知道它们的位置。答案是将它们部署到直接位于扩展路径上的node_modules文件夹中。

那么如何在运行时确定扩展文件夹的位置?您可以使用VSCode extensions location variable描述的方法,但我不推荐它。相反,这样做:

let x = vscode.extensions.getExtension("dilcorp.groovyext");
if (!x) {
  throw new Error("Cannot resolve extension. Has the name changed? " +
                  "It is defined by the publisher and the extension name " + 
                  "which are defined in package.json`); 
}
let stylePath = `${x.extensionPath}/node_modules/highlight.js/styles`;

上面的代码分为两部分。首先,我们获取有关扩展的运行时信息,其中包括其绝对文件路径作为属性extensionPath

第二部分利用了我们的扩展项目的node_modules被复制到extensionPath文件夹的事实。

这适用于调试器。

© www.soinside.com 2019 - 2024. All rights reserved.