7.6 KiB
Executable File
Installation
- Clone this repository.(
git clone https://github.com/Jonghakseo/chrome-extension-boilerplate-react-vite) - Ensure your node version is >= than in
.nvmrcfile, recommend to use nvm - Edit
/packages/i18n/locales/{your locale(s)}/messages.json - In the objects
extensionDescriptionandextensionName, change themessagefields (leavedescriptionalone) - Install pnpm globally:
npm install -g pnpm - Run
pnpm install - Check if you have that configuration in your IDE/Editor:
- VS Code:
- Installed ESLint extension
- Installed Prettier extension
- Enabled
Typescript Workbench versionin settings:- CTRL + SHIFT + P -> Search:
Typescript: Select Typescript version...->Use Workbench version - Read more
- CTRL + SHIFT + P -> Search:
- Optional, for imports to work correctly in WSL, you might need to install the Remote - WSL extension and connect to WSL remotely from VS Code. See overview section in the extension page for more information.
- WebStorm:
- VS Code:
- Run
pnpm update-version <version>for change theversionto the desired version of your extension.
Important
On Windows, make sure you have WSL enabled and Linux distribution (e.g. Ubuntu) installed on WSL.
Then, depending on the target browser:
For Chrome:
- Run:
- Dev:
pnpm dev(on Windows, you should run as administrator; see issue#456) - Prod:
pnpm build
- Dev:
- Open in browser -
chrome://extensions - Check - Developer mode
- Click - Load unpacked in the upper left corner
- Select the
distdirectory from the boilerplate project
For Firefox:
- Run:
- Dev:
pnpm dev:firefox - Prod:
pnpm build:firefox
- Dev:
- Open in browser -
about:debugging#/runtime/this-firefox - Click - Load Temporary Add-on... in the upper right corner
- Select the
./dist/manifest.jsonfile 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:
For root:
- Run
pnpm i <package> -w
For module:
- 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
How do I disable modules I'm not using?
Environment variables
Read: Env Documentation
Boilerplate structure
Chrome extension
The extension lives in the chrome-extension directory and includes the following files:
manifest.ts- script that outputs themanifest.jsonsrc/background- background script (background.service_workerin 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.jsaccordingly.
Pages
Code that is transpiled to be part of the extension lives in the pages directory.
content- Scripts injected into specified pages (You can see it in console)content-ui- React Components injected into specified pages (You can see it at the very bottom of pages)content-runtime- injected content scripts This can be injected from e.g.popuplike standardcontentdevtools- extend the browser DevTools (devtools_pagein manifest.json)devtools-panel- DevTools panel for devtoolsnew-tab- override the default New Tab page (chrome_url_overrides.newtabin manifest.json)options- options page (options_pagein manifest.json)popup- popup shown when clicking the extension in the toolbar (action.default_popupin manifest.json)side-panel- sidepanel (Chrome 114+) (side_panel.default_pathin manifest.json)
Packages
Some shared packages:
dev-utils- utilities for Chrome extension development (manifest-parser, logger)env- exports object which contain all environment variables from.envand dynamically declaredhmr- custom HMR plugin for Vite, injection script for reload/refresh, HMR dev-serveri18n- custom internationalization package; provides i18n function with type safety and other validationshared- 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 project
Other useful packages:
zipper- runpnpm zipto pack thedistfolder intoextension-YYYYMMDD-HHmmss.zipinside the newly createddist-zipmodule-manager- runpnpm module-managerto enable/disable modulese2e- runpnpm e2efor end-to-end tests of your zipped extension on different browsers