Convert Figma logo to code with AI

lxieyang logochrome-extension-boilerplate-react

A Chrome Extensions boilerplate using React 18 and Webpack 5.

3,547
1,095
3,547
73

Top Related Projects

Scaffold out a Chrome extension

Boilerplate for Chrome Extension React.js project

A basic foundation boilerplate for rich Chrome Extensions using Webpack to help you write modular and modern Javascript code, load CSS easily and automatic reload the browser on code changes.

🖥🔋Web Extension starter to build "Write Once Run on Any Browser" extension

Quick Overview

The lxieyang/chrome-extension-boilerplate-react is a modern boilerplate for creating Chrome extensions using React. It provides a well-structured starting point for developers to build feature-rich extensions with a familiar React ecosystem, including hot-reloading and the latest JavaScript features.

Pros

  • Utilizes React for building user interfaces, allowing for component-based development
  • Includes hot-reloading for faster development and debugging
  • Supports modern JavaScript features through Babel and Webpack
  • Provides a well-organized project structure for easier maintenance

Cons

  • May have a steeper learning curve for developers not familiar with React
  • Potentially larger bundle size compared to vanilla JavaScript extensions
  • Limited to Chrome browser, not cross-browser compatible out of the box
  • Requires additional setup and configuration compared to simpler extension templates

Getting Started

  1. Clone the repository:

    git clone https://github.com/lxieyang/chrome-extension-boilerplate-react.git
    
  2. Install dependencies:

    cd chrome-extension-boilerplate-react
    npm install
    
  3. Start the development server:

    npm start
    
  4. Load the extension in Chrome:

    • Open Chrome and navigate to chrome://extensions
    • Enable "Developer mode"
    • Click "Load unpacked" and select the build folder in the project directory
  5. Begin development by modifying files in the src directory. The extension will automatically reload with your changes.

Competitor Comparisons

Scaffold out a Chrome extension

Pros of generator-chrome-extension

  • Offers more flexibility and customization options through Yeoman's scaffolding system
  • Supports a wider range of Chrome extension types and configurations
  • Provides a more established and mature ecosystem with Yeoman

Cons of generator-chrome-extension

  • Less focused on React-specific development compared to chrome-extension-boilerplate-react
  • May require more setup and configuration for React-based projects
  • Potentially steeper learning curve for developers new to Yeoman

Code Comparison

generator-chrome-extension:

module.exports = class extends Generator {
  prompting() {
    return this.prompt([
      {
        type: 'input',
        name: 'name',
        message: 'Your extension name'
      }
    ]).then(answers => {
      this.answers = answers;
    });
  }
}

chrome-extension-boilerplate-react:

const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

The code snippets highlight the different approaches: generator-chrome-extension uses Yeoman's generator system, while chrome-extension-boilerplate-react focuses on webpack configuration for React-based extensions.

Boilerplate for Chrome Extension React.js project

Pros of react-chrome-extension-boilerplate

  • Includes Redux for state management out of the box
  • Supports hot reloading for faster development
  • Provides a more comprehensive example with multiple pages and components

Cons of react-chrome-extension-boilerplate

  • Less frequently updated (last update was 3 years ago)
  • Uses older versions of React and other dependencies
  • More complex setup, which may be overwhelming for beginners

Code Comparison

chrome-extension-boilerplate-react:

import React from 'react';
import { render } from 'react-dom';

import Popup from './Popup';
import './index.css';

render(<Popup />, document.getElementById('root'));

react-chrome-extension-boilerplate:

import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import App from './containers/App'
import configureStore from './store/configureStore'

const store = configureStore()

render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.querySelector('#root')
)

The main difference in the code is that react-chrome-extension-boilerplate includes Redux setup, while chrome-extension-boilerplate-react has a simpler entry point. This reflects the overall complexity difference between the two boilerplates.

A basic foundation boilerplate for rich Chrome Extensions using Webpack to help you write modular and modern Javascript code, load CSS easily and automatic reload the browser on code changes.

Pros of chrome-extension-webpack-boilerplate

  • Simpler setup with fewer dependencies, making it easier to understand and maintain
  • More lightweight, potentially resulting in faster build times
  • Provides a basic webpack configuration that can be easily customized

Cons of chrome-extension-webpack-boilerplate

  • Lacks React integration, which may be preferred for more complex UIs
  • Doesn't include hot reloading, requiring manual reloads during development
  • Less comprehensive documentation and examples compared to chrome-extension-boilerplate-react

Code Comparison

chrome-extension-webpack-boilerplate:

module.exports = {
  entry: {
    popup: path.join(__dirname, "src", "js", "popup.js"),
    options: path.join(__dirname, "src", "js", "options.js"),
    background: path.join(__dirname, "src", "js", "background.js")
  },
  output: {
    path: path.join(__dirname, "build"),
    filename: "[name].bundle.js"
  },
  // ...
};

chrome-extension-boilerplate-react:

const config = {
  entry: {
    popup: path.join(__dirname, 'src', 'pages', 'Popup', 'index.jsx'),
    options: path.join(__dirname, 'src', 'pages', 'Options', 'index.jsx'),
    background: path.join(__dirname, 'src', 'pages', 'Background', 'index.js'),
    contentScript: path.join(__dirname, 'src', 'pages', 'Content', 'index.js'),
  },
  // ...
};

The code comparison shows that chrome-extension-boilerplate-react has a more structured approach with separate directories for different components, while chrome-extension-webpack-boilerplate uses a simpler file structure.

🖥🔋Web Extension starter to build "Write Once Run on Any Browser" extension

Pros of web-extension-starter

  • Supports multiple browsers (Chrome, Firefox, Opera, Edge) out of the box
  • Includes TypeScript configuration for better type safety and developer experience
  • Provides a more comprehensive project structure with separate directories for background, content, and options scripts

Cons of web-extension-starter

  • Less active maintenance and fewer recent updates compared to chrome-extension-boilerplate-react
  • Slightly more complex setup and configuration due to multi-browser support
  • Fewer stars and community engagement on GitHub

Code Comparison

web-extension-starter (TypeScript):

import Browser from 'webextension-polyfill';

export async function getCurrentTab() {
  const [tab] = await Browser.tabs.query({ active: true, currentWindow: true });
  return tab;
}

chrome-extension-boilerplate-react (JavaScript):

/*global chrome*/
export const getCurrentTab = async () => {
  const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
  return tab;
};

The main difference in the code snippets is the use of the webextension-polyfill library in web-extension-starter, which provides cross-browser compatibility. chrome-extension-boilerplate-react uses the Chrome-specific API directly.

Both projects offer solid foundations for building browser extensions, with web-extension-starter focusing on multi-browser support and TypeScript, while chrome-extension-boilerplate-react provides a simpler React-based setup primarily for Chrome extensions.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Chrome Extension (MV3) Boilerplate with React 18 and Webpack 5

npm npm-download npm

Announcements

Features

This is a basic Chrome Extensions boilerplate to help you write modular and modern Javascript code, load CSS easily and automatic reload the browser on code changes.

This boilerplate is updated with:

This boilerplate is heavily inspired by and adapted from https://github.com/samuelsimoes/chrome-extension-webpack-boilerplate, with additional support for React 18 features, Webpack 5, and Webpack Dev Server 4.

Please open up an issue to nudge me to keep the npm packages up-to-date. FYI, it takes time to make different packages with different versions work together nicely.

Installing and Running

Procedures:

  1. Check if your Node.js version is >= 18.
  2. Clone this repository.
  3. Change the package's name, description, and repository fields in package.json.
  4. Change the name of your extension on src/manifest.json.
  5. Run npm install to install the dependencies.
  6. Run npm start
  7. Load your extension on Chrome following:
    1. Access chrome://extensions/
    2. Check Developer mode
    3. Click on Load unpacked extension
    4. Select the build folder.
  8. Happy hacking.

Structure

All your extension's code must be placed in the src folder.

The boilerplate is already prepared to have a popup, an options page, a background page, and a new tab page (which replaces the new tab page of your browser). But feel free to customize these.

TypeScript

This boilerplate now supports TypeScript! The Options Page is implemented using TypeScript. Please refer to src/pages/Options/ for example usages.

Webpack auto-reload and HRM

To make your workflow much more efficient this boilerplate uses the webpack server to development (started with npm start) with auto reload feature that reloads the browser automatically every time that you save some file in your editor.

You can run the dev mode on other port if you want. Just specify the env var port like this:

$ PORT=6002 npm run start

Content Scripts

Although this boilerplate uses the webpack dev server, it's also prepared to write all your bundles files on the disk at every code change, so you can point, on your extension manifest, to your bundles that you want to use as content scripts, but you need to exclude these entry points from hot reloading (why?). To do so you need to expose which entry points are content scripts on the webpack.config.js using the chromeExtensionBoilerplate -> notHotReload config. Look the example below.

Let's say that you want use the myContentScript entry point as content script, so on your webpack.config.js you will configure the entry point and exclude it from hot reloading, like this:

{
  …
  entry: {
    myContentScript: "./src/js/myContentScript.js"
  },
  chromeExtensionBoilerplate: {
    notHotReload: ["myContentScript"]
  }
  …
}

and on your src/manifest.json:

{
  "content_scripts": [
    {
      "matches": ["https://www.google.com/*"],
      "js": ["myContentScript.bundle.js"]
    }
  ]
}

Intelligent Code Completion

Thanks to @hudidit's kind suggestions, this boilerplate supports chrome-specific intelligent code completion using @types/chrome.

Packing

After the development of your extension run the command

$ NODE_ENV=production npm run build

Now, the content of build folder will be the extension ready to be submitted to the Chrome Web Store. Just take a look at the official guide to more infos about publishing.

Secrets

If you are developing an extension that talks with some API you probably are using different keys for testing and production. Is a good practice you not commit your secret keys and expose to anyone that have access to the repository.

To this task this boilerplate import the file ./secrets.<THE-NODE_ENV>.js on your modules through the module named as secrets, so you can do things like this:

./secrets.development.js

export default { key: '123' };

./src/popup.js

import secrets from 'secrets';
ApiCall({ key: secrets.key });

:point_right: The files with name secrets.*.js already are ignored on the repository.

Resources:


Michael Xieyang Liu | Website