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.

How Does It Work?

Extension.js performs the following tasks for JSON resources:

  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. Error Handling: If a referenced JSON file is missing or incorrectly configured, warnings are generated to help developers fix issues during the build process.

JSON Support

The following manifest fields are used to declare JSON files:

Manifest Field File Type Expected
declarative_net_request.rule_resources .json
storage.managed_schema .json

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.

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

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.