This boilerplate is meant to be a quick start for creating chrome extensions using React, Typescript and Tailwind CSS.

Overview
logo

Chrome Extension Boilerplate with
React + Vite + TypeScript + TailwindCSS

Table of Contents

Intro

This boilerplate is meant to be a quick start for creating chrome extensions using React, Typescript and Tailwind CSS.

For improved DX and rapid building vite and nodemon are used.

Chrome does not accept manifest v2 extensions since Jan 2022, therefore this template uses manifest v3.

Firefox + other browsers don't yet support manifest v3, so cross browser usage is not encouraged.

  • Read more about Chrome manifest v2 support here.
  • Read more about Firefox Manifest v3 support here.

As soon as Firefox supports manifest v3, support will be added in this repo as well.

Why another boilerplate?

I have used webpack react boilerplates and found it too hard to configure.

Vite is actually mega easy to understand, which makes it easier to get into and to maintain for others.

I couldn't find a boilerplate for React, TypeScript and Tailwind CSS. So here it is.

Features

Usage

Setup

  1. Clone this repository.
  2. Change name and description in package.json => Auto synchronize with manifest
  3. Run yarn or npm i (check your node version >= 16)
  4. Run yarn dev or npm run dev
  5. Load Extension on Chrome
    1. Open - Chrome browser
    2. Access - chrome://extensions
    3. Check - Developer mode
    4. Find - Load unpacked extension
    5. Select - dist folder in this project (after dev or build)
  6. If you want to build in production, Just run yarn build or npm run build.

Customization

As the template has all of the potential Chrome extension pages implemented, you likely have to customize it to fit your needs.

E.g. you don't want the newtab page to activate whenever you open a new tab:

  1. remove the directory newtab and its contents in src/pages
  2. remove the newtab rollup input in the vite.config.ts
//...
build: {
    outDir,
    rollupOptions: {
      input: {
        devtools: resolve(pagesDir, 'devtools', 'index.html'),
        panel: resolve(pagesDir, 'panel', 'index.html'),
        content: resolve(pagesDir, 'content', 'index.ts'),
        background: resolve(pagesDir, 'background', 'index.ts'),
        popup: resolve(pagesDir, 'popup', 'index.html'),
        newtab: resolve(pagesDir, 'newtab', 'index.html'),  // <--- REMOVE THIS LINE
        options: resolve(pagesDir, 'options', 'index.html'),
      },
      output: {
        entryFileNames: (chunk) => `src/pages/${chunk.name}/index.js`,
      },
    },
  },
/...

CSS files in the src/pages/* directories are not necessary. They are left in there in case you want to use it in combination with Tailwind CSS. Feel free to delete them.

Tailwind can be configured as usual in the tailwind.config.cjs file. See doc link below.

Tech Docs

Credit

Heavily inspired by Jonghakseo's vite chrome extension boilerplate. It uses SASS instead of TailwindCSS if you want to check it out.

Contributing

Feel free to open PRs or raise issues!

Comments
  • Dev server create not wanted file

    Dev server create not wanted file

    Nice template =)

    Exactly what i want. I try to use it, but that is a strange behavior when dev server is running.

    If you update vite.config.js that create a temporary file like this vite.config.ts.timestamp-1665005464976.js

    How to reproduce

    • npm run dev
    • make changes inside vite.config.js
    • after the reload on dev server, you have a timestamped file at the root of the app.
    opened by Horsty80 14
  • Content Script cannot use React

    Content Script cannot use React

    • Hi, I have the content script that will create a div element and use React render to that div then append it into the document.body, but when content script loaded it show this error Cannot use import statement outside a module: image
    • My content script code: image image
    opened by dim0147 4
  • Renaming the extension ts to js

    Renaming the extension ts to js

    So I have to change the extension file of my content.ts to content js. The problem is, when I build my project the index js in dist/src/page/content is empty

    opened by moshOntong-IT 2
  • Is it possible to make popup

    Is it possible to make popup "pop out"?

    You can see this behavior with Bitwarden's plugin here. So I'm wondering how I can implement the same feature. Sorry, I'm not very familiar with Vite and configuring chrome extensions ☹️

    opened by henrychea 1
  • Cannot use import in content script

    Cannot use import in content script

    Im getting "Uncaught SyntaxError: Cannot use import statement outside a module" When trying to use import within a content script.

    https://stackoverflow.com/questions/48104433/how-to-import-es6-modules-in-content-script-for-chrome-extension

    This stackoverflow post here explains it can get solved by wrapping it with