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