Awesome
<div align="center"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://github.com/user-attachments/assets/99cb6303-64e4-4bed-bf3f-35735353e6de" /> <source media="(prefers-color-scheme: light)" srcset="https://github.com/user-attachments/assets/a5dbf71c-c509-4c4f-80f4-be88a1943b0a" /> <img alt="Logo" src="https://github.com/user-attachments/assets/99cb6303-64e4-4bed-bf3f-35735353e6de" /> </picture>
<img src="https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https://github.com/Jonghakseo/chrome-extension-boilerplate-react-viteFactions&count_bg=%23#222222&title_bg=%23#454545&title=😀&edge_flat=true" alt="hits"/> <a href="https://discord.gg/4ERQ6jgV9a" target="_blank"><img src="https://discord.com/api/guilds/1263404974830915637/widget.png"/></a>
</div>This boilerplate has Legacy version
[!NOTE] This project is listed in the Awesome Vite
[!TIP] Share storage state between all pages
https://github.com/user-attachments/assets/3b8e189f-6443-490e-a455-4f9570267f8c
Table of Contents
- Intro
- Features
- Structure
- Getting started
- Install dependency
- Community
- Reference
- Star History
- Contributors
Intro
This boilerplate helps you create Chrome/Firefox extensions using React and Typescript. It improves the build speed and development experience by using Vite and Turborepo.
Features
- React18
- TypeScript
- Tailwindcss
- Vite with Rollup
- Turborepo
- Prettier
- ESLint
- Chrome Extensions Manifest Version 3
- Custom i18n package
- Custom HMR (Hot Module Rebuild) plugin
- End-to-end testing with WebdriverIO
Getting started
-
When you're using Windows run this:
git config --global core.eol lf
git config --global core.autocrlf input
This will set the EOL (End of line) character to be the same as on Linux/macOS. Without this, our bash script won't work, and you will have conflicts with developers on Linux/macOS.
-
Clone this repository.
-
Edit
/packages/i18n/locales/
{your locale(s)}/messages.json
-
In the objects
extensionDescription
andextensionName
, change themessage
fields (leavedescription
alone) -
In
/.package.json
, change theversion
to the desired version of your extension. -
Install pnpm globally:
npm install -g pnpm
(check your node version >= 18.19.1)) -
Run
pnpm install
Then, depending on the target browser:
For Chrome: <a name="getting-started-chrome"></a>
- Run:
- Dev:
pnpm dev
(on Windows, you should run as administrator; see issue#456) - Prod:
pnpm build
- Dev:
- Open in browser -
chrome://extensions
- Check - <kbd>Developer mode</kbd>
- Click - <kbd>Load unpacked</kbd> in the upper left corner
- Select the
dist
directory from the boilerplate project
For Firefox: <a name="getting-started-firefox"></a>
- Run:
- Dev:
pnpm dev:firefox
- Prod:
pnpm build:firefox
- Dev:
- Open in browser -
about:debugging#/runtime/this-firefox
- Click - <kbd>Load Temporary Add-on...</kbd> in the upper right corner
- Select the
./dist/manifest.json
file from the boilerplate project
[!NOTE] In Firefox, you load add-ons in temporary mode. That means they'll disappear after each browser close. You have to load the add-on on every browser launch.
Install dependency for turborepo: <a name="install-dependency"></a>
For root: <a name="install-dependency-for-root"></a>
- Run
pnpm i <package> -w
For module: <a name="install-dependency-for-module"></a>
- Run
pnpm i <package> -F <module name>
package
- Name of the package you want to install e.g. nodemon
module-name
- You can find it inside each package.json
under the key name
, e.g. @extension/content-script
, you can use only content-script
without @extension/
prefix
Environment variables
To add an environment variable:
-
Copy
.example.env
to.env
(in the same directory) -
Add a new record inside
.env
, prefixed withVITE_
, e.g.VITE_MY_API_KEY=...
-
Edit
./vite-env.d.ts
and in theImportMetaEnv
interface, add your variable with the appropriate type, e.g.readonly VITE_MY_API_KEY: string;
-
Then you can read the variable via
import.meta.env.VITE_MY_API_KEY
(learn more at Env Variables and Modes)
If you want to set it for each package independently:
- Create
.env
inside that package - Open related
vite.config.mts
and addenvDir: '.'
at the end of this config - Rest steps like above
Remember you can't use global and local at the same time for the same package(It will be overwritten)
Boilerplate structure <a name="structure"></a>
Chrome extension <a name="structure-chrome-extension"></a>
The extension lives in the chrome-extension
directory and includes the following files:
manifest.js
- script that outputs themanifest.json
src/background
- background script (background.service_worker
in manifest.json)public
- icons referenced in the manifest; content CSS for user's page injection
[!IMPORTANT] To facilitate development, the boilerplate is configured to "Read and change all your data on all websites". In production, it's best practice to limit the premissions to only the strictly necessary websites. See Declaring permissions and edit
manifest.js
accordingly.
Pages <a name="structure-pages"></a>
Code that is transpiled to be part of the extension lives in the pages directory.
content
- content scripts (content_scripts
in manifest.json)content-ui
- React UI rendered in the current page (you can see it at the very bottom when you get started) (content_scripts
in manifest.json)content-runtime
- injected content scripts; this can be injected frompopup
like standardcontent
devtools
- extend the browser DevTools (devtools_page
in manifest.json)devtools-panel
- DevTools panel for devtoolsnew-tab
- override the default New Tab page (chrome_url_overrides.newtab
in manifest.json)options
- options page (options_page
in manifest.json)popup
- popup shown when clicking the extension in the toolbar (action.default_popup
in manifest.json)side-panel
- sidepanel (Chrome 114+) (side_panel.default_path
in manifest.json)
Packages <a name="structure-packages"></a>
Some shared packages:
dev-utils
- utilities for Chrome extension development (manifest-parser, logger)i18n
- custom internationalization package; provides i18n function with type safety and other validationhmr
- custom HMR plugin for Vite, injection script for reload/refresh, HMR dev-servershared
- shared code for the entire project (types, constants, custom hooks, components etc.)storage
- helpers for easier integration with storage, e.g. local/session storagestailwind-config
- shared Tailwind config for entire projecttsconfig
- shared tsconfig for the entire projectui
- function to merge your Tailwind config with the global one; you can save components herevite-config
- shared Vite config for the entire projectzipper
- runpnpm zip
to pack thedist
folder intoextension-YYYYMMDD-HHmmss.zip
inside the newly createddist-zip
e2e
- runpnpm e2e
for end-to-end tests of your zipped extension on different browsers
Troubleshooting
Hot module reload seems to have frozen
If saving source files doesn't cause the extension HMR code to trigger a reload of the browser page, try this:
- Ctrl+C the development server and restart it (
pnpm run dev
) - If you get a
grpc
error, kill theturbo
process and runpnpm dev
again.
Community
To chat with other community members, you can join the Discord server. You can ask questions on that server, and you can also help others.
Also, suggest new features or share any challenges you've faced while developing Chrome extensions!
Reference
Star History <a name="star-history"></a>
<a href="https://star-history.com/#Jonghakseo/chrome-extension-boilerplate-react-vite&Date"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/svg?repos=Jonghakseo/chrome-extension-boilerplate-react-vite&type=Date&theme=dark" /> <source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/svg?repos=Jonghakseo/chrome-extension-boilerplate-react-vite&type=Date" /> <img alt="Star History Chart" src="https://api.star-history.com/svg?repos=Jonghakseo/chrome-extension-boilerplate-react-vite&type=Date" /> </picture> </a>Contributors <a name="contributors"></a>
This Boilerplate is made possible thanks to all of its contributors.
<a href="https://github.com/Jonghakseo/chrome-extension-boilerplate-react-vite/graphs/contributors"> <img width="500px" src="https://contrib.rocks/image?repo=Jonghakseo/chrome-extension-boilerplate-react-vite" alt="All Contributors"/> </a>Special Thanks To
<a href="https://jb.gg/OpenSourceSupport"><img width="40" src="https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.png" alt="JetBrains Logo (Main) logo."></a> | <a href="https://www.linkedin.com/in/j-acks0n"><img width="40" style="border-radius:50%" src='https://avatars.githubusercontent.com/u/23139754' alt='Jackson Hong'/></a> |
---|
Made by Jonghakseo