How to publish declaration files (.d.ts) for vscode extensions

472 views Asked by At

I wrote a vscode extension that exposes an API for use by other extensions (by having the activate() function return a value).

I would like to publish a scoped npm package that exposes a declaration file (.d.ts), that would assist developers of extensions to consume my extension.

My issue is that I cannot reuse the extension's package.json, because it doesn't allow for @ in the package's name (vsce package fails).

If I create a dedicated package.json for the purpose of publishing the declaration file to npm, I end up copying the .d.ts file and all its dependencies from the extension's out directory. This approach seems a bit cumbersome.

I couldn't find documentation that describes the proper way for doing this: publishing vscode extension declaration files to npm (scoped or not).

What is the correct approach to doing this?

1

There are 1 answers

0
Perspectivus On

This is what I ended up doing:

  1. I created another folder, that contains no source code, adjacent to the extension's folder.
  2. I placed a different package.json in it:
    1. I placed my package's scope in the name property and suffixed it with -types for clarity.
    2. I created a copy npm script that copies artifacts from the out folder of the extension's folder. This script uses ncp for cross platform support.
    3. I added @types/vscode to the dependencies property (not devDependencies, as noted in publishing declaration files) because my .d.ts file includes a reference to vscode.
  3. I created a .npmignore file to reinclude the out folder (using negate: !out) that was excluded in the .gitignore file.
  4. I needed to exclude .js files from the out folder to avoid errors related to missing transitive dependencies. I could have created another tsconfig.json file that emits only .d.ts files (using emitDeclarationOnly), but ended up using .npmignore to do this.

The above works for me, but there must be a better way, right??