When you bundle resources (asset/resource
) with Webpack 5 the files end up placed relative to the publicPath
you specify in webpack.config.js
In my case that produces {workspace}/dist/3fc1e0720620d224139a.jpg
in the debugger, and {extension folder}/dist/3fc1e0720620d224139a.jpg
for an installed extension.
In extension code (Typescript, of course) we resolve the original name to the bundled name using require(originalName)
and it duly returns dist/3fc1e0720620d224139a.jpg
The problem is I don't see how to write an expression that resolves to the folder containing this extension for both the debug host and the deployed extension.
CodePudding user response:
It appears this information is available from the ExtensionContext
passed to the activate method. The extension root can be captured in the activate method and made available to other code.
However, if you need it in some library code it can be a pain to pass it. My solution was to define a Metadata
class with a static ExtensionContext
property, set the static ExtensionContext
property in the activate
method and import the class wherever I need the ExtensionContext
.
If you need it before activate is called, for example while initialising a logger, you can get it like so
const extensionId = "pdconsec.vscode-print";
const extension = vscode.extensions.getExtension(extensionId)!;
const extensionPath = extension.extensionPath;