Create a Vite-React Chrome Extension in 90 seconds
Tags: vite, react, tutorial, webdev
Published: 2021-12-15 22:41:00

The bundler configuration for a Chrome Extension can be pretty complex. When I started making Chrome Extensions, they were small projects for clients who wanted to automate a task. I was starting a new Chrome Extension almost every week, and it took too much time to configure a new project. Then I thought, "We already have a manifest; why do we need more config files?"

That's the idea behind CRXJS Vite Plugin. My name is Jack Steam, and I've been hard at work over the creating a Vite plugin for Chrome Extensions. So today, I'm excited to invite you to try out CRXJS with Vite.

CRXJS makes it easy to set up a Chrome Extension project with a modern development experience. How easy? Let me show you.

## Create your project

Use your favorite package manager to scaffold a new project and follow the prompts to create a React project.

> CRXJS doesn't yet work with Vite 3, but support is coming soon!

```sh
npm init vite@^2
```

## Install CRXJS Vite Plugin

Now install the latest release of CRXJS using your favorite package manager.

```sh
npm i @crxjs/vite-plugin -D
```

## Update the Vite config

Update `vite.config.js` to match the code below.

```js
// vite.config.js

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import { crx } from "@crxjs/vite-plugin";
// Node 14 & 16
import manifest from './manifest.json'
// Node >=17
import manifest from './manifest.json' assert { type: 'json' } 

export default defineConfig({
  plugins: [react(), crx({ manifest })],
});
```
 
Create a file named `manifest.json` next to `vite.config.js`.

```jsonc
// manifest.json
{
  "manifest_version": 3,
  "name": "CRXJS React Vite Example",
  "version": "1.0.0",
  "action": { "default_popup": "index.html" }
}
```

And run the dev command.

```sh
npm run dev
```

That's it! CRXJS will do the rest. Your project directory should look like this:

![CRXJS File Structure](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n593fj1mc4w5iyjb5191.png)

## Add the extension to Chrome

Let's try it out.

When the build completes, open Chrome or Edge and navigate to `chrome://extensions`. Make sure to turn on the developer mode switch.

| Chrome | Edge |
|---------|------|
| Top right corner | Middle left sidebar |
| ![Chrome developer mode switch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d70yn4lt9zb20hye2p4h.png) | ![Edge developer mode switch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/engbzpm86ejapusdvzdr.png) |

Drag your `dist` folder into the Extensions Dashboard to install it. Your extension icon will be in the top bar. The icon will be the first letter of the extension's name. 

![Chrome Extension Action Context Menu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxib8tx0vnhps4ey47us.png) 

## Profit with Vite HMR

Once you've found the extension icon, right-click it and choose "Inspect popup window". This will open the popup and the popup dev tools window. We need to inspect the popup to keep it open while making changes.

![Popup without min-width](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkpb4jfgeyx8uyideo73.png)

That popup is pretty tiny; let's add some CSS to make it wider.

```css
/* App.css */
.App {
  text-align: center;
+ min-width: 350px;
}
```

And boom! HMR magic at work.

![Popup with min-width](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7d36jlxkwitdag7bebi.png)

If you enjoyed this how-to guide, check out [CRXJS on GitHub](https://github.com/crxjs/rollup-plugin-chrome-extension) and give us a star!