1{"version":3,"file":"plugin-resolver.js","names":["pluginFileName","findUpPackageJson","root","packageJson","findUp","sync","cwd","assert","resolvePluginForModule","projectRoot","modulePath","resolved","resolveFrom","silent","PluginError","moduleNameIsDirectFileReference","isPluginFile","filePath","findUpPlugin","pathIsFilePath","name","match","slashCount","split","path","sep","length","startsWith","resolveExpoPluginFile","pluginModuleFile","fileExists","moduleRoot","dirname","pluginFile","normalizeStaticPlugin","plugin","Array","isArray","undefined","assertInternalProjectRoot","resolveConfigPluginFunction","pluginReference","resolveConfigPluginFunctionWithInfo","result","requirePluginFile","error","SyntaxError","learnMoreLink","pluginError","message","stack","resolveConfigPluginExport","default","require"],"sources":["../../src/utils/plugin-resolver.ts"],"sourcesContent":["import assert from 'assert';\nimport findUp from 'find-up';\nimport * as path from 'path';\nimport resolveFrom from 'resolve-from';\n\nimport { ConfigPlugin, StaticPlugin } from '../Plugin.types';\nimport { PluginError } from './errors';\nimport { fileExists } from './modules';\n\n// Default plugin entry file name.\nexport const pluginFileName = 'app.plugin.js';\n\nfunction findUpPackageJson(root: string): string {\n  const packageJson = findUp.sync('package.json', { cwd: root });\n  assert(packageJson, `No package.json found for module \"${root}\"`);\n  return packageJson;\n}\n\nexport function resolvePluginForModule(projectRoot: string, modulePath: string) {\n  const resolved = resolveFrom.silent(projectRoot, modulePath);\n  if (!resolved) {\n    throw new PluginError(\n      `Failed to resolve plugin for module \"${modulePath}\" relative to \"${projectRoot}\"`,\n      'PLUGIN_NOT_FOUND'\n    );\n  }\n  // If the modulePath is something like `@bacon/package/index.js` or `expo-foo/build/app`\n  // then skip resolving the module `app.plugin.js`\n  if (moduleNameIsDirectFileReference(modulePath)) {\n    return { isPluginFile: false, filePath: resolved };\n  }\n  return findUpPlugin(resolved);\n}\n\n// TODO: Test windows\nfunction pathIsFilePath(name: string): boolean {\n  // Matches lines starting with: . / ~/\n  return !!name.match(/^(\\.|~\\/|\\/)/g);\n}\n\nexport function moduleNameIsDirectFileReference(name: string): boolean {\n  if (pathIsFilePath(name)) {\n    return true;\n  }\n\n  const slashCount = name.split(path.sep)?.length;\n  // Orgs (like @expo/config ) should have more than one slash to be a direct file.\n  if (name.startsWith('@')) {\n    return slashCount > 2;\n  }\n\n  // Regular packages should be considered direct reference if they have more than one slash.\n  return slashCount > 1;\n}\n\nfunction resolveExpoPluginFile(root: string): string | null {\n  // Find the expo plugin root file\n  const pluginModuleFile = resolveFrom.silent(\n    root,\n    // use ./ so it isn't resolved as a node module\n    `./${pluginFileName}`\n  );\n\n  // If the default expo plugin file exists use it.\n  if (pluginModuleFile && fileExists(pluginModuleFile)) {\n    return pluginModuleFile;\n  }\n  return null;\n}\n\nfunction findUpPlugin(root: string): { filePath: string; isPluginFile: boolean } {\n  // Get the closest package.json to the node module\n  const packageJson = findUpPackageJson(root);\n  // resolve the root folder for the node module\n  const moduleRoot = path.dirname(packageJson);\n  // use whatever the initial resolved file was ex: `node_modules/my-package/index.js` or `./something.js`\n  const pluginFile = resolveExpoPluginFile(moduleRoot);\n  return { filePath: pluginFile ?? root, isPluginFile: !!pluginFile };\n}\n\nexport function normalizeStaticPlugin(plugin: StaticPlugin | ConfigPlugin | string): StaticPlugin {\n  if (Array.isArray(plugin)) {\n    assert(\n      plugin.length > 0 && plugin.length < 3,\n      `Wrong number of arguments provided for static config plugin, expected either 1 or 2, got ${plugin.length}`\n    );\n    return plugin;\n  }\n  return [plugin, undefined];\n}\n\nexport function assertInternalProjectRoot(projectRoot?: string): asserts projectRoot {\n  assert(\n    projectRoot,\n    `Unexpected: Config \\`_internal.projectRoot\\` isn't defined by expo-cli, this is a bug.`\n  );\n}\n\n// Resolve the module function and assert type\nexport function resolveConfigPluginFunction(projectRoot: string, pluginReference: string) {\n  const { plugin } = resolveConfigPluginFunctionWithInfo(projectRoot, pluginReference);\n  return plugin;\n}\n\n// Resolve the module function and assert type\nexport function resolveConfigPluginFunctionWithInfo(projectRoot: string, pluginReference: string) {\n  const { filePath: pluginFile, isPluginFile } = resolvePluginForModule(\n    projectRoot,\n    pluginReference\n  );\n  let result: any;\n  try {\n    result = requirePluginFile(pluginFile);\n  } catch (error) {\n    if (error instanceof SyntaxError) {\n      const learnMoreLink = `Learn more: https://docs.expo.dev/guides/config-plugins/#creating-a-plugin`;\n      // If the plugin reference is a node module, and that node module has a syntax error, then it probably doesn't have an official config plugin.\n      if (!isPluginFile && !moduleNameIsDirectFileReference(pluginReference)) {\n        const pluginError = new PluginError(\n          `Package \"${pluginReference}\" does not contain a valid config plugin.\\n${learnMoreLink}\\n\\n${error.message}`,\n          'INVALID_PLUGIN_IMPORT'\n        );\n        pluginError.stack = error.stack;\n        throw pluginError;\n      }\n    }\n    throw error;\n  }\n\n  const plugin = resolveConfigPluginExport({\n    plugin: result,\n    pluginFile,\n    pluginReference,\n    isPluginFile,\n  });\n  return { plugin, pluginFile, pluginReference, isPluginFile };\n}\n\n/**\n * - Resolve the exported contents of an Expo config (be it default or module.exports)\n * - Assert no promise exports\n * - Return config type\n * - Serialize config\n *\n * @param props.plugin plugin results\n * @param props.pluginFile plugin file path\n * @param props.pluginReference the string used to reference the plugin\n * @param props.isPluginFile is file path from the app.plugin.js module root\n */\nexport function resolveConfigPluginExport({\n  plugin,\n  pluginFile,\n  pluginReference,\n  isPluginFile,\n}: {\n  plugin: any;\n  pluginFile: string;\n  pluginReference: string;\n  isPluginFile: boolean;\n}): ConfigPlugin<unknown> {\n  if (plugin.default != null) {\n    plugin = plugin.default;\n  }\n  if (typeof plugin !== 'function') {\n    const learnMoreLink = `Learn more: https://docs.expo.dev/guides/config-plugins/#creating-a-plugin`;\n    // If the plugin reference is a node module, and that node module does not export a function then it probably doesn't have a config plugin.\n    if (!isPluginFile && !moduleNameIsDirectFileReference(pluginReference)) {\n      throw new PluginError(\n        `Package \"${pluginReference}\" does not contain a valid config plugin. Module must export a function from file: ${pluginFile}\\n${learnMoreLink}`,\n        'INVALID_PLUGIN_TYPE'\n      );\n    }\n    throw new PluginError(\n      `Plugin \"${pluginReference}\" must export a function from file: ${pluginFile}. ${learnMoreLink}`,\n      'INVALID_PLUGIN_TYPE'\n    );\n  }\n\n  return plugin;\n}\n\nfunction requirePluginFile(filePath: string): any {\n  try {\n    return require(filePath);\n  } catch (error) {\n    // TODO: Improve error messages\n    throw error;\n  }\n}\n"],"mappings":";;;;;;;;;;;;;AAAA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAGA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AACA;EAAA;EAAA;IAAA;EAAA;EAAA;AAAA;AAAuC;AAAA;AAAA;AAEvC;AACO,MAAMA,cAAc,GAAG,eAAe;AAAC;AAE9C,SAASC,iBAAiB,CAACC,IAAY,EAAU;EAC/C,MAAMC,WAAW,GAAGC,iBAAM,CAACC,IAAI,CAAC,cAAc,EAAE;IAAEC,GAAG,EAAEJ;EAAK,CAAC,CAAC;EAC9D,IAAAK,iBAAM,EAACJ,WAAW,EAAG,qCAAoCD,IAAK,GAAE,CAAC;EACjE,OAAOC,WAAW;AACpB;AAEO,SAASK,sBAAsB,CAACC,WAAmB,EAAEC,UAAkB,EAAE;EAC9E,MAAMC,QAAQ,GAAGC,sBAAW,CAACC,MAAM,CAACJ,WAAW,EAAEC,UAAU,CAAC;EAC5D,IAAI,CAACC,QAAQ,EAAE;IACb,MAAM,KAAIG,qBAAW,EAClB,wCAAuCJ,UAAW,kBAAiBD,WAAY,GAAE,EAClF,kBAAkB,CACnB;EACH;EACA;EACA;EACA,IAAIM,+BAA+B,CAACL,UAAU,CAAC,EAAE;IAC/C,OAAO;MAAEM,YAAY,EAAE,KAAK;MAAEC,QAAQ,EAAEN;IAAS,CAAC;EACpD;EACA,OAAOO,YAAY,CAACP,QAAQ,CAAC;AAC/B;;AAEA;AACA,SAASQ,cAAc,CAACC,IAAY,EAAW;EAC7C;EACA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