OPEN TO WORK

Available for new opportunities! Let's build something amazing together.

Using Vite for bundling your Visual Studio Code extension

Bundling Development Extensions Visual Studio Code Vite
post

This post is over a year old, some of this information may be out of date.

A couple of weeks ago, I started looking into the possibility of retrieving the Content Collections from Astro and generating content-types for Front Matter CMS. Eventually, this got me into Vite, and I started to wonder if I could use it to bundle my Visual Studio Code extension to replace the current Webpack setup.

In this article, I will explain how I got it working.

Why Vite?

Vite is a build tool that focuses on speed and simplicity. I want to switch to Vite for the Front Matter CMS extension to speed up the builds for the three bundles I must create (extension and two webview bundles). The webpack setup takes 10-20 seconds before I can start debugging the extension. Vite should be able to do this in a couple of seconds.

Getting started

To start, create a new Visual Studio Code extension project with yo code. You can choose not to use the webpack setup; we will change the configuration anyway.

Show image Create a new VS Code extension
Create a new VS Code extension

Once the project is created, you can install Vite by running the npm i vite -D command.

Configuring Vite

The next step is to configure Vite. Create a new file called vite.config.ts in the root of your project and add the following configuration:

import { defineConfig } from "vite";
export default defineConfig({
build: {
lib: {
entry: "./src/extension.ts",
formats: ["cjs"],
fileName: "extension",
},
rollupOptions: {
external: ["vscode"],
},
sourcemap: true,
outDir: "out",
},
plugins: [],
});

This configuration will create a CommonJS bundle for the extension, like the current webpack setup. The rollupOptions exclude the vscode module from the bundle, as this is already available in the Visual Studio Code environment.

Updating the package.json

In the package.json file we can update the scripts section to use Vite.

{
"scripts": {
"compile": "vite build",
"watch": "vite build --watch",
"package": "vite build --mode production",
}
}

Time to test

Now that everything is in place, we can test if the extension still works. Run the npm run compile command to build the extension.

Show image Bundle the extension code with Vite
Bundle the extension code with Vite

To debug, I typically run the npm run watch command myself, giving me more flexibility in opening/closing the debugging extension. This flexibility requires a small change in the launch.json file.

  • Open the launch.json file
  • Remove the preLaunchTask from the configuration
  • Save the file

If you applied this change, you can now run npm run watch and press F5 to debug your extension.

Show image Test the extension with a Vite generated bundle
Test the extension with a Vite generated bundle

Related articles

Report issues or make changes on GitHub

Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.

Comments

Elio Struyf

Solutions Architect & Developer Expert

Loading...

Let's build together

Manage content in VS Code

Present from VS Code