简体   繁体   English

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

[英]How to use stylesheets from a referenced node module

I'm writing a VS Code module. 我正在编写VS Code模块。 It uses highlight.js to generate HTML for syntax highlighting of source code. 它使用highlight.js生成HTML以用于源代码的语法突出显示。 The highlight.js npm module contains a folder styles which is full of CSS files I would like to use. highlight.js npm模块包含一个文件夹styles ,其中包含我想使用的CSS文件。

What do I have to do so that I can access these CSS files at run-time? 我必须怎么做才能在运行时访问这些CSS文件?

I mean all of them . 我的意思是所有人

I suspect something like require will be needed to cause them to be bundled in the absence of an explicit reference to each CSS file, but I need guidance on how to do this. 我怀疑在没有显式引用每个CSS文件的情况下,将需要诸如require东西将它们捆绑在一起,但是我需要有关如何执行此操作的指南。

Also how does one access the bundled resources? 另外,如何访问捆绑的资源?

You don't have to do anything. 您不必做任何事情。 Bundling happens automatically. 捆绑自动发生。 Your project's node_modules folder accompanies the extension when it is deployed. 项目的node_modules文件夹在部署时会随扩展一起提供。

To access resources within these modules you need to know where they are. 要访问这些模块中的资源,您需要知道它们的位置。 The answer to that is they are deployed to a node_modules folder located directly on the extension path. 答案是将它们部署到直接位于扩展路径上的node_modules文件夹中。

So how do you determine the location of the extension folder at run-time? 那么如何在运行时确定扩展文件夹的位置? You could use the method described by VSCode extensions location variable but I don't recommend it. 您可以使用VSCode扩展位置变量描述的方法,但我不建议这样做。 Instead, do it like this: 而是这样做:

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`;

There are two parts to the above code. 上面的代码分为两部分。 First we get runtime information about the extension, which includes its absolute file path as a property extensionPath . 首先,我们获得有关扩展的运行时信息,其中包括其绝对文件路径作为属性extensionPath

The second part exploits the fact that our extension project's node_modules is copied into the extensionPath folder. 第二部分利用了将扩展项目的node_modules复制到extensionPath文件夹这一事实。

This works in the debugger. 这在调试器中有效。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM