maxedison
maxedison

Reputation: 17553

Can an extension require other extensions and call functions from them?

I'm developing an extension for multiple languages. I'd like to have just a single core extension, and then make the code for each language a separate extension. Is it possible to do this? The core extension would essentially need to be able to recognize that the others are installed and call some of their code.

Upvotes: 4

Views: 1730

Answers (1)

Gama11
Gama11

Reputation: 34138

Yes, this should be possible via the extensions API - extensions can return an API from their activate() method:

export function activate(context: vscode.ExtensionContext) {
    let api = {
        sum(a, b) {
            return a + b;
        },
        mul(a, b) {
            return a * b;
        }
    };
    // 'export' public api-surface
    return api;
}

And another extension can then retrieve and use that API via a getExtension() call:

let mathExt = extensions.getExtension('genius.math');
let importedApi = mathExt.exports;

console.log(importedApi.mul(42, 1));

A list of all extensions known to VSCode is also available via extensions.all.

Upvotes: 8

Related Questions