Plasmo Framework

repository·main·Indexed 12 days ago

https://github.com/plasmohq/plasmo

A high-level framework for browser extension development that automates configuration, bundling, and manifest management. It features file-based routing for popups, options, and content scripts, first-class React and TypeScript support, and a comprehensive messaging API including hooks like useMessage and usePort, as well as a PubSub hub for cross-context communication.

Tokens
36.7K
Snippets
136
Records
185
Agent score
96%

What's inside Plasmo

  1. Overview of Plasmo Framework features

    main

    Plasmo is a battery-packed SDK for building browser extensions with features including:

    • Framework Support: First-class React and TypeScript support (with optional Svelte and Vue support).
    • Developer Experience: Live-reloading, React HMR, and declarative development.
    • Built-in APIs: Storage API, Messaging API, and support for .env* files.
    • Advanced Capabilities: Content Scripts UI, Tab Pages, and remote code bundling (e.g., for Google Analytics).
    • Workflow: Targeting multiple browser/manifest pairs and automated deployment via BPP.
  2. Core Plasmo features

    main

    Plasmo is a browser extension SDK designed to simplify development by handling manifest files and browser-specific complexities. Key features include:

    • First-class support: React and TypeScript.
    • Declarative development: Automatic manifest generation.
    • UI Capabilities: Content Script UI (CSUI) and Tab Pages.
    • Developer Experience: Live reloading, React HMR, and .env* file support.
    • APIs: Built-in Storage API and Messaging API.
    • Advanced Workflows: Remote code bundling (e.g., for Google Analytics), multi-browser/manifest targeting, and automatic deployment via BPP (Browser Platform Publisher).
    • Framework Options: Optional support for Svelte and Vue.
  3. Plasmo Framework Features

    main

    Plasmo provides several developer-experience features for building browser extensions:

    • React + TypeScript Support: Direct support for modern web development workflows.
    • Declarative Development: Automatically generates the manifest.json (MV3) based on your file structure.
    • Automatic Reloading: Instant feedback during development.
    • Environment Variables: Support for .env* files.
    • Remote Code Bundling: Ability to bundle external scripts (e.g., for gtag4).
    • Automated Deployment: Streamlined submission via BPP.
  4. Plasmo Framework overview and features

    main

    Plasmo is a browser extension development kit designed for React and TypeScript users. Key features include:

    • First-class React + TypeScript support
    • Declarative Development: Manifest configuration is handled via file structure.
    • Content Scripts UI: Ability to inject React components into web pages.
    • Tab Pages: Support for dedicated extension tab pages.
    • Built-in APIs: Includes Storage API, Messaging API, and support for .env* files.
    • Advanced Workflows: Remote code bundling (e.g., for Google Analytics), multi-browser/manifest targeting, and automatic deployment via Browser Platform Publisher (BPP).
    • Framework Agnostic: While optimized for React, it offers optional support for Svelte and Vue.
  5. Core Features of the Plasmo Framework

    main

    Plasmo is an SDK for developing cross-platform browser extensions, often described as 'Next.js for browser extensions'. Key features include:

    • React & TypeScript Support: First-class support for modern web development.
    • Declarative Manifest: Automatic manifest.json (MV3) configuration.
    • UI-enabled Content Scripts: Support for rendering UI within content scripts.
    • Tab Pages: Easy creation of extension tab pages.
    • Developer Experience: Active reloading and React HMR.
    • Environment Variables: Support for .env* files.
    • Built-in APIs: Specialized APIs for Storage and Messaging (communication between different parts of the extension).
    • Remote Code Support: Ability to connect remote code (e.g., for Google Analytics).
    • Cross-platform: Support for various manifest types and build targets.
    • Automated Deployment: Integration with BPP (Browser Platform Publisher).
    • Framework Extensions: Additional support for Svelte and Vue via plugins.
  6. Project Structure and File Mapping

    main

    Plasmo uses a file-based routing/structure system to define different parts of your browser extension. You can place these files in the root directory or within a src directory.

    Core Extension Files

    • Popup: Edit popup.tsx to change the extension popup UI.
    • Options Page: Edit options.tsx to change the extension options page.
    • Content Scripts: Edit content.ts to modify scripts that run in the context of web pages.
    • Background Service Worker: Edit background.ts to modify the background script.

    Example Directory Layout

    You can organize your project as follows:

    ext-dir
    ├───assets
    │   └───icon.png
    ├───popup
    │   ├───index.tsx
    │   └───button.tsx
    ├───options
    │   ├───index.tsx
    │   ├───utils.ts
    │   └───input.tsx
    ├───contents
    │   ├───site-one.ts
    │   ├───site-two.ts
    │   └───site-three.ts

    Note: If you move source code to a src directory, assets and configuration files must remain in the project root.

  7. Understand the Plasmo project structure

    main

    Plasmo uses a file-based routing system to define different parts of your browser extension. You can place these files in the root directory or organize them into subdirectories.

    Core Extension Files

    • popup.tsx: Defines the extension's popup UI.
    • options.tsx: Defines the extension's options page.
    • content.ts: Defines content scripts that run in the context of web pages.
    • background.ts: Defines the background service worker.

    Directory Organization

    You can organize your project by moving these files into specific folders:

    ext-dir
    ├───assets
    |   └───icon512.png
    ├───popup
    |   ├───index.tsx
    |   └───button.tsx
    ├───options
    |   ├───index.tsx
    |   ├───utils.ts
    |   └───input.tsx
    ├───contents
    |   ├───site-one.ts
    |   ├───site-two.ts
    |   └───site-three.ts

    Note: You can also move source code into a src subdirectory, but assets and configuration files must remain in the project root.

  8. Understand Plasmo project structure and file mapping

    main

    Plasmo uses a declarative approach where specific filenames map to extension components. By default, changes to these files trigger updates to the corresponding extension parts:

    • popup.tsx: Updates the extension's popup UI.
    • options.tsx: Updates the extension's options page.
    • content.ts: Updates content scripts.
    • background.ts: Updates the background service worker.

    You can organize these files into subdirectories (e.g., popup/index.tsx, contents/site-one.ts). If you prefer to move your source code into a src directory, follow the official customization guide, but note that assets and configuration files must remain in the root directory.

    ext-dir
    ├───assets
    |   └───icon.png
    ├───popup
    |   ├───index.tsx
    |   └───button.tsx
    ├───options
    |   ├───index.tsx
    |   ├───utils.ts
    |   └───input.tsx
    ├───contents
    |   ├───site-one.ts
    |   ├───site-two.ts
    |   └───site-three.ts
  9. Core entry points for extension components

    main

    Plasmo uses a file-based routing system for extension components. Depending on what part of the extension you want to build, you should edit the following files:

    • Popup: popup.tsx (the UI that appears when clicking the extension icon)
    • Options page: options.tsx (the settings page for the extension)
    • Content Scripts: content.ts (scripts that run in the context of web pages)
    • Background Service Worker: background.ts (the background logic of the extension)
  10. Project Structure and File Organization

    main

    Plasmo uses a file-based routing system for extension components. By default, files in the root directory define the extension's entry points. You can also move your source code into a src directory to keep the root clean, though assets and configuration files must remain in the root.

    Core Entry Points

    • popup.tsx: Defines the extension's popup UI.
    • options.tsx: Defines the extension's options page.
    • content.ts: Defines content scripts that run in the context of web pages.
    • background.ts: Defines the background service worker.

    Example Directory Layout

    ext-dir
    ├───assets
    │   └───icon512.png
    ├───popup
    │   ├───index.tsx
    │   └───button.tsx
    ├───options
    │   ├───index.tsx
    │   ├───utils.ts
    │   └───input.tsx
    ├───contents
    │   ├───site-one.ts
    │   ├───site-two.ts
    │   └───site-three.ts
  11. Understand Plasmo's file-based routing and entry points

    main

    Plasmo uses a declarative development model where specific filenames correspond to different parts of the browser extension. By default, you can place these files in your root directory:

    • popup.tsx: Defines the extension's popup UI.
    • options.tsx: Defines the extension's options page.
    • content.ts: Defines content scripts that run in the context of web pages.
    • background.ts: Defines the background service worker.

    You can also organize these into sub-directories (e.g., popup/index.tsx, contents/site-one.ts) or move all source code into a src directory to keep your root clean. Note that assets and configuration files must remain in the root directory.

    ext-dir
    ├───assets
    │   └───icon.png
    ├───popup
    │   ├───index.tsx
    │   └───button.tsx
    ├───options
    │   ├───index.tsx
    │   ├───utils.ts
    │   └───input.tsx
    ├───contents
    │   ├───site-one.ts
    │   ├───site-two.ts
    │   └───site-three.ts
  12. Quickstart: Create a new Plasmo project

    main

    To start a new browser extension project with Plasmo, use the pnpm create plasmo command. After initialization, navigate into the directory and start the development server to enable Hot Module Replacement (HMR) and active reloading.

    System Requirements:

    • Node.js 16.x or higher
    • MacOS, Windows, or Linux
    • pnpm (strongly recommended)
    pnpm create plasmo example-dir
    cd example-dir
    pnpm dev