export async function loadJSConfigFile()

in src/config.js [142:198]


export async function loadJSConfigFile(filePath) {
  const resolvedFilePath = path.resolve(filePath);
  log.debug(
    `Loading JS config file: "${filePath}" ` +
      `(resolved to "${resolvedFilePath}")`,
  );
  if (filePath.endsWith('.js')) {
    log.warn(`WARNING: config file ${filePath} ${WARN_LEGACY_JS_EXT}`);
  }
  let configObject;
  try {
    const nonce = `${Date.now()}-${Math.random()}`;
    let configModule;
    if (resolvedFilePath.endsWith('package.json')) {
      configModule = parseJSON(
        await fs.readFile(resolvedFilePath, { encoding: 'utf-8' }),
      );
    } else {
      configModule = await import(`file://${resolvedFilePath}?nonce=${nonce}`);
    }

    if (configModule.default) {
      const { default: configDefault, ...esmConfigMod } = configModule;
      // ES modules may expose both a default and named exports and so
      // we merge the named exports on top of what may have been set in
      // the default export.
      configObject = { ...configDefault, ...esmConfigMod };
    } else {
      configObject = { ...configModule };
    }
  } catch (error) {
    log.debug('Handling error:', error);
    let errorMessage = error.message;
    if (error.message.startsWith(ERR_MODULE_FROM_ESM)) {
      errorMessage = HELP_ERR_MODULE_FROM_ESM;
    } else if (
      [ERR_IMPORT_FROM_CJS, ERR_EXPORT_FROM_CJS].includes(error.message)
    ) {
      errorMessage = HELP_ERR_IMPORTEXPORT_CJS;
    }
    throw new UsageError(
      `Cannot read config file: ${resolvedFilePath}\n` +
        `Error: ${errorMessage}`,
    );
  }
  if (filePath.endsWith('package.json')) {
    log.debug('Looking for webExt key inside package.json file');
    configObject = configObject.webExt || {};
  }
  if (Object.keys(configObject).length === 0) {
    log.debug(
      `Config file ${resolvedFilePath} did not define any options. ` +
        'Did you set module.exports = {...}?',
    );
  }
  return configObject;
}