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

Code before:
          },
          env: {
            context: 'service-worker',
            sourceType:
              program.background.type == 'module' ? 'module' : 'script',
          },
        },
      );
    }
    if (hot) {
      // Enable eval HMR for sandbox,
      const csp = program.content_security_policy || {};
      csp.extension_pages = cspPatchHMR(
        csp.extension_pages,
        `http://${hmrOptions?.host || 'localhost'}`,
      );
      // Sandbox allows eval by default
      if (csp.sandbox) csp.sandbox = cspPatchHMR(csp.sandbox);
      program.content_security_policy = csp;
      if (needRuntimeBG) {
        if (!program.background) {
          program.background = {};
        }
        if (!program.background.service_worker) {
          program.background.service_worker = asset.addURLDependency(
            './runtime/default-bg.js',
            {
              resolveFrom: __filename,
              env: {context: 'service-worker'},
            },
          );
        }
        asset.meta.webextBGInsert = program.background.service_worker;
      }
    }
  }
}

function cspPatchHMR(policy: ?string, insert?: string) {
  let defaultSrc = "'self'";
  if (insert == null) {
    insert = "'unsafe-eval'";
    defaultSrc = "'self' blob: filesystem:";
  }
  if (policy) {
    const csp = parseCSP(policy);
    policy = '';
    if (!csp['script-src']) {
      csp['script-src'] = [defaultSrc];
    }
    if (!csp['script-src'].includes(insert)) {

Review comment (bug):
I'm getting an error here: `@parcel/transformer-webextension: Cannot read properties of undefined (reading 'flatMap')`

Trying to build a MV2 without content_scripts

Code after:
        program.background.scripts.push(
          asset.addURLDependency('./runtime/default-bg.js', {
            resolveFrom: __filename,
          }),
        );
      }
      asset.meta.webextBGInsert = program.background.scripts[0];
    } else {
      if (!program.background.service_worker) {
        program.background.service_worker = asset.addURLDependency(
          './runtime/default-bg.js',
          {
            resolveFrom: __filename,
            env: {context: 'service-worker'},
          },
        );
      }
      asset.meta.webextBGInsert = program.background.service_worker;
    }

    if (!program.permissions) program.permissions = [];
    if (!isMV2 && !program.permissions.includes('scripting')) {
      program.permissions.push('scripting');
    }
    const hostPerms = [
      ...new Set(program.content_scripts.flatMap(sc => sc.matches)),
    ];
    if (isMV2) program.permissions = program.permissions.concat(hostPerms);
    else {
      if (!program.host_permissions) program.host_permissions = [];
      program.host_permissions = program.host_permissions.concat(hostPerms);
    }
  }
}

function cspPatchHMR(policy: ?string, insert?: string) {
  let defaultSrc = "'self'";
  if (insert == null) {
    insert = "'unsafe-eval'";
    defaultSrc = "'self' blob: filesystem:";
  }
  if (policy) {
    const csp = parseCSP(policy);
    policy = '';
    if (!csp['script-src']) {
      csp['script-src'] = [defaultSrc];
    }
    if (!csp['script-src'].includes(insert)) {
      csp['script-src'].push(insert);
    }
    if (csp.sandbox && !csp.sandbox.includes('allow-scripts')) {