Repository: parcel-bundler/parcel
File: packages/transformers/webextension/src/WebExtensionTransformer.js (JavaScript)

Code before:
    program.web_accessible_resources = war;
  }
  if (program.declarative_net_request?.rule_resources) {
    const rrs: {path: string, id: string, enabled: boolean}[] =
      program.declarative_net_request?.rule_resources ?? [];
    for (let i = 0; i < rrs.length; ++i) {
      const resources = rrs[i];
      const rulesPath = path.relative(assetDir, resources.path);
      if (!(await fs.exists(rulesPath))) {
        const pathSourceLoc = getJSONSourceLocation(
          ptrs[`/declarative_net_request/rule_resources/${i}/path`],
          'value',
        );
        throw new ThrowableDiagnostic({
          diagnostic: [
            {
              message: 'Invalid Web Extension manifest',
              origin: '@parcel/transformer-webextension',
              codeFrames: [
                {
                  filePath,
                  language: 'json',
                  codeHighlights: [
                    {
                      ...pathSourceLoc,
                      message: `Path does not exist`,
                    },
                  ],
                },
              ],
            },
          ],
        });
      }

      asset.addURLDependency(rulesPath, {
        needsStableName: true,
        pipeline: 'isolated',
      });
    }
  }

  for (const loc of DEP_LOCS) {
    const location = '/' + loc.join('/');
    if (!ptrs[location]) continue;
    let parent: any = program;
    for (let i = 0; i < loc.length - 1; ++i) {
      parent = parent[loc[i]];
    }
    const lastLoc = loc[loc.length - 1];
    const obj = parent[lastLoc];

Review comment (suggestion):
We could make this include the ruleset ID, e.g. do this for `codeHighlights[0]`:

```js
{
  ...getJSONSourceLocation(
    ptrs[`/declarative_net_request/rule_resources/${i}/path`],
    'value',
  ),
  message: `Ruleset file for static ruleset ${resources.id} does not exist`
}
```

This is just to accomodate the fact that in some environments codeframes aren't fully shown, so the original message would be too vague.

Code after:
      }
    }
    program.web_accessible_resources = war;
  }
  if (program.declarative_net_request) {
    const rrs: {|path: string, id: string, enabled: boolean|}[] =
      program.declarative_net_request?.rule_resources ?? [];
    rrs.forEach((resources, i) => {
      resources.path = asset.addURLDependency(resources.path, {
        pipeline: 'raw',
        loc: {
          filePath,
          ...getJSONSourceLocation(
            ptrs[`/declarative_net_request/rule_resources/${i}/path`],
            'value',
          ),
        },
      });
    });
  }

  for (const loc of DEP_LOCS) {
    const location = '/' + loc.join('/');
    if (!ptrs[location]) continue;
    let parent: any = program;
    for (let i = 0; i < loc.length - 1; ++i) {
      parent = parent[loc[i]];
    }
    const lastLoc = loc[loc.length - 1];
    const obj = parent[lastLoc];
    if (typeof obj == 'string')
      parent[lastLoc] = asset.addURLDependency(obj, {
        bundleBehavior: 'isolated',
        loc: {
          filePath,
          ...getJSONSourceLocation(ptrs[location], 'value'),
        },
        pipeline: path.extname(obj) == '.json' ? 'raw' : undefined,
      });
    else {
      for (const k of Object.keys(obj)) {
        obj[k] = asset.addURLDependency(obj[k], {
          bundleBehavior: 'isolated',
          loc: {
            filePath,
            ...getJSONSourceLocation(ptrs[location + '/' + k], 'value'),
          },
          pipeline: path.extname(obj[k]) == '.json' ? 'raw' : undefined,
        });
      }
    }