Skip to content

Commit

Permalink
A bunch of updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto committed Nov 3, 2024
1 parent 16b27af commit 302c751
Show file tree
Hide file tree
Showing 16 changed files with 724 additions and 351 deletions.
2 changes: 1 addition & 1 deletion docs/en/docs/commands/build.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The `build` command in Extension.js is responsible for compiling your extension
This command creates optimized builds for different browsers, ensuring compatibility and performance.
The `build/` folder will contain subfolders for each targeted browser, each with the necessary code transforms and optimizations.

## How it Works?
## How Does It Work?

During the build process, the following steps take place:

Expand Down
15 changes: 7 additions & 8 deletions docs/en/docs/commands/create.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PackageManagerTabs } from "@theme";
import { AvatarImage } from "../../../../theme/components/avatar-image";
import {CarouselTemplates} from "../../../../theme/components/carousel-templates";
import { CarouselTemplates } from "../../../../theme/components/carousel-templates";

<AvatarImage icon="extension" />

Expand All @@ -12,7 +12,7 @@ and allows you to define the directory or path where the extension will be creat
The command is designed to simplify the setup process for developers,
providing a streamlined way to get started with cross-browser extension development.

## How it Works?
## How Does It Work?

![Create Command](https://github.com/user-attachments/assets/666c4f07-7303-4bdc-971f-9c3684adb06f)

Expand Down Expand Up @@ -67,11 +67,11 @@ In this example, the extension is created in the `path/to/my-extension` director

The `create` command accepts several arguments and flags to customize the behavior of the extension creation:

| Flag | Argument | What it does | Defaults to |
| ---------------- | ----------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------ |
| `[path or name]` | The extension path or name | If a path is defined, loads the local extension. If a name is provided, loads the extension in the current working directory. | `process.cwd()` |
| -t, --template | Name of the template used to bootstrap your extension | Bootstrap your extension using a template | [new](../getting-started/templates#template-new) |
| --install | Whether or not to auto install the template dependencies | Install dependencies and devDependencies during creation time | `true` |
| Flag | Argument | What it does | Defaults to |
| ---------------- | -------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------ |
| `[path or name]` | The extension path or name | If a path is defined, loads the local extension. If a name is provided, loads the extension in the current working directory. | `process.cwd()` |
| -t, --template | Name of the template used to bootstrap your extension | Bootstrap your extension using a template | [new](../getting-started/templates#template-new) |
| --install | Whether or not to auto install the template dependencies | Install dependencies and devDependencies during creation time | `true` |

- **Path or Name:** If a path is provided, the extension is created at that location. If a name is provided, it is created in the current working directory.
- **`--template`:** Specifies the template to use when bootstrapping the extension (e.g., `react`, `typescript`).
Expand All @@ -92,7 +92,6 @@ This command creates a new extension named my-new-extension using the React temp

<CarouselTemplates />


## Best Practices

- Starting from a Template: Using a template is the fastest way to get started with browser extension development in Extension.js.
Expand Down
24 changes: 12 additions & 12 deletions docs/en/docs/commands/dev.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AvatarImage } from "../../../../theme/components/avatar-image";
The `dev` command watches for changes in your extension's files, recompiles them, and reloads the extension in real-time. If you provide a URL, Extension.js will download and run the extension as if it were local.

## How it Works?
## How Does It Work?

For development frameworks, it's an industry-standard practice to have a development server that watches for changes in your files and recompiles them in real time. Given the complex nature of browser extensions, the `dev` command in Extension.js handles the watch and recompile process for all the different contexts (HTML or not) of your extension.

Expand Down Expand Up @@ -46,16 +46,16 @@ In this example, the extension is developed in the `path/to/my-extension` direct

Below is a breakdown of the available flags for the `dev` command:

| Flag | Argument | What it does | Defaults to |
| ----------------- | --------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ |
| `[path or url]` | Extension path or remote URL | If a path is defined, it loads the local extension. If a URL is provided, it pulls the extension from a remote source and loads it as a local extension | `process.cwd()` |
| `--polyfill` | Boolean | Enables compatibility for the `browser.*` API in Chromium-based browsers | `false` |
| `--profile` | Profile path | Specifies a path to a browser profile for testing | `default` |
| `-b, --browser` | Browser to run the extension | Specifies the browser to run (`chrome`, `edge`, `firefox`, `all`) | `"chrome"` |
| `--chromium-binary` | Path to the Chromium binary | Provides the path to a custom Chromium-based browser binary [Read more](../browsers/unsupported.mdx) | `undefined` |
| `--gecko-binary` | Path to the Gecko binary | Provides the path to a custom Gecko-based browser binary [Read more](../browsers/unsupported.mdx) | `undefined` |
| `--starting-url` | URL | Starting URL for testing with the extension | `chrome://newtab` |
| `--open` | Boolean | Whether or not to automatically open the browser upon starting dev | `true` |
| Flag | Argument | What it does | Defaults to |
| ------------------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------- |
| `[path or url]` | Extension path or remote URL | If a path is defined, it loads the local extension. If a URL is provided, it pulls the extension from a remote source and loads it as a local extension | `process.cwd()` |
| `--polyfill` | Boolean | Enables compatibility for the `browser.*` API in Chromium-based browsers | `false` |
| `--profile` | Profile path | Specifies a path to a browser profile for testing | `default` |
| `-b, --browser` | Browser to run the extension | Specifies the browser to run (`chrome`, `edge`, `firefox`, `all`) | `"chrome"` |
| `--chromium-binary` | Path to the Chromium binary | Provides the path to a custom Chromium-based browser binary [Read more](../browsers/unsupported.mdx) | `undefined` |
| `--gecko-binary` | Path to the Gecko binary | Provides the path to a custom Gecko-based browser binary [Read more](../browsers/unsupported.mdx) | `undefined` |
| `--starting-url` | URL | Starting URL for testing with the extension | `chrome://newtab` |
| `--open` | Boolean | Whether or not to automatically open the browser upon starting dev | `true` |

## Examples

Expand Down Expand Up @@ -92,7 +92,7 @@ Below is a breakdown of the available flags for the `dev` command:
npm: "extension dev ./my-extension --browser firefox",
pnpm: "extension dev ./my-extension --browser firefox",
yarn: "extension dev ./my-extension --browser firefox",
}}
}}
/>

### Running in All Browsers at Once
Expand Down
18 changes: 9 additions & 9 deletions docs/en/docs/commands/preview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AvatarImage } from "../../../../theme/components/avatar-image";
**The `preview` command allows you to run your extension in a production-like environment without making a final build.** It simulates how the extension will behave in a production browser environment. This is helpful for testing before officially deploying or building the extension.

## How it Works?
## How Does It Work?

The `build` command compiles your extension and creates a production-ready package that the `preview` command can run. The `preview` command runs the production-ready package in a browser environment, allowing you to test the extension's functionality.

Expand Down Expand Up @@ -40,14 +40,14 @@ In this example, the extension is created in the `path/to/my-extension` director

## Arguments And Flags

| Flag | Argument | What it does | Defaults to |
| ------------- | ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | --------------- |
| [path or url] | The extension path or the remote extension URL | If a path is defined, previews the local extension. If a URL is provided, pulls the extension from remote source and previews it as a local extension | `process.cwd()` |
| `--profile` | Profile path | Specifies a path to a browser profile for testing | `default` |
| `-b, --browser` | Browser to run the extension | Specifies the browser to run (`chrome`, `edge`, `firefox`, `all`) | `"chrome"` |
| `--chromium-binary` | Path to the Chromium binary | Provides the path to a custom Chromium-based browser binary [Read more](../browsers/unsupported.mdx) | `undefined` |
| `--gecko-binary` | Path to the Gecko binary | Provides the path to a custom Gecko-based browser binary [Read more](../browsers/unsupported.mdx) | `undefined` |
| `--starting-url` | URL | Starting URL for testing with the extension | `chrome://newtab` |
| Flag | Argument | What it does | Defaults to |
| ------------------- | ---------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------- |
| [path or url] | The extension path or the remote extension URL | If a path is defined, previews the local extension. If a URL is provided, pulls the extension from remote source and previews it as a local extension | `process.cwd()` |
| `--profile` | Profile path | Specifies a path to a browser profile for testing | `default` |
| `-b, --browser` | Browser to run the extension | Specifies the browser to run (`chrome`, `edge`, `firefox`, `all`) | `"chrome"` |
| `--chromium-binary` | Path to the Chromium binary | Provides the path to a custom Chromium-based browser binary [Read more](../browsers/unsupported.mdx) | `undefined` |
| `--gecko-binary` | Path to the Gecko binary | Provides the path to a custom Gecko-based browser binary [Read more](../browsers/unsupported.mdx) | `undefined` |
| `--starting-url` | URL | Starting URL for testing with the extension | `chrome://newtab` |

## Examples

Expand Down
6 changes: 4 additions & 2 deletions docs/en/docs/development/_meta.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
[
{ "name": "manifest-json", "label": "Manifest" },
{ "name": "manifest-json", "label": "manifest.json" },
{ "name": "html", "label": "HTML" },
{ "name": "scripts", "label": "Scripts" },
{ "name": "css", "label": "CSS" },
{ "name": "javascript", "label": "JavaScript" },
{ "name": "background", "label": "Background Scripts / Service Worker" },
{ "name": "content_scripts", "label": "Content Scripts" },
{ "name": "locales", "label": "Locales" },
{ "name": "json", "label": "JSON" },
{ "name": "icons", "label": "Icons" },
Expand Down
77 changes: 77 additions & 0 deletions docs/en/docs/development/background.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { PackageManagerTabs } from "@theme";
import { AvatarImage } from "../../../../theme/components/avatar-image";

<AvatarImage icon="background" />

# Background Scripts

Background scripts are JavaScript files running in the background of an extension, providing persistent functionality such as handling events, storing data, and managing state. Extension.js supports background scripts in both `background.scripts` and `background.service_worker` formats, providing full control over long-lived processes within your extension.

## How It Works?

Extension.js handles background scripts with the following features:

1. **Persistent Event Listeners:** Background scripts can register persistent listeners to respond to browser events, API calls, or messages from content scripts.
2. **Hot-Module Replacement (HMR):** For development, HMR is supported for background scripts declared under `background.scripts`, allowing faster feedback without restarting the extension.
3. **Service Worker Mode:** When using `background.service_worker`, the background script will run as a service worker, aligning with Manifest V3’s requirements.

## Background Script Support

The following fields in `manifest.json` are used to declare background scripts:

| Manifest Field | File Type Expected | HMR Support |
| --------------------------- | ------------------ | ----------- |
| `background.service_worker` | .js, .ts, .mjs | No |
| `background.scripts` | .js, .ts, .mjs | Yes |

## Sample Background Script Declaration

Below is an example of how to declare a background script within the `manifest.json` file:

```json
{
"manifest_version": 3,
"name": "My Extension",
"version": "1.0",
"background": {
"service_worker": "./scripts/background-service-worker.js"
}
}
```

This configuration defines `background-service-worker.js` as a service worker, which will persist and respond to events as needed by your extension.

## Handling Additional Background Scripts

Place any additional background scripts that are not directly declared in `manifest.json` into the `/scripts` folder. Extension.js processes these files for inclusion in the build.

**Example Usage:**

```plaintext
scripts/
└── extra-background-script.js
```

## Output Path

Background scripts will be output in the following directory structure:

```plaintext
dist/
[browser]/
├── background/
│ └── background-service-worker.js
│ └── extra-background-script.js
└── other-extension-files
```

## Best Practices

- **Use Service Workers for Manifest V3 Compliance:** With Manifest V3, consider using `background.service_worker` to ensure compatibility.
- **Organize Scripts in `/scripts` Folder:** Maintain a structured `/scripts` folder for modularity and code clarity.
- **Optimize Long-Running Processes:** Background scripts should be optimized for performance, especially in service worker mode where resources are limited.

## Next Steps

- Learn more about structuring and using [Special Folders](../features/special-folders.mdx).
- Discover the options for managing resources and event handling with [Extension.js Background API](../features/background-api).
Loading

0 comments on commit 302c751

Please sign in to comment.