JSON

JSON in browser extensions provide data for rulesets, managed storage schemas, and more. Extension.js handles the inclusion, bundling, and processing of JSON files referenced in the manifest.json, ensuring they are correctly emitted and added to the compilation output.

JSON File Support

Extension.js supports handling JSON files defined in the manifest.json, ensuring that they are processed and output correctly. This includes rulesets for declarative network requests and storage-managed schemas. The following manifest fields are supported:

Manifest Field File Type Expected HMR Support
declarative_net_request.rule_resources .json Yes
storage.managed_schema .json Yes

Sample JSON Declaration in manifest.json

Here’s an example of how to declare a JSON file in your manifest.json:

{
  "manifest_version": 3,
  "name": "My Extension",
  "version": "1.0",
  "declarative_net_request": {
    "rule_resources": [
      {
        "id": "ruleset_1",
        "enabled": true,
        "path": "rules/ruleset.json"
      }
    ]
  },
  "storage": {
    "managed_schema": "schema/storage-schema.json"
  }
}

This configuration ensures that Extension.js will process the JSON files and include them in the final output of your extension.

Handling JSON Files

If you have additional JSON files that are not directly declared in the manifest.json, such as extra configuration or data files, you can place these JSON files in the /public folder. Extension.js will include these files in the build output as-is, ensuring they are accessible for use throughout your extension.

Example Usage: Place your JSON files in the /public/json folder:

public/
└── json/
    β”œβ”€β”€ ruleset.json
    └── storage-schema.json

For more information, refer to the Special Folders documentation.

Output Path

The output path for JSON files follows a simple structure based on their context in the manifest. For example, the resulting files would look like this:

dist/
  [browser]/
  β”œβ”€β”€ declarative_net_request/
  β”‚   └── rule_resources-0.json
  β”œβ”€β”€ storage/
  β”‚   └── managed_schema.json
  └── other-extension-files

How Extension.js Handles JSON

The plugin responsible for handling JSON files in Extension.js ensures that the following steps occur during compilation:

  1. Emit JSON Files: The JSON files referenced in the manifest.json are emitted to the output directory.
  2. Add to File Dependencies: JSON files are added to the file dependencies of the compilation, ensuring that any updates to the JSON files trigger a recompile.
  3. Support for Multiple Features: JSON handling supports declarative network requests, managed storage schemas, and other JSON-based manifest fields.
  4. Error Handling: If a referenced JSON file is missing or incorrectly configured, warnings are generated to help developers fix issues during the build process.

Best Practices

  • Use the /public/ folder for additional JSON files that are not directly declared in the manifest.
  • Ensure that JSON files are well-formed and follow the correct schema for their intended use (e.g., rulesets, managed schemas).

Next Steps

  • Learn more about handling JSON files and other static assets via Special Folders.