1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.assertInternalProjectRoot = assertInternalProjectRoot;
7exports.moduleNameIsDirectFileReference = moduleNameIsDirectFileReference;
8exports.normalizeStaticPlugin = normalizeStaticPlugin;
9exports.pluginFileName = void 0;
10exports.resolveConfigPluginExport = resolveConfigPluginExport;
11exports.resolveConfigPluginFunction = resolveConfigPluginFunction;
12exports.resolveConfigPluginFunctionWithInfo = resolveConfigPluginFunctionWithInfo;
13exports.resolvePluginForModule = resolvePluginForModule;
14function _assert() {
15  const data = _interopRequireDefault(require("assert"));
16  _assert = function () {
17    return data;
18  };
19  return data;
20}
21function _findUp() {
22  const data = _interopRequireDefault(require("find-up"));
23  _findUp = function () {
24    return data;
25  };
26  return data;
27}
28function path() {
29  const data = _interopRequireWildcard(require("path"));
30  path = function () {
31    return data;
32  };
33  return data;
34}
35function _resolveFrom() {
36  const data = _interopRequireDefault(require("resolve-from"));
37  _resolveFrom = function () {
38    return data;
39  };
40  return data;
41}
42function _errors() {
43  const data = require("./errors");
44  _errors = function () {
45    return data;
46  };
47  return data;
48}
49function _modules() {
50  const data = require("./modules");
51  _modules = function () {
52    return data;
53  };
54  return data;
55}
56function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
57function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
58function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
59// Default plugin entry file name.
60const pluginFileName = 'app.plugin.js';
61exports.pluginFileName = pluginFileName;
62function findUpPackageJson(root) {
63  const packageJson = _findUp().default.sync('package.json', {
64    cwd: root
65  });
66  (0, _assert().default)(packageJson, `No package.json found for module "${root}"`);
67  return packageJson;
68}
69function resolvePluginForModule(projectRoot, modulePath) {
70  const resolved = _resolveFrom().default.silent(projectRoot, modulePath);
71  if (!resolved) {
72    throw new (_errors().PluginError)(`Failed to resolve plugin for module "${modulePath}" relative to "${projectRoot}"`, 'PLUGIN_NOT_FOUND');
73  }
74  // If the modulePath is something like `@bacon/package/index.js` or `expo-foo/build/app`
75  // then skip resolving the module `app.plugin.js`
76  if (moduleNameIsDirectFileReference(modulePath)) {
77    return {
78      isPluginFile: false,
79      filePath: resolved
80    };
81  }
82  return findUpPlugin(resolved);
83}
84
85// TODO: Test windows
86function pathIsFilePath(name) {
87  // Matches lines starting with: . / ~/
88  return !!name.match(/^(\.|~\/|\/)/g);
89}
90function moduleNameIsDirectFileReference(name) {
91  var _name$split;
92  if (pathIsFilePath(name)) {
93    return true;
94  }
95  const slashCount = (_name$split = name.split(path().sep)) === null || _name$split === void 0 ? void 0 : _name$split.length;
96  // Orgs (like @expo/config ) should have more than one slash to be a direct file.
97  if (name.startsWith('@')) {
98    return slashCount > 2;
99  }
100
101  // Regular packages should be considered direct reference if they have more than one slash.
102  return slashCount > 1;
103}
104function resolveExpoPluginFile(root) {
105  // Find the expo plugin root file
106  const pluginModuleFile = _resolveFrom().default.silent(root,
107  // use ./ so it isn't resolved as a node module
108  `./${pluginFileName}`);
109
110  // If the default expo plugin file exists use it.
111  if (pluginModuleFile && (0, _modules().fileExists)(pluginModuleFile)) {
112    return pluginModuleFile;
113  }
114  return null;
115}
116function findUpPlugin(root) {
117  // Get the closest package.json to the node module
118  const packageJson = findUpPackageJson(root);
119  // resolve the root folder for the node module
120  const moduleRoot = path().dirname(packageJson);
121  // use whatever the initial resolved file was ex: `node_modules/my-package/index.js` or `./something.js`
122  const pluginFile = resolveExpoPluginFile(moduleRoot);
123  return {
124    filePath: pluginFile !== null && pluginFile !== void 0 ? pluginFile : root,
125    isPluginFile: !!pluginFile
126  };
127}
128function normalizeStaticPlugin(plugin) {
129  if (Array.isArray(plugin)) {
130    (0, _assert().default)(plugin.length > 0 && plugin.length < 3, `Wrong number of arguments provided for static config plugin, expected either 1 or 2, got ${plugin.length}`);
131    return plugin;
132  }
133  return [plugin, undefined];
134}
135function assertInternalProjectRoot(projectRoot) {
136  (0, _assert().default)(projectRoot, `Unexpected: Config \`_internal.projectRoot\` isn't defined by expo-cli, this is a bug.`);
137}
138
139// Resolve the module function and assert type
140function resolveConfigPluginFunction(projectRoot, pluginReference) {
141  const {
142    plugin
143  } = resolveConfigPluginFunctionWithInfo(projectRoot, pluginReference);
144  return plugin;
145}
146
147// Resolve the module function and assert type
148function resolveConfigPluginFunctionWithInfo(projectRoot, pluginReference) {
149  const {
150    filePath: pluginFile,
151    isPluginFile
152  } = resolvePluginForModule(projectRoot, pluginReference);
153  let result;
154  try {
155    result = requirePluginFile(pluginFile);
156  } catch (error) {
157    if (error instanceof SyntaxError) {
158      const learnMoreLink = `Learn more: https://docs.expo.dev/guides/config-plugins/#creating-a-plugin`;
159      // 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.
160      if (!isPluginFile && !moduleNameIsDirectFileReference(pluginReference)) {
161        const pluginError = new (_errors().PluginError)(`Package "${pluginReference}" does not contain a valid config plugin.\n${learnMoreLink}\n\n${error.message}`, 'INVALID_PLUGIN_IMPORT');
162        pluginError.stack = error.stack;
163        throw pluginError;
164      }
165    }
166    throw error;
167  }
168  const plugin = resolveConfigPluginExport({
169    plugin: result,
170    pluginFile,
171    pluginReference,
172    isPluginFile
173  });
174  return {
175    plugin,
176    pluginFile,
177    pluginReference,
178    isPluginFile
179  };
180}
181
182/**
183 * - Resolve the exported contents of an Expo config (be it default or module.exports)
184 * - Assert no promise exports
185 * - Return config type
186 * - Serialize config
187 *
188 * @param props.plugin plugin results
189 * @param props.pluginFile plugin file path
190 * @param props.pluginReference the string used to reference the plugin
191 * @param props.isPluginFile is file path from the app.plugin.js module root
192 */
193function resolveConfigPluginExport({
194  plugin,
195  pluginFile,
196  pluginReference,
197  isPluginFile
198}) {
199  if (plugin.default != null) {
200    plugin = plugin.default;
201  }
202  if (typeof plugin !== 'function') {
203    const learnMoreLink = `Learn more: https://docs.expo.dev/guides/config-plugins/#creating-a-plugin`;
204    // 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.
205    if (!isPluginFile && !moduleNameIsDirectFileReference(pluginReference)) {
206      throw new (_errors().PluginError)(`Package "${pluginReference}" does not contain a valid config plugin. Module must export a function from file: ${pluginFile}\n${learnMoreLink}`, 'INVALID_PLUGIN_TYPE');
207    }
208    throw new (_errors().PluginError)(`Plugin "${pluginReference}" must export a function from file: ${pluginFile}. ${learnMoreLink}`, 'INVALID_PLUGIN_TYPE');
209  }
210  return plugin;
211}
212function requirePluginFile(filePath) {
213  try {
214    return require(filePath);
215  } catch (error) {
216    // TODO: Improve error messages
217    throw error;
218  }
219}
220//# sourceMappingURL=plugin-resolver.js.map