A Bun template that contains the scaffolding for a VSCode extension.
- bun integration
- typescript support
- jest support for testing
- running your extension via a VSCode launch config (i.e. using
F5
) - placeholder extension metatdata in
package.json
bun create lalunamel/bun-vscode-extension my-extension
cd my-extension
code .
- Modify
package.json
with your information - Modify
LICENSE.txt
with the current year and your name - Start recording your work in
CHANGELOG.md
.vscode
- contains the config for vscodelaunch.json
- contains the config that enables running the extension withF5
tasks.json
- contains the config that defines the default build task, used inlaunch.json
dist
- contains the transpiled source filesextension.cjs
- the transpiled version ofsrc/extension.ts
. Usescjs
extension to conform to CommonJS (which is required for VSCode extensions)
mocks
- for testing. Contains mocked versions of librariesvscode.ts
- the mocked out version of the vscode library. I'm not quite sure what the structure of the normal library is, but I had a hell of a time getting it to work with bun and bun test. I think just the types are published, but the source code isn't, and then when your extension is built and bundled, the actual implementation of all the api calls used in your extension live within vscode itself. Quite complex, quite annoying. I'm sure I'm missing something and there's a better way but 🤷
scripts
build-with-esbuild.ts
- contains the logic to build the app with esbuild. Since esbuild does not use config files like the rest of the bundlers, this is the way to move all of that config into a file somewhere other thanpackage.json
. In order to get around the annoyances with thevscode
package not actually existing, thevscode
package is marked as "external" (and therefore not required to be resolvable).esbuild.config.ts
- contains the config for esbuild.watch-with-esbuild.ts
- same as the build script, but runs in continuous watch mode.
src
- contains the source filesextension.ts
- the extension file.extension.test.ts
- the test for the extension
The extension is located at src/extension.ts
.
The template's package.json
already has all the options configured to create a "Hello World" extension.
Write your tests at src/extension.test.ts
The tests there are unit tests, which is to say that all the dependencies of the subject under test are mocked out. They are mocked out by two means:
spyOn
from bun's testing packagemocks/vscode.ts
The first is normal spying/mocking as you'd find in any other test.
When writing tests for your extension, you must mock out any aspect of the vscode
package you're using in mocks/vscode.ts
so that those aspects are available to your implementation when the test runs.
The second is required because the vscode
package doesn't actually contain any implementation - only types (see notes on build-with-esbuild.ts
). The mocks
folder is hooked up by the paths.vscode
value in tsconfig.json
so that when you import * as vscode from "vscode"
, rather than looking in node_modules
for vscode
, the resolver looks in ./mocks/vscode.ts
.
The mocks are only used when running tests and not when building to ./dist
. That is because vscode
is marked as "external` in the esbuild config.
This second part is required so that when running tests, import * as vscode from "vscode"
in the implementation file actually returns something (the something it returns is determined by the contents of ./mocks/vscode.ts
).
To run the extension, simply press F5
or go to Run and Debug > Run Extension
. A new VSCode instance will be started and your extension will be loaded in there. You can console.log
and set breakpoints, too!
Once the VSCode instance has loaded the extension, open the command pallette with Cmd + Shift + P
and type hello world
to find the command added by the extension.
Packaging a VSCode extension means turning it all into a single .vsix
file that can be installed manually by Right Click > Install extension VSIX
in VSCode.
To package your extension, bun run package
.
- This template does not use mocha as described in the VSCode - Testing Extensions docs
- It also does not use
vscode-test
like the Yeoman generator does - It uses
esbuild
rather than webpack
I generally found that the Yeoman template uses older technologies like mocha and webpack that don't play well with newer tooling, and so I did my best to replicate and update the functionality present in the Yeoman generator.
Activating extension 'undefined_publisher.bun-vscode-extension' failed: require() of ES Module dist/extension.js from /Applications/Visual Studio Code.app/Contents/Resources/app/out/vs/loader.js not supported. extension.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules. Instead rename extension.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
This error is produced because VSCode can't load your extension. This is probably happening because the file you're tyring to load is using ESM instead of CommonJS. You can fix this by fiddling with the settings in build-with-esbuild.ts
.