1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4  value: true
5});
6exports.withStaticPlugin = void 0;
7
8function _assert() {
9  const data = _interopRequireDefault(require("assert"));
10
11  _assert = function () {
12    return data;
13  };
14
15  return data;
16}
17
18function _getenv() {
19  const data = require("getenv");
20
21  _getenv = function () {
22    return data;
23  };
24
25  return data;
26}
27
28function _errors() {
29  const data = require("../utils/errors");
30
31  _errors = function () {
32    return data;
33  };
34
35  return data;
36}
37
38function _pluginResolver() {
39  const data = require("../utils/plugin-resolver");
40
41  _pluginResolver = function () {
42    return data;
43  };
44
45  return data;
46}
47
48function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
49
50const EXPO_DEBUG = (0, _getenv().boolish)('EXPO_DEBUG', false); // Show all error info related to plugin resolution.
51
52const EXPO_CONFIG_PLUGIN_VERBOSE_ERRORS = (0, _getenv().boolish)('EXPO_CONFIG_PLUGIN_VERBOSE_ERRORS', false); // Force using the fallback unversioned plugin instead of a local versioned copy,
53// this should only be used for testing the CLI.
54
55const EXPO_USE_UNVERSIONED_PLUGINS = (0, _getenv().boolish)('EXPO_USE_UNVERSIONED_PLUGINS', false);
56
57function isModuleMissingError(name, error) {
58  // @ts-ignore
59  if (['MODULE_NOT_FOUND', 'PLUGIN_NOT_FOUND'].includes(error.code)) {
60    return true;
61  }
62
63  return error.message.includes(`Cannot find module '${name}'`);
64}
65
66function isUnexpectedTokenError(error) {
67  if (error instanceof SyntaxError || error instanceof _errors().PluginError && error.code === 'INVALID_PLUGIN_IMPORT') {
68    return (// These are the most common errors that'll be thrown when a package isn't transpiled correctly.
69      !!error.message.match(/Unexpected token/) || !!error.message.match(/Cannot use import statement/)
70    );
71  }
72
73  return false;
74}
75/**
76 * Resolves static module plugin and potentially falls back on a provided plugin if the module cannot be resolved
77 *
78 * @param config
79 * @param fallback Plugin with `_resolverError` explaining why the module couldn't be used
80 * @param projectRoot optional project root, fallback to _internal.projectRoot. Used for testing.
81 * @param _isLegacyPlugin Used to suppress errors thrown by plugins that are applied automatically
82 */
83
84
85const withStaticPlugin = (config, props) => {
86  var _pluginProps;
87
88  let projectRoot = props.projectRoot;
89
90  if (!projectRoot) {
91    var _config$_internal;
92
93    projectRoot = (_config$_internal = config._internal) === null || _config$_internal === void 0 ? void 0 : _config$_internal.projectRoot;
94    (0, _pluginResolver().assertInternalProjectRoot)(projectRoot);
95  }
96
97  let [pluginResolve, pluginProps] = (0, _pluginResolver().normalizeStaticPlugin)(props.plugin); // Ensure no one uses this property by accident.
98
99  (0, _assert().default)(!((_pluginProps = pluginProps) !== null && _pluginProps !== void 0 && _pluginProps._resolverError), `Plugin property '_resolverError' is a reserved property of \`withStaticPlugin\``);
100  let withPlugin;
101
102  if ( // Function was provided, no need to resolve: [withPlugin, {}]
103  typeof pluginResolve === 'function') {
104    withPlugin = pluginResolve;
105  } else if (typeof pluginResolve === 'string') {
106    try {
107      // Resolve and evaluate plugins.
108      withPlugin = (0, _pluginResolver().resolveConfigPluginFunction)(projectRoot, pluginResolve); // Only force if the project has the versioned plugin, otherwise use default behavior.
109      // This helps see which plugins are being skipped.
110
111      if (EXPO_USE_UNVERSIONED_PLUGINS && !!withPlugin && !!props._isLegacyPlugin && !!props.fallback) {
112        console.log(`Force "${pluginResolve}" to unversioned plugin`);
113        withPlugin = props.fallback;
114      }
115    } catch (error) {
116      if (EXPO_DEBUG) {
117        if (EXPO_CONFIG_PLUGIN_VERBOSE_ERRORS) {
118          // Log the error in debug mode for plugins with fallbacks (like the Expo managed plugins).
119          console.log(`Error resolving plugin "${pluginResolve}"`);
120          console.log(error);
121          console.log();
122        } else {
123          const shouldMuteWarning = props._isLegacyPlugin && (isModuleMissingError(pluginResolve, error) || isUnexpectedTokenError(error));
124
125          if (!shouldMuteWarning) {
126            if (isModuleMissingError(pluginResolve, error)) {
127              // Prevent causing log spew for basic resolution errors.
128              console.log(`Could not find plugin "${pluginResolve}"`);
129            } else {
130              // Log the error in debug mode for plugins with fallbacks (like the Expo managed plugins).
131              console.log(`Error resolving plugin "${pluginResolve}"`);
132              console.log(error);
133              console.log();
134            }
135          }
136        }
137      } // TODO: Maybe allow for `PluginError`s to be thrown so external plugins can assert invalid options.
138      // If the static module failed to resolve, attempt to use a fallback.
139      // This enables support for built-in plugins with versioned variations living in other packages.
140
141
142      if (props.fallback) {
143        if (!pluginProps) pluginProps = {}; // Pass this to the fallback plugin for potential warnings about needing to install a versioned package.
144
145        pluginProps._resolverError = error;
146        withPlugin = props.fallback;
147      } else {
148        // If no fallback, throw the resolution error.
149        throw error;
150      }
151    }
152  } else {
153    throw new (_errors().PluginError)(`Plugin is an unexpected type: ${typeof pluginResolve}`, 'INVALID_PLUGIN_TYPE');
154  } // Execute the plugin.
155
156
157  config = withPlugin(config, pluginProps);
158  return config;
159};
160
161exports.withStaticPlugin = withStaticPlugin;
162//# sourceMappingURL=withStaticPlugin.js.map